Class: HTTP::URI
Constant Summary collapse
- HTTP_SCHEME =
"http"
- HTTPS_SCHEME =
"https"
- PERCENT_ENCODE =
/[^\x21-\x7E]+/
- NORMALIZER =
lambda do |uri| uri = HTTP::URI.parse uri HTTP::URI.new( scheme: uri.normalized_scheme, authority: uri., path: uri.path.empty? ? "/" : percent_encode(Addressable::URI.normalize_path(uri.path)), query: percent_encode(uri.query), fragment: uri.normalized_fragment ) end
Instance Attribute Summary collapse
-
#host ⇒ String
Host, either a domain name or IP address.
-
#normalized_host ⇒ String
readonly
Normalized host, either a domain name or IP address.
Class Method Summary collapse
-
.form_encode(form_values, sort = false) ⇒ String
Encodes key/value pairs as application/x-www-form-urlencoded.
-
.parse(uri) ⇒ HTTP::URI
Parse the given URI string, returning an HTTP::URI object.
-
.percent_encode(string) ⇒ String
Percent-encode all characters matching a regular expression.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
Are these URI objects equal? Normalizes both URIs prior to comparison.
-
#dup ⇒ Object
Duplicated URI.
-
#eql?(other) ⇒ TrueClass, FalseClass
Are these URI objects equal? Does NOT normalizes both URIs prior to comparison.
-
#hash ⇒ Integer
Hash value based off the normalized form of a URI.
- #http? ⇒ True, False
- #https? ⇒ True, False
-
#initialize(options_or_uri = {}) ⇒ HTTP::URI
constructor
Creates an HTTP::URI instance from the given options.
-
#inspect ⇒ String
Human-readable representation of URI.
-
#port ⇒ Integer
Port number, either as specified or the default if unspecified.
-
#to_s ⇒ String
(also: #to_str)
Convert an HTTP::URI to a String.
Constructor Details
#initialize(options_or_uri = {}) ⇒ HTTP::URI
Creates an HTTP::URI instance from the given options
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/http/uri.rb', line 104 def initialize( = {}) case when Hash @uri = Addressable::URI.new() when Addressable::URI @uri = else raise TypeError, "expected Hash for options, got #{.class}" end @host = process_ipv6_brackets(@uri.host) @normalized_host = process_ipv6_brackets(@uri.normalized_host) end |
Instance Attribute Details
#host ⇒ String
Host, either a domain name or IP address. If the host is an IPv6 address, it will be returned without brackets surrounding it.
26 27 28 |
# File 'lib/http/uri.rb', line 26 def host @host end |
#normalized_host ⇒ String (readonly)
Normalized host, either a domain name or IP address. If the host is an IPv6 address, it will be returned without brackets surrounding it.
32 33 34 |
# File 'lib/http/uri.rb', line 32 def normalized_host @normalized_host end |
Class Method Details
.form_encode(form_values, sort = false) ⇒ String
Encodes key/value pairs as application/x-www-form-urlencoded
73 74 75 |
# File 'lib/http/uri.rb', line 73 def self.form_encode(form_values, sort = false) Addressable::URI.form_encode(form_values, sort) end |
.parse(uri) ⇒ HTTP::URI
Parse the given URI string, returning an HTTP::URI object
61 62 63 64 65 |
# File 'lib/http/uri.rb', line 61 def self.parse(uri) return uri if uri.is_a?(self) new(Addressable::URI.parse(uri)) end |
.percent_encode(string) ⇒ String
Percent-encode all characters matching a regular expression.
84 85 86 87 88 |
# File 'lib/http/uri.rb', line 84 def self.percent_encode(string) string&.gsub(PERCENT_ENCODE) do |substr| substr.encode(Encoding::UTF_8).bytes.map { |c| format("%%%02X", c) }.join end end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass
Are these URI objects equal? Normalizes both URIs prior to comparison
123 124 125 |
# File 'lib/http/uri.rb', line 123 def ==(other) other.is_a?(URI) && normalize.to_s == other.normalize.to_s end |
#dup ⇒ Object
Returns duplicated URI.
174 175 176 |
# File 'lib/http/uri.rb', line 174 def dup self.class.new @uri.dup end |
#eql?(other) ⇒ TrueClass, FalseClass
Are these URI objects equal? Does NOT normalizes both URIs prior to comparison
132 133 134 |
# File 'lib/http/uri.rb', line 132 def eql?(other) other.is_a?(URI) && to_s == other.to_s end |
#hash ⇒ Integer
Hash value based off the normalized form of a URI
139 140 141 |
# File 'lib/http/uri.rb', line 139 def hash @hash ||= to_s.hash * -1 end |
#http? ⇒ True, False
163 164 165 |
# File 'lib/http/uri.rb', line 163 def http? HTTP_SCHEME == scheme end |
#https? ⇒ True, False
169 170 171 |
# File 'lib/http/uri.rb', line 169 def https? HTTPS_SCHEME == scheme end |
#inspect ⇒ String
Returns human-readable representation of URI.
187 188 189 |
# File 'lib/http/uri.rb', line 187 def inspect format("#<%s:0x%014x URI:%s>", self.class.name, object_id << 1, to_s) end |
#port ⇒ Integer
Port number, either as specified or the default if unspecified
157 158 159 |
# File 'lib/http/uri.rb', line 157 def port @uri.port || @uri.default_port end |
#to_s ⇒ String Also known as: to_str
Convert an HTTP::URI to a String
181 182 183 |
# File 'lib/http/uri.rb', line 181 def to_s @uri.to_s end |