Class: RackOnLambda::Query

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

Overview

The ‘multiValueQueryStringParameters` object from the API Gateway event keeps all values in an array, regardless of the actual size of the array and regardless of the “intent” of the query string parameter.

In order to normalise this behaviour, we treat the query strings

`key=value1&key=value2`

and

`key[]=value1&key[]=value2`

the same. Both are to be serialised to the query string

`key[]=value1&key[]=value2`

However, the query strings

`key=value`

and

`key[]=value`

are not to be treated the same.

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Query

Returns a new instance of Query.



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

def initialize(data = {})
  @params = data.with_indifferent_access
end

Instance Method Details

#add(key, values) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/rack_on_lambda/query.rb', line 34

def add(key, values)
  if key.to_s.end_with?('[]')
    actual_key = key[0..-3]
    add_list(actual_key, values)
  else
    values.each { |value| add_item(key, value) }
  end
end

#to_hObject



43
44
45
# File 'lib/rack_on_lambda/query.rb', line 43

def to_h
  @params.dup
end

#to_sObject



47
48
49
# File 'lib/rack_on_lambda/query.rb', line 47

def to_s
  Rack::Utils.build_nested_query(to_h)
end