diff --git a/lib/uri/common.rb b/lib/uri/common.rb index e3ed405857..799a268b93 100644 --- a/lib/uri/common.rb +++ b/lib/uri/common.rb @@ -145,6 +145,20 @@ module URI @@schemes end + # + # Construct a URI instance, using the scheme to detect the appropriate class + # from +URI.scheme_list+. + # + def self.for(scheme, *arguments, default: Generic) + if scheme + uri_class = @@schemes[scheme.upcase] || default + else + uri_class = default + end + + return uri_class.new(scheme, *arguments) + end + # # Base class for all URI exceptions. # diff --git a/lib/uri/rfc2396_parser.rb b/lib/uri/rfc2396_parser.rb index 556da20aa2..38c0c36b6d 100644 --- a/lib/uri/rfc2396_parser.rb +++ b/lib/uri/rfc2396_parser.rb @@ -208,21 +208,9 @@ module URI # #=> # # def parse(uri) - scheme, userinfo, host, port, - registry, path, opaque, query, fragment = self.split(uri) - - if scheme && URI.scheme_list.include?(scheme.upcase) - URI.scheme_list[scheme.upcase].new(scheme, userinfo, host, port, - registry, path, opaque, query, - fragment, self) - else - Generic.new(scheme, userinfo, host, port, - registry, path, opaque, query, - fragment, self) - end + URI.for(*self.split(uri), self) end - # # == Args # diff --git a/lib/uri/rfc3986_parser.rb b/lib/uri/rfc3986_parser.rb index 08539f069a..49a594c17d 100644 --- a/lib/uri/rfc3986_parser.rb +++ b/lib/uri/rfc3986_parser.rb @@ -69,18 +69,7 @@ module URI end def parse(uri) # :nodoc: - scheme, userinfo, host, port, - registry, path, opaque, query, fragment = self.split(uri) - scheme_list = URI.scheme_list - if scheme && scheme_list.include?(uc = scheme.upcase) - scheme_list[uc].new(scheme, userinfo, host, port, - registry, path, opaque, query, - fragment, self) - else - Generic.new(scheme, userinfo, host, port, - registry, path, opaque, query, - fragment, self) - end + URI.for(*self.split(uri), self) end