Skip to content

Commit c145017

Browse files
committed
Simplify construction of URI instances using parser interface.
1 parent b309c53 commit c145017

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

lib/uri/common.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ def self.scheme_list
145145
@@schemes
146146
end
147147

148+
#
149+
# Construct a URI instance, using the scheme to detect the appropriate class
150+
# from +URI.scheme_list+.
151+
#
152+
def self.for(scheme, *arguments, default: Generic)
153+
if scheme
154+
uri_class = @@schemes[scheme.upcase] || default
155+
else
156+
uri_class = default
157+
end
158+
159+
return uri_class.new(scheme, *arguments)
160+
end
161+
148162
#
149163
# Base class for all URI exceptions.
150164
#

lib/uri/rfc2396_parser.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,9 @@ def split(uri)
208208
# #=> #<URI::LDAP ldap://ldap.example.com/dc=example?user=john>
209209
#
210210
def parse(uri)
211-
scheme, userinfo, host, port,
212-
registry, path, opaque, query, fragment = self.split(uri)
213-
214-
if scheme && URI.scheme_list.include?(scheme.upcase)
215-
URI.scheme_list[scheme.upcase].new(scheme, userinfo, host, port,
216-
registry, path, opaque, query,
217-
fragment, self)
218-
else
219-
Generic.new(scheme, userinfo, host, port,
220-
registry, path, opaque, query,
221-
fragment, self)
222-
end
211+
URI.for(*self.split(uri), self)
223212
end
224213

225-
226214
#
227215
# == Args
228216
#

lib/uri/rfc3986_parser.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,7 @@ def split(uri) #:nodoc:
6969
end
7070

7171
def parse(uri) # :nodoc:
72-
scheme, userinfo, host, port,
73-
registry, path, opaque, query, fragment = self.split(uri)
74-
scheme_list = URI.scheme_list
75-
if scheme && scheme_list.include?(uc = scheme.upcase)
76-
scheme_list[uc].new(scheme, userinfo, host, port,
77-
registry, path, opaque, query,
78-
fragment, self)
79-
else
80-
Generic.new(scheme, userinfo, host, port,
81-
registry, path, opaque, query,
82-
fragment, self)
83-
end
72+
URI.for(*self.split(uri), self)
8473
end
8574

8675

0 commit comments

Comments
 (0)