Module: Virtuatable::Helpers::Parameters

Included in:
Controllers::Base
Defined in:
lib/virtuatable/helpers/parameters.rb

Overview

Helpers to correctly build the parameters hash, even from the JSON body.

Author:

Instance Method Summary collapse

Instance Method Details

#body_paramsHash

The parameters from the JSON body if it is sent.

Returns:

  • (Hash)

    the JSON body parsed as a dictionary.



21
22
23
24
25
26
# File 'lib/virtuatable/helpers/parameters.rb', line 21

def body_params
  request.body.rewind
  JSON.parse(request.body.read.to_s)
rescue JSON::ParserError
  {}
end

#params(*subset) ⇒ Hash

Returns the parameters depending on whether the request has a body or not. If it has a body, it parses it, otherwise it just returns the params.

Returns:

  • (Hash)

    the parameters sent with the request.



11
12
13
14
15
16
17
# File 'lib/virtuatable/helpers/parameters.rb', line 11

def params(*subset)
  all_params = super.merge(body_params)
  # If we don't want a subset of the parameters, return it all.
  return all_params if subset.empty?

  all_params.select { |key, _| subset.map(&:to_s).include? key }
end