class HTTP::URI

Constants

HTTPS_SCHEME

@private

HTTP_SCHEME

@private

NORMALIZER

@private

PERCENT_ENCODE

@private

Attributes

host[R]

Host, either a domain name or IP address. If the host is an IPv6 address, it will be returned without brackets surrounding it.

@return [String] The host of the URI

normalized_host[R]

Normalized host, either a domain name or IP address. If the host is an IPv6 address, it will be returned without brackets surrounding it.

@return [String] The normalized host of the URI

Public Class Methods

form_encode(form_values, sort = false) click to toggle source

Encodes key/value pairs as application/x-www-form-urlencoded

@param [#to_hash, to_ary] form_values to encode @param [TrueClass, FalseClass] sort should key/value pairs be sorted first?

@return [String] encoded value

# File lib/http/uri.rb, line 73
def self.form_encode(form_values, sort = false)
  Addressable::URI.form_encode(form_values, sort)
end
new(options_or_uri = {}) click to toggle source

Creates an HTTP::URI instance from the given options

@param [Hash, Addressable::URI] options_or_uri

@option options_or_uri [String, to_str] :scheme URI scheme @option options_or_uri [String, to_str] :user for basic authentication @option options_or_uri [String, to_str] :password for basic authentication @option options_or_uri [String, to_str] :host name component @option options_or_uri [String, to_str] :port network port to connect to @option options_or_uri [String, to_str] :path component to request @option options_or_uri [String, to_str] :query component distinct from path @option options_or_uri [String, to_str] :fragment component at the end of the URI

@return [HTTP::URI] new URI instance

# File lib/http/uri.rb, line 104
def initialize(options_or_uri = {})
  case options_or_uri
  when Hash
    @uri = Addressable::URI.new(options_or_uri)
  when Addressable::URI
    @uri = options_or_uri
  else
    raise TypeError, "expected Hash for options, got #{options_or_uri.class}"
  end

  @host = process_ipv6_brackets(@uri.host)
  @normalized_host = process_ipv6_brackets(@uri.normalized_host)
end
parse(uri) click to toggle source

Parse the given URI string, returning an HTTP::URI object

@param [HTTP::URI, String, to_str] uri to parse

@return [HTTP::URI] new URI instance

# 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) click to toggle source

Percent-encode all characters matching a regular expression.

@param [String] string raw string

@return [String] encoded value

@private

# 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

Public Instance Methods

==(other) click to toggle source

Are these URI objects equal? Normalizes both URIs prior to comparison

@param [Object] other URI to compare this one with

@return [TrueClass, FalseClass] are the URIs equivalent (after normalization)?

# File lib/http/uri.rb, line 123
def ==(other)
  other.is_a?(URI) && normalize.to_s == other.normalize.to_s
end
dup() click to toggle source

@return [Object] duplicated URI

# File lib/http/uri.rb, line 174
def dup
  self.class.new @uri.dup
end
eql?(other) click to toggle source

Are these URI objects equal? Does NOT normalizes both URIs prior to comparison

@param [Object] other URI to compare this one with

@return [TrueClass, FalseClass] are the URIs equivalent?

# File lib/http/uri.rb, line 132
def eql?(other)
  other.is_a?(URI) && to_s == other.to_s
end
hash() click to toggle source

Hash value based off the normalized form of a URI

@return [Integer] A hash of the URI

# File lib/http/uri.rb, line 139
def hash
  @hash ||= to_s.hash * -1
end
host=(new_host) click to toggle source

Sets the host component for the URI.

@param [String, to_str] new_host The new host component. @return [void]

# File lib/http/uri.rb, line 147
def host=(new_host)
  @uri.host = process_ipv6_brackets(new_host, :brackets => true)

  @host = process_ipv6_brackets(@uri.host)
  @normalized_host = process_ipv6_brackets(@uri.normalized_host)
end
http?() click to toggle source

@return [True] if URI is HTTP @return [False] otherwise

# File lib/http/uri.rb, line 163
def http?
  HTTP_SCHEME == scheme
end
https?() click to toggle source

@return [True] if URI is HTTPS @return [False] otherwise

# File lib/http/uri.rb, line 169
def https?
  HTTPS_SCHEME == scheme
end
inspect() click to toggle source

@return [String] human-readable representation of URI

# 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() click to toggle source

Port number, either as specified or the default if unspecified

@return [Integer] port number

# File lib/http/uri.rb, line 157
def port
  @uri.port || @uri.default_port
end
to_s() click to toggle source

Convert an HTTP::URI to a String

@return [String] URI serialized as a String

# File lib/http/uri.rb, line 181
def to_s
  @uri.to_s
end
Also aliased as: to_str
to_str()
Alias for: to_s

Private Instance Methods

process_ipv6_brackets(raw_host, brackets: false) click to toggle source

Process a URI host, adding or removing surrounding brackets if the host is an IPv6 address.

@param [Boolean] brackets When true, brackets will be added to IPv6 addresses if missing. When

false, they will be removed if present.

@return [String] Host with IPv6 address brackets added or removed

# File lib/http/uri.rb, line 199
def process_ipv6_brackets(raw_host, brackets: false)
  ip = IPAddr.new(raw_host)

  if ip.ipv6?
    brackets ? "[#{ip}]" : ip.to_s
  else
    raw_host
  end
rescue IPAddr::Error
  raw_host
end