Class: Landline::Util::Query
- Inherits:
-
Object
- Object
- Landline::Util::Query
- Includes:
- ParserSorting
- Defined in:
- lib/landline/util/query.rb
Overview
Query string parser
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#[](key) ⇒ String, Array
Get key from query.
-
#initialize(query) ⇒ Query
constructor
A new instance of Query.
-
#parse ⇒ Hash
Better(tm) query parser.
-
#parse_shallow ⇒ Hash
Shallow query parser (does not do PHP-like array keys).
Constructor Details
#initialize(query) ⇒ Query
Returns a new instance of Query.
14 15 16 |
# File 'lib/landline/util/query.rb', line 14 def initialize(query) @query = query end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
11 12 13 |
# File 'lib/landline/util/query.rb', line 11 def query @query end |
Instance Method Details
#[](key) ⇒ String, Array
Get key from query.
41 42 43 |
# File 'lib/landline/util/query.rb', line 41 def [](key) (@cache ||= parse)[key] end |
#parse ⇒ Hash
Better(tm) query parser. Returns a hash with arrays. Key semantics:
-
‘key=value` creates a key value pair
-
‘key[]=value` appends `value` to an array named `key`
-
‘key=value` sets `value` at `index` of array named `key`
34 35 36 |
# File 'lib/landline/util/query.rb', line 34 def parse construct_deep_hash(URI.decode_www_form(@query)) end |
#parse_shallow ⇒ Hash
Shallow query parser (does not do PHP-like array keys)
20 21 22 23 24 |
# File 'lib/landline/util/query.rb', line 20 def parse_shallow URI.decode_www_form(@query) .sort_by { |array| array[0] } .to_h end |