Class: UniformResourceIdentifier::Query
- Inherits:
-
Object
- Object
- UniformResourceIdentifier::Query
- Extended by:
- Parsable
- Defined in:
- lib/uniform_resource_identifier/query.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #blank? ⇒ Boolean
-
#initialize(query = nil) ⇒ Query
constructor
A new instance of Query.
- #to_h ⇒ Object
- #to_s ⇒ Object
Methods included from Parsable
Constructor Details
#initialize(query = nil) ⇒ Query
Returns a new instance of Query.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/uniform_resource_identifier/query.rb', line 10 def initialize(query=nil) if query.respond_to?(:to_str) query = "#{query =~ /^\?/ ? nil : '?'}#{query.to_str}" # Prepend a question mark if needed @query = Addressable::URI.parse(query).query_values elsif query.respond_to?(:to_hash) @query = query.to_hash else raise(TypeError, "query must either be a String or a Hash") unless query.nil? end end |
Instance Method Details
#[](key) ⇒ Object
21 22 23 |
# File 'lib/uniform_resource_identifier/query.rb', line 21 def [](key) @query[key] end |
#[]=(key, value) ⇒ Object
25 26 27 |
# File 'lib/uniform_resource_identifier/query.rb', line 25 def []=(key, value) @query[key] = value end |
#blank? ⇒ Boolean
37 38 39 |
# File 'lib/uniform_resource_identifier/query.rb', line 37 def blank? @query.blank? end |
#to_h ⇒ Object
33 34 35 |
# File 'lib/uniform_resource_identifier/query.rb', line 33 def to_h @query end |
#to_s ⇒ Object
29 30 31 |
# File 'lib/uniform_resource_identifier/query.rb', line 29 def to_s @query.blank? ? "" : @query.to_query end |