Class: Landline::Util::Query

Inherits:
Object
  • Object
show all
Includes:
ParserSorting
Defined in:
lib/landline/util/query.rb

Overview

Query string parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Query

Returns a new instance of Query.

Parameters:

  • query (String)


14
15
16
# File 'lib/landline/util/query.rb', line 14

def initialize(query)
  @query = query
end

Instance Attribute Details

#queryObject (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.

Parameters:

  • key (String)

Returns:

  • (String, Array)


41
42
43
# File 'lib/landline/util/query.rb', line 41

def [](key)
  (@cache ||= parse)[key]
end

#parseHash

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`

Returns:

  • (Hash)


34
35
36
# File 'lib/landline/util/query.rb', line 34

def parse
  construct_deep_hash(URI.decode_www_form(@query))
end

#parse_shallowHash

Shallow query parser (does not do PHP-like array keys)

Returns:

  • (Hash)


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