Class: HeartlandRetail::Client::URI
- Inherits:
-
Object
- Object
- HeartlandRetail::Client::URI
- Defined in:
- lib/heartland/client/uri.rb
Overview
A wrapper around URI
Class Method Summary collapse
-
.parse(value) ⇒ URI
Returns a URI object based on the parsed string.
Instance Method Summary collapse
-
#==(other_uri) ⇒ boolean
Checks if supplied URI matches current URI.
-
#dup ⇒ URI
Clones the URI object.
-
#initialize(uri) ⇒ URI
constructor
Creates a new URI object from an Addressable::URI.
-
#merge_query_values!(values) ⇒ Object
Merges the given hash of query string parameters and values with the URI’s existing query string parameters (if any).
-
#query_values ⇒ hash
Returns a hash of query string parameters and values.
-
#query_values=(values) ⇒ Object
Overwrites the query using the supplied query values.
-
#subpath(subpath) ⇒ URI
Returns a new URI with the given subpath appended to it.
Constructor Details
#initialize(uri) ⇒ URI
Creates a new URI object from an Addressable::URI
21 22 23 |
# File 'lib/heartland/client/uri.rb', line 21 def initialize(uri) @uri = uri end |
Class Method Details
.parse(value) ⇒ URI
Returns a URI object based on the parsed string.
12 13 14 15 |
# File 'lib/heartland/client/uri.rb', line 12 def self.parse(value) return value.dup if value.is_a?(self) new(::URI.parse(value.to_s)) end |
Instance Method Details
#==(other_uri) ⇒ boolean
Checks if supplied URI matches current URI
58 59 60 61 |
# File 'lib/heartland/client/uri.rb', line 58 def ==(other_uri) return false unless other_uri.is_a?(self.class) uri == other_uri.__send__(:uri) end |
#dup ⇒ URI
Clones the URI object
29 30 31 |
# File 'lib/heartland/client/uri.rb', line 29 def dup self.class.new(@uri.dup) end |
#merge_query_values!(values) ⇒ Object
Merges the given hash of query string parameters and values with the URI’s existing query string parameters (if any).
49 50 51 52 |
# File 'lib/heartland/client/uri.rb', line 49 def merge_query_values!(values) old_query_values = self.query_values || {} self.query_values = old_query_values.merge(normalize_query_hash(values)) end |
#query_values ⇒ hash
Returns a hash of query string parameters and values
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/heartland/client/uri.rb', line 73 def query_values return nil if query.nil? ::URI.decode_www_form(query).each_with_object({}) do |(k, v), hash| if k.end_with?('[]') k.gsub!(/\[\]$/, '') hash[k] = Array(hash[k]) + [v] else hash[k] = v end end end |
#query_values=(values) ⇒ Object
Overwrites the query using the supplied query values
65 66 67 |
# File 'lib/heartland/client/uri.rb', line 65 def query_values=(values) self.query = ::URI.encode_www_form(normalize_query_hash(values).sort) end |
#subpath(subpath) ⇒ URI
Returns a new URI with the given subpath appended to it. Ensures a single forward slash between the URI’s path and the given subpath.
38 39 40 41 42 43 44 |
# File 'lib/heartland/client/uri.rb', line 38 def subpath(subpath) uri = dup uri.path = "#{path}/" unless path.end_with?('/') escaped_subpath = ::URI::Parser.new.escape(subpath.to_s.gsub(/^\//, '')) uri.path = uri.path + escaped_subpath uri end |