Module: V1::ApiController::Parameters

Included in:
V1::ApiController
Defined in:
app/controllers/v1/api_controller/parameters.rb

Overview

Parameters request module

Constant Summary collapse

PARAMS =
{
  "page":  'numeric?',
  "limit": 'numeric?'
}.freeze

Instance Method Summary collapse

Instance Method Details

#check_paramsboolean

Check the params of the GLOBAL VAR *PARAMS*.

Returns:

  • (boolean)

    if checks are ok or not


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/v1/api_controller/parameters.rb', line 16

def check_params
  # For each key,value in GLOBAL VAR **PARAMS**.
  PARAMS.each do |param, method|
    # Next if there isn't param in the request of client
    next unless params[param]
    # Check the *param* with *method*
    unless send(method, params[param])
      @error_object = "Param #{param} is wrong, #{method} failed"
      return false
    end
  end
  true
end

#expand_resources?boolean

Check if there is a param expand=resources in request

Returns:

  • (boolean)

    if there is the param expand=resources


33
34
35
# File 'app/controllers/v1/api_controller/parameters.rb', line 33

def expand_resources?
  params['expand'] == 'resources'
end

#numeric?(obj) ⇒ boolean

Check if the obj is a numeric value

Returns:

  • (boolean)

    if is numeric


40
41
42
# File 'app/controllers/v1/api_controller/parameters.rb', line 40

def numeric?(obj)
  obj.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil? ? false : true
end

#true?(obj) ⇒ Boolean

Returns:

  • (Boolean)

44
45
46
# File 'app/controllers/v1/api_controller/parameters.rb', line 44

def true?(obj)
  obj.to_s == "true"
end