Sha256: 5bcfe0bbc4a1d3eadf1698731540a91dfc4a62dafe11ba28f76dc1e718fcaab5

Contents?: true

Size: 767 Bytes

Versions: 1

Compression:

Stored size: 767 Bytes

Contents

module Jersey::Helpers
  module AutoJsonParams
    # 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
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jersey-0.2.0 lib/jersey/helpers/auto_json_params.rb