Class: Hyde::Util::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/hyde/util/query.rb

Overview

Query string parser

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Query

Returns a new instance of Query.

Parameters:

  • query (String)


10
11
12
# File 'lib/hyde/util/query.rb', line 10

def initialize(query)
  @query = query
end

Instance Method Details

#parseHash

Better(tm) query parser with 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)


30
31
32
# File 'lib/hyde/util/query.rb', line 30

def parse
  construct_deep_hash(URI.decode_www_form(@query, Encoding::UTF_8))
end

#parse_shallowHash

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

Returns:

  • (Hash)


16
17
18
19
20
# File 'lib/hyde/util/query.rb', line 16

def parse_shallow
  URI.decode_www_form(@query, Encoding::UTF_8)
     .sort_by { |array| array[0] }
     .to_h
end