Module: Resto::Request::Uri
- Included in:
- Base
- Defined in:
- lib/resto/request/uri.rb
Instance Attribute Summary collapse
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
Instance Method Summary collapse
- #composed_path ⇒ Object
- #hash_to_params ⇒ Object
- #normalize_uri(url) ⇒ Object
- #parse_url(url) ⇒ Object
- #read_host ⇒ Object
- #read_port ⇒ Object
Instance Attribute Details
#scheme ⇒ Object (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_path ⇒ Object
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_params ⇒ Object
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_host ⇒ Object
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_port ⇒ Object
15 |
# File 'lib/resto/request/uri.rb', line 15 def read_port; @port ||= 80; end |