Class: BitBucket::API::Arguments
- Inherits:
-
Object
- Object
- BitBucket::API::Arguments
- Includes:
- Normalizer, ParameterFilter, Validations
- Defined in:
- lib/bitbucket_rest_api/api/arguments.rb
Overview
A class responsible for handilng request arguments
Constant Summary collapse
- AUTO_PAGINATION =
'auto_pagination'.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
The remaining unparsed arguments.
Instance Method Summary collapse
-
#[](property) ⇒ Object
Hash like access to request arguments.
- #[]=(property, value) ⇒ Object
-
#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(options = {}, &block) ⇒ Arguments
constructor
Initialize an Arguments.
- #method_missing(method_name, *args, &block) ⇒ Object
-
#optional(*attrs, &block) ⇒ Object
Specify optional attribute(s).
-
#parse(*args, &block) ⇒ Object
Parse arguments to allow for flexible api calls.
-
#permit(keys, key = nil, options = {}) ⇒ Object
Remove unknown keys from parameters hash.
-
#require(*attrs, &block) ⇒ Object
(also: #required)
Specify required attribute(s).
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
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(options = {}, &block) ⇒ Arguments
Initialize an Arguments
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 33 def initialize( = {}, &block) normalize! @api = .fetch('api') @required = .fetch('required', []).map(&:to_s) @optional = .fetch('optional', []).map(&:to_s) @assigns = {} yield_or_eval(&block) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 74 def method_missing(method_name, *args, &block) if @assigns.key?(method_name.to_s) self[method_name] else super end end |
Instance Attribute Details
#api ⇒ Object (readonly)
The request api
20 21 22 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 20 def api @api end |
#params ⇒ Object (readonly)
Parameters passed to request
14 15 16 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 14 def params @params end |
#remaining ⇒ Object (readonly)
The remaining unparsed arguments
17 18 19 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 17 def remaining @remaining end |
Instance Method Details
#[](property) ⇒ Object
Hash like access to request arguments
66 67 68 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 66 def [](property) @assigns[property.to_s] end |
#[]=(property, value) ⇒ Object
70 71 72 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 70 def []=(property, value) @assigns[property.to_s] = value end |
#assert_required(required) ⇒ Object
Check if required keys are present inside parameters hash.
121 122 123 124 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 121 def assert_required(required) assert_required_keys required, params self end |
#assert_values(values, key = nil) ⇒ Object
Check if parameters match expected values.
129 130 131 132 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 129 def assert_values(values, key=nil) assert_valid_values values, (key.nil? ? params : params[key]) self end |
#optional(*attrs, &block) ⇒ Object
Specify optional attribute(s)
57 58 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 57 def optional(*attrs, &block) 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.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 91 def parse(*args, &block) = ParamsHash.new(args.) normalize! if args.size.zero? # Arguments are inside the parameters hash parse_hash() else parse_array(*args) end @params = @remaining = args[@required.size..-1] extract_pagination() yield_or_eval(&block) self end |
#permit(keys, key = nil, options = {}) ⇒ Object
Remove unknown keys from parameters hash.
Parameters
:recursive - boolean that toggles whether nested filtering should be applied
113 114 115 116 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 113 def permit(keys, key=nil, ={}) filter! keys, (key.nil? ? params : params[key]), if keys.any? self end |
#require(*attrs, &block) ⇒ Object Also known as: required
Specify required attribute(s)
47 48 49 50 51 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 47 def require(*attrs, &block) attrs_clone = attrs.clone @required = Array(attrs_clone) self end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
82 83 84 |
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 82 def respond_to_missing?(method_name, include_private = false) @assigns.key?(method_name) || super end |