Module: Core::Helpers::Parameters

Included in:
Controllers::Base
Defined in:
lib/core/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 dict ionary.



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

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

#paramsHash

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
# File 'lib/core/helpers/parameters.rb', line 11

def params
  super.merge(body_params)
end

#sym_paramsObject



15
16
17
# File 'lib/core/helpers/parameters.rb', line 15

def sym_params
  params.map { |k, v| [k.to_sym, v] }.to_h
end