Class: Nimbu::Request::Arguments
- Inherits:
-
Object
- Object
- Nimbu::Request::Arguments
- 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
Constants included from Validations::Token
Validations::Token::TOKEN_REQUIRED, Validations::Token::TOKEN_REQUIRED_REGEXP
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
The request api.
-
#params ⇒ Object
readonly
Parameters passed to request.
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
Instance Method Summary collapse
-
#assert_required(required) ⇒ Object
Check if required keys are present inside parameters hash.
-
#assert_values(values, key = nil) ⇒ Object
Check if parameters match expected values.
-
#initialize(api, options = {}) ⇒ Arguments
constructor
Takes api, filters and required arguments.
-
#parse(*args, &block) ⇒ Object
Parse arguments to allow for flexible api calls.
-
#sift(keys, key = nil, options = {}) ⇒ Object
Remove unkown keys from parameters hash.
Methods included from Validations::Required
Methods included from Validations::Token
Methods included from Validations::Format
Methods included from Validations::Presence
Methods included from ParameterFilter
Methods included from Normalizer
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, ={}) normalize! @api = api @required = .fetch('required', []).map(&:to_s) @optional = .fetch('optional', []).map(&:to_s) end |
Instance Attribute Details
#api ⇒ Object (readonly)
The request api
25 26 27 |
# File 'lib/nimbu-api/request/arguments.rb', line 25 def api @api end |
#params ⇒ Object (readonly)
Parameters passed to request
19 20 21 |
# File 'lib/nimbu-api/request/arguments.rb', line 19 def params @params end |
#remaining ⇒ Object (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) = args. normalize! if !args.size.zero? parse_arguments *args else # Arguments are inside the parameters hash end @params = @remaining = extract_remaining(args) extract_pagination() extract_content_locale() 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, ={}) filter! keys, (key.nil? ? params : params[key]), if keys.any? self end |