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.
Instance Method Summary collapse
-
#body_params ⇒ Hash
The parameters from the JSON body if it is sent.
-
#params(*subset) ⇒ Hash
Returns the parameters depending on whether the request has a body or not.
Instance Method Details
#body_params ⇒ Hash
The parameters from the JSON body if it is sent.
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.
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 |