Class: HTTP::URI

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

Constant Summary collapse

HTTP_SCHEME =
"http".freeze
HTTPS_SCHEME =
"https".freeze

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



60
61
62
63
64
65
66
67
68
# File 'lib/http/uri.rb', line 60

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
end

Class Method Details

.form_encode(form_values, sort = false) ⇒ String

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



44
45
46
# File 'lib/http/uri.rb', line 44

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



32
33
34
35
36
# File 'lib/http/uri.rb', line 32

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



75
76
77
# File 'lib/http/uri.rb', line 75

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

#eql?(other) ⇒ TrueClass, FalseClass

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



84
85
86
# File 'lib/http/uri.rb', line 84

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

#hashInteger

Hash value based off the normalized form of a URI



91
92
93
# File 'lib/http/uri.rb', line 91

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

#http?True, False



104
105
106
# File 'lib/http/uri.rb', line 104

def http?
  HTTP_SCHEME == scheme
end

#https?True, False



110
111
112
# File 'lib/http/uri.rb', line 110

def https?
  HTTPS_SCHEME == scheme
end

#inspectString



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

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



98
99
100
# File 'lib/http/uri.rb', line 98

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

#to_sString Also known as: to_str

Convert an HTTP::URI to a String



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

def to_s
  @uri.to_s
end