Module: Jersey::Helpers::AutoJsonParams

Defined in:
lib/jersey/helpers/auto_json_params.rb

Instance Method Summary collapse

Instance Method Details

#paramsObject

Merges sinatra @params Hash with json data parsed by a rack middleware that has set ‘rack.json` on the rack env.

If the parsed data is an array, merges by using the array index as a hash key.

Json data gets precendence in naming collisions



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jersey/helpers/auto_json_params.rb', line 11

def params
  # we have parsed json!
  if @env['rack.json']
    json = @env['rack.json']
    if json.is_a?(Hash)
      # merge with params
      super.merge(json)
    else
      # covert array to hash by index
      zipped = json.each_with_index
      zipped = zipped.to_a.map(&:reverse)
      super.merge(Hash[zipped])
    end
  else
    super
  end
end