Module: Resto::Request::Uri

Included in:
Base
Defined in:
lib/resto/request/uri.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#schemeObject (readonly)

Returns the value of attribute scheme.



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

def scheme
  @scheme
end

Instance Method Details

#composed_pathObject



17
18
19
20
21
22
23
24
# File 'lib/resto/request/uri.rb', line 17

def composed_path
  path    = ["/#{@path}", @append_path].compact.join('/').gsub('//', "/")
  path    = "#{path}.#{current_formatter.extension}" if @add_extension
  params = hash_to_params
  return path unless (@query or params)

  [path, [@query, params].compact.join('&')].join('?')
end

#hash_to_paramsObject

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
# File 'lib/resto/request/uri.rb', line 40

def hash_to_params
  return nil unless @params
  raise ArgumentError unless @params.is_a?(Hash)

  @params.sort.map do |a|
    "#{CGI.escape(a.fetch(0).to_s)}=#{CGI.escape(a.fetch(1).to_s)}"
  end.join('&')
end

#normalize_uri(url) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/resto/request/uri.rb', line 27

def normalize_uri(url)
  @uri = URI.parse(url.match(/^https?:/) ? url : "http://#{url}")
  @scheme ||= @uri.scheme
  @host   ||= @uri.host
  @port   ||= @uri.port
  @path   ||= @uri.path
  @query  ||= @uri.query
end

#parse_url(url) ⇒ Object



36
37
38
# File 'lib/resto/request/uri.rb', line 36

def parse_url(url)
  normalize_uri(url)
end

#read_hostObject



8
9
10
11
12
13
# File 'lib/resto/request/uri.rb', line 8

def read_host
  return nil unless @host

  normalize_uri(@host)
  @uri.host
end

#read_portObject



15
# File 'lib/resto/request/uri.rb', line 15

def read_port; @port ||= 80; end