@@ -68,16 +68,32 @@ module Schemes
68
68
end
69
69
private_constant :Schemes
70
70
71
+ # Registers the given +klass+ as the class to be instantiated
72
+ # when parsing a \URI with the given +scheme+:
71
73
#
72
- # Register the given +klass+ to be instantiated when parsing URLs with the given +scheme+.
73
- # Note that currently only schemes which after .upcase are valid constant names
74
- # can be registered (no -/+/. allowed).
74
+ # URI.register_scheme('MS_SEARCH', URI::Generic) # => URI::Generic
75
+ # URI.scheme_list['MS_SEARCH'] # => URI::Generic
75
76
#
77
+ # Note that after calling String#upcase on +scheme+, it must be a valid
78
+ # constant name.
76
79
def self . register_scheme ( scheme , klass )
77
80
Schemes . const_set ( scheme . to_s . upcase , klass )
78
81
end
79
82
80
- # Returns a Hash of the defined schemes.
83
+ # Returns a hash of the defined schemes:
84
+ #
85
+ # URI.scheme_list
86
+ # # =>
87
+ # {"MAILTO"=>URI::MailTo,
88
+ # "LDAPS"=>URI::LDAPS,
89
+ # "WS"=>URI::WS,
90
+ # "HTTP"=>URI::HTTP,
91
+ # "HTTPS"=>URI::HTTPS,
92
+ # "LDAP"=>URI::LDAP,
93
+ # "FILE"=>URI::File,
94
+ # "FTP"=>URI::FTP}
95
+ #
96
+ # Related: URI.register_scheme.
81
97
def self . scheme_list
82
98
Schemes . constants . map { |name |
83
99
[ name . to_s . upcase , Schemes . const_get ( name ) ]
@@ -88,9 +104,21 @@ def self.scheme_list
88
104
private_constant :INITIAL_SCHEMES
89
105
Ractor . make_shareable ( INITIAL_SCHEMES ) if defined? ( Ractor )
90
106
107
+ # Returns a new object constructed from the given +scheme+, +arguments+,
108
+ # and +default+:
109
+ #
110
+ # - The new object is an instance of <tt>URI.scheme_list[scheme.upcase]</tt>.
111
+ # - The object is initialized by calling the class initializer
112
+ # using +scheme+ and +arguments+.
113
+ # See URI::Generic.new.
91
114
#
92
- # Construct a URI instance, using the scheme to detect the appropriate class
93
- # from +URI.scheme_list+.
115
+ # Examples:
116
+ #
117
+ # values = ['john.doe', 'www.example.com', '123', nil, '/forum/questions/', nil, 'tag=networking&order=newest', 'top']
118
+ # URI.for('https', *values)
119
+ # # => #<URI::HTTPS https://[email protected] :123/forum/questions/?tag=networking&order=newest#top>
120
+ # URI.for('foo', *values, default: URI::HTTP)
121
+ # # => #<URI::HTTP foo://[email protected] :123/forum/questions/?tag=networking&order=newest#top>
94
122
#
95
123
def self . for ( scheme , *arguments , default : Generic )
96
124
const_name = scheme . to_s . upcase
@@ -121,73 +149,37 @@ class InvalidComponentError < Error; end
121
149
#
122
150
class BadURIError < Error ; end
123
151
124
- #
125
- # == Synopsis
126
- #
127
- # URI::split(uri)
128
- #
129
- # == Args
130
- #
131
- # +uri+::
132
- # String with URI.
133
- #
134
- # == Description
135
- #
136
- # Splits the string on following parts and returns array with result:
137
- #
138
- # * Scheme
139
- # * Userinfo
140
- # * Host
141
- # * Port
142
- # * Registry
143
- # * Path
144
- # * Opaque
145
- # * Query
146
- # * Fragment
147
- #
148
- # == Usage
149
- #
150
- # require 'uri'
151
- #
152
- # URI.split("http://www.ruby-lang.org/")
153
- # # => ["http", nil, "www.ruby-lang.org", nil, nil, "/", nil, nil, nil]
152
+ # Returns a 9-element array representing the parts of the \URI
153
+ # formed from the string +uri+;
154
+ # each array element is a string or +nil+:
155
+ #
156
+ # names = %w[scheme userinfo host port registry path opaque query fragment]
157
+ # values = URI.split('https://[email protected] :123/forum/questions/?tag=networking&order=newest#top')
158
+ # names.zip(values)
159
+ # # =>
160
+ # [["scheme", "https"],
161
+ # ["userinfo", "john.doe"],
162
+ # ["host", "www.example.com"],
163
+ # ["port", "123"],
164
+ # ["registry", nil],
165
+ # ["path", "/forum/questions/"],
166
+ # ["opaque", nil],
167
+ # ["query", "tag=networking&order=newest"],
168
+ # ["fragment", "top"]]
154
169
#
155
170
def self . split ( uri )
156
171
RFC3986_PARSER . split ( uri )
157
172
end
158
173
174
+ # Returns a new \URI object constructed from the given string +uri+:
159
175
#
160
- # == Synopsis
161
- #
162
- # URI::parse(uri_str)
163
- #
164
- # == Args
165
- #
166
- # +uri_str+::
167
- # String with URI.
168
- #
169
- # == Description
170
- #
171
- # Creates one of the URI's subclasses instance from the string.
172
- #
173
- # == Raises
174
- #
175
- # URI::InvalidURIError::
176
- # Raised if URI given is not a correct one.
177
- #
178
- # == Usage
179
- #
180
- # require 'uri'
181
- #
182
- # uri = URI.parse("http://www.ruby-lang.org/")
183
- # # => #<URI::HTTP http://www.ruby-lang.org/>
184
- # uri.scheme
185
- # # => "http"
186
- # uri.host
187
- # # => "www.ruby-lang.org"
176
+ # URI.parse('https://[email protected] :123/forum/questions/?tag=networking&order=newest#top')
177
+ # # => #<URI::HTTPS https://[email protected] :123/forum/questions/?tag=networking&order=newest#top>
178
+ # URI.parse('http://[email protected] :123/forum/questions/?tag=networking&order=newest#top')
179
+ # # => #<URI::HTTP http://[email protected] :123/forum/questions/?tag=networking&order=newest#top>
188
180
#
189
- # It's recommended to first ::escape the provided +uri_str+ if there are any
190
- # invalid URI characters.
181
+ # It's recommended to first ::escape string +uri+
182
+ # if it may contain invalid URI characters.
191
183
#
192
184
def self . parse ( uri )
193
185
RFC3986_PARSER . parse ( uri )
0 commit comments