Class: Github::API::Arguments
- Inherits:
-
Object
- Object
- Github::API::Arguments
- Includes:
- Normalizer, ParameterFilter, Validations
- Defined in:
- lib/github_api2/api/arguments.rb
Overview
A class responsible for handling 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
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/github_api2/api/arguments.rb', line 38 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
79 80 81 82 83 84 85 |
# File 'lib/github_api2/api/arguments.rb', line 79 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
25 26 27 |
# File 'lib/github_api2/api/arguments.rb', line 25 def api @api end |
#params ⇒ Object (readonly)
Parameters passed to request
19 20 21 |
# File 'lib/github_api2/api/arguments.rb', line 19 def params @params end |
#remaining ⇒ Object (readonly)
The remaining unparsed arguments
22 23 24 |
# File 'lib/github_api2/api/arguments.rb', line 22 def remaining @remaining end |
Instance Method Details
#[](property) ⇒ Object
Hash like access to request arguments
71 72 73 |
# File 'lib/github_api2/api/arguments.rb', line 71 def [](property) @assigns[property.to_s] end |
#[]=(property, value) ⇒ Object
75 76 77 |
# File 'lib/github_api2/api/arguments.rb', line 75 def []=(property, value) @assigns[property.to_s] = value end |
#assert_required(*required) ⇒ Object
Check if required keys are present inside parameters hash.
126 127 128 129 |
# File 'lib/github_api2/api/arguments.rb', line 126 def assert_required(*required) assert_required_keys(required, params) self end |
#assert_values(values, key = nil) ⇒ Object
Check if parameters match expected values.
134 135 136 137 |
# File 'lib/github_api2/api/arguments.rb', line 134 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)
62 63 |
# File 'lib/github_api2/api/arguments.rb', line 62 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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/github_api2/api/arguments.rb', line 96 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
118 119 120 121 |
# File 'lib/github_api2/api/arguments.rb', line 118 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)
52 53 54 55 56 |
# File 'lib/github_api2/api/arguments.rb', line 52 def require(*attrs, &block) attrs_clone = attrs.clone @required = Array(attrs_clone) self end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
87 88 89 |
# File 'lib/github_api2/api/arguments.rb', line 87 def respond_to_missing?(method_name, include_private = false) @assigns.key?(method_name) || super end |