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.


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

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 => e
    raise InvalidURIError, e
  end
end

Instance Method Details

#fragmentObject


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

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

#pathObject


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

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

#queryObject


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

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

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

#request_has_body?Boolean

Returns:

  • (Boolean)

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

def request_has_body?
  @request_has_body
end

#request_uriObject


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

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