Class: Nimbu::Request::Arguments

Inherits:
Object
  • Object
show all
Includes:
Normalizer, ParameterFilter, Validations
Defined in:
lib/nimbu-api/request/arguments.rb

Overview

Request arguments handler

Constant Summary collapse

AUTO_PAGINATION =
'auto_pagination'.freeze
CONTENT_LOCALE =
'content_locale'.freeze

Constants included from Validations

Validations::VALID_API_KEYS

Constants included from Validations::Token

Validations::Token::TOKEN_REQUIRED, Validations::Token::TOKEN_REQUIRED_REGEXP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations::Required

#assert_required_keys

Methods included from Validations::Token

#validates_token_for

Methods included from Validations::Format

#assert_valid_values

Methods included from Validations::Presence

#assert_presence_of

Methods included from ParameterFilter

#filter!

Methods included from Normalizer

#normalize!

Constructor Details

#initialize(api, options = {}) ⇒ Arguments

Takes api, filters and required arguments

Parameters

:required - arguments that must be present before request is fired


42
43
44
45
46
47
# File 'lib/nimbu-api/request/arguments.rb', line 42

def initialize(api, options={})
  normalize! options
  @api      = api
  @required = options.fetch('required', []).map(&:to_s)
  @optional = options.fetch('optional', []).map(&:to_s)
end

Instance Attribute Details

#apiObject (readonly)

The request api



25
26
27
# File 'lib/nimbu-api/request/arguments.rb', line 25

def api
  @api
end

#paramsObject (readonly)

Parameters passed to request



19
20
21
# File 'lib/nimbu-api/request/arguments.rb', line 19

def params
  @params
end

#remainingObject (readonly)

Returns the value of attribute remaining.



21
22
23
# File 'lib/nimbu-api/request/arguments.rb', line 21

def remaining
  @remaining
end

Instance Method Details

#assert_required(required) ⇒ Object

Check if required keys are present inside parameters hash.



82
83
84
85
# File 'lib/nimbu-api/request/arguments.rb', line 82

def assert_required(required)
  assert_required_keys required, params
  self
end

#assert_values(values, key = nil) ⇒ Object

Check if parameters match expected values.



89
90
91
92
# File 'lib/nimbu-api/request/arguments.rb', line 89

def assert_values(values, key=nil)
  assert_valid_values values, (key.nil? ? params : params[key])
  self
end

#parse(*args, &block) ⇒ Object

Parse arguments to allow for flexible api calls. Arguments can be part of parameters hash or be simple string arguments.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nimbu-api/request/arguments.rb', line 52

def parse(*args, &block)
  options = args.extract_options!
  normalize! options

  if !args.size.zero?
    parse_arguments *args
  else
    # Arguments are inside the parameters hash
    parse_options options
  end
  @params = options
  @remaining = extract_remaining(args)
  extract_pagination(options)
  extract_content_locale(options)
  yield_or_eval(&block)
  self
end

#sift(keys, key = nil, options = {}) ⇒ Object

Remove unkown keys from parameters hash.

Parameters

:recursive - boolean that toggles whether nested filtering should be applied


75
76
77
78
# File 'lib/nimbu-api/request/arguments.rb', line 75

def sift(keys, key=nil, options={})
  filter! keys, (key.nil? ? params : params[key]), options if keys.any?
  self
end