Skip to content

Commit 02dfc79

Browse files
[DOC] Common methods rdoc (#49)
1 parent 2bfd848 commit 02dfc79

File tree

1 file changed

+58
-66
lines changed

1 file changed

+58
-66
lines changed

lib/uri/common.rb

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,32 @@ module Schemes
6868
end
6969
private_constant :Schemes
7070

71+
# Registers the given +klass+ as the class to be instantiated
72+
# when parsing a \URI with the given +scheme+:
7173
#
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
7576
#
77+
# Note that after calling String#upcase on +scheme+, it must be a valid
78+
# constant name.
7679
def self.register_scheme(scheme, klass)
7780
Schemes.const_set(scheme.to_s.upcase, klass)
7881
end
7982

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.
8197
def self.scheme_list
8298
Schemes.constants.map { |name|
8399
[name.to_s.upcase, Schemes.const_get(name)]
@@ -88,9 +104,21 @@ def self.scheme_list
88104
private_constant :INITIAL_SCHEMES
89105
Ractor.make_shareable(INITIAL_SCHEMES) if defined?(Ractor)
90106

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.
91114
#
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>
94122
#
95123
def self.for(scheme, *arguments, default: Generic)
96124
const_name = scheme.to_s.upcase
@@ -121,73 +149,37 @@ class InvalidComponentError < Error; end
121149
#
122150
class BadURIError < Error; end
123151

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"]]
154169
#
155170
def self.split(uri)
156171
RFC3986_PARSER.split(uri)
157172
end
158173

174+
# Returns a new \URI object constructed from the given string +uri+:
159175
#
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>
188180
#
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.
191183
#
192184
def self.parse(uri)
193185
RFC3986_PARSER.parse(uri)

0 commit comments

Comments
 (0)