Class: Aitch::URI

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

Instance Method Summary collapse

Constructor Details

#initialize(url, data = {}, request_has_body = false) ⇒ URI

Returns a new instance of URI.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/aitch/uri.rb', line 8

def initialize(url, data = {}, request_has_body = false)
  @url = url
  @data = data
  @request_has_body = request_has_body

  begin
    @uri = ::URI.parse(url)
  rescue ::URI::InvalidURIError => error
    raise InvalidURIError, error
  end
end

Instance Method Details

#fragmentObject



32
33
34
# File 'lib/aitch/uri.rb', line 32

def fragment
  "##{@uri.fragment}" if @uri.fragment
end

#pathObject



24
25
26
# File 'lib/aitch/uri.rb', line 24

def path
  File.join("/", @uri.path)
end

#queryObject



36
37
38
39
40
41
42
# File 'lib/aitch/uri.rb', line 36

def query
  query = [@uri.query]
  query << ::URI.encode_www_form(@data.to_a) if !request_has_body? && @data.respond_to?(:to_a)
  query = query.compact.reject(&:empty?).join("&")

  "?#{query}" unless query == ""
end

#request_has_body?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/aitch/uri.rb', line 20

def request_has_body?
  @request_has_body
end

#request_uriObject



28
29
30
# File 'lib/aitch/uri.rb', line 28

def request_uri
  [path, query, fragment].compact.join("")
end