Class: HTTP::URI

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/http/uri.rb

Constant Summary collapse

HTTP_SCHEME =
"http"
HTTPS_SCHEME =
"https"
NORMALIZER =
lambda do |uri|
  uri = HTTP::URI.parse uri

  HTTP::URI.new(
    :scheme    => uri.normalized_scheme,
    :authority => uri.normalized_authority,
    :path      => uri.normalized_path,
    :query     => uri.query,
    :fragment  => uri.normalized_fragment
  )
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options_or_uri = {}) ⇒ HTTP::URI

Creates an HTTP::URI instance from the given options

Parameters:

  • options_or_uri (Hash, Addressable::URI) (defaults to: {})

Options Hash (options_or_uri):

  • :scheme (String, #to_str)

    URI scheme

  • :user (String, #to_str)

    for basic authentication

  • :password (String, #to_str)

    for basic authentication

  • :host (String, #to_str)

    name component

  • :port (String, #to_str)

    network port to connect to

  • :path (String, #to_str)

    component to request

  • :query (String, #to_str)

    component distinct from path

  • :fragment (String, #to_str)

    component at the end of the URI



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/http/uri.rb', line 88

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

Instance Attribute Details

#hostString

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

Returns:

  • (String)

    The host of the URI



26
27
28
# File 'lib/http/uri.rb', line 26

def host
  @host
end

#normalized_hostString (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.

Returns:

  • (String)

    The normalized host of the URI



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

Parameters:

  • form_values (#to_hash, #to_ary)

    to encode

  • sort (TrueClass, FalseClass) (defaults to: false)

    should key/value pairs be sorted first?

Returns:

  • (String)

    encoded value



70
71
72
# File 'lib/http/uri.rb', line 70

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

Parameters:

Returns:



58
59
60
61
62
# File 'lib/http/uri.rb', line 58

def self.parse(uri)
  return uri if uri.is_a?(self)

  new(Addressable::URI.parse(uri))
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

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

Parameters:

  • other (Object)

    URI to compare this one with

Returns:

  • (TrueClass, FalseClass)

    are the URIs equivalent (after normalization)?



107
108
109
# File 'lib/http/uri.rb', line 107

def ==(other)
  other.is_a?(URI) && normalize.to_s == other.normalize.to_s
end

#dupObject

Returns duplicated URI.

Returns:

  • (Object)

    duplicated URI



158
159
160
# File 'lib/http/uri.rb', line 158

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

Parameters:

  • other (Object)

    URI to compare this one with

Returns:

  • (TrueClass, FalseClass)

    are the URIs equivalent?



116
117
118
# File 'lib/http/uri.rb', line 116

def eql?(other)
  other.is_a?(URI) && to_s == other.to_s
end

#hashInteger

Hash value based off the normalized form of a URI

Returns:

  • (Integer)

    A hash of the URI



123
124
125
# File 'lib/http/uri.rb', line 123

def hash
  @hash ||= to_s.hash * -1
end

#http?True, False

Returns:

  • (True)

    if URI is HTTP

  • (False)

    otherwise



147
148
149
# File 'lib/http/uri.rb', line 147

def http?
  HTTP_SCHEME == scheme
end

#https?True, False

Returns:

  • (True)

    if URI is HTTPS

  • (False)

    otherwise



153
154
155
# File 'lib/http/uri.rb', line 153

def https?
  HTTPS_SCHEME == scheme
end

#inspectString

Returns human-readable representation of URI.

Returns:

  • (String)

    human-readable representation of URI



171
172
173
# File 'lib/http/uri.rb', line 171

def inspect
  format("#<%s:0x%014x URI:%s>", self.class.name, object_id << 1, to_s)
end

#portInteger

Port number, either as specified or the default if unspecified

Returns:

  • (Integer)

    port number



141
142
143
# File 'lib/http/uri.rb', line 141

def port
  @uri.port || @uri.default_port
end

#to_sString Also known as: to_str

Convert an HTTP::URI to a String

Returns:

  • (String)

    URI serialized as a String



165
166
167
# File 'lib/http/uri.rb', line 165

def to_s
  @uri.to_s
end