class Github::API::Arguments
A class responsible for handilng request arguments
Constants
- AUTO_PAGINATION
Attributes
The request api
Parameters passed to request
The remaining unparsed arguments
Public Class Methods
Initialize an Arguments
@param [Hash] options
@option options [Array] :required
arguments that must be present before request is fired
@option options [Github::API] :api
the reference to the current api
@api public
# File lib/github_api/api/arguments.rb, line 32 def initialize(options = {}, &block) normalize! options @api = options.fetch('api') @required = options.fetch('required', []).map(&:to_s) @optional = options.fetch('optional', []).map(&:to_s) @assigns = {} yield_or_eval(&block) end
Public Instance Methods
Hash like access to request arguments
@param [String, Symbol] property
the property name
@api public
# File lib/github_api/api/arguments.rb, line 65 def [](property) @assigns[property.to_s] end
# File lib/github_api/api/arguments.rb, line 69 def []=(property, value) @assigns[property.to_s] = value end
Check if required keys are present inside parameters hash.
@api public
# File lib/github_api/api/arguments.rb, line 120 def assert_required(*required) assert_required_keys(required, params) self end
Check if parameters match expected values.
@api public
# File lib/github_api/api/arguments.rb, line 128 def assert_values(values, key=nil) assert_valid_values values, (key.nil? ? params : params[key]) self end
# File lib/github_api/api/arguments.rb, line 73 def method_missing(method_name, *args, &block) if @assigns.key?(method_name.to_s) self[method_name] else super end end
Specify optional attribute(s)
@api public
# File lib/github_api/api/arguments.rb, line 56 def optional(*attrs, &block) end
Parse arguments to allow for flexible api calls
Arguments can be part of parameters hash or be simple string arguments.
@api public
# File lib/github_api/api/arguments.rb, line 90 def parse(*args, &block) options = ParamsHash.new(args.extract_options!) normalize! options if args.size.zero? # Arguments are inside the parameters hash parse_hash(options) else parse_array(*args) end @params = options @remaining = args[@required.size..-1] extract_pagination(options) yield_or_eval(&block) self end
Specify required attribute(s)
@api public
# File lib/github_api/api/arguments.rb, line 46 def require(*attrs, &block) attrs_clone = attrs.clone @required = Array(attrs_clone) self end
# File lib/github_api/api/arguments.rb, line 81 def respond_to_missing?(method_name, include_private = false) @assigns.key?(method_name) || super end
Private Instance Methods
Check if api has non-empty property
@param [String] property
the property to check
@return [Boolean]
@api private
# File lib/github_api/api/arguments.rb, line 211 def api_property?(property) api.respond_to?(:"#{property}") && api.send(:"#{property}") end
Check if required arguments are present.
# File lib/github_api/api/arguments.rb, line 218 def check_requirement!(*args) args_length = args.length required_length = @required.length if args_length < required_length raise ArgumentError, "Wrong number of arguments " "(#{args_length} for #{required_length}). " "Expected `#{@required.join(', ')}` as required arguments, " "but got `#{args.join(", ")}`." end end
Find auto_pagination parameter in options hash
# File lib/github_api/api/arguments.rb, line 232 def extract_pagination(options) if (value = options.delete(AUTO_PAGINATION)) api.auto_pagination = value end end
Parse array arguments and assign in order to required properties
@param [Array] args
@raise ArgumentError
@return [nil]
@api public
# File lib/github_api/api/arguments.rb, line 144 def parse_array(*args) assert_presence_of(*args) @required.each_with_index do |req, indx| @assigns[req] = args[indx] end check_requirement!(*args) end
Remove required arguments from parameters and validate their presence(if not nil or empty string).
@param [Hash] options
@return [nil]
@api private
# File lib/github_api/api/arguments.rb, line 160 def parse_hash(options) options.each { |key, val| remove_required(options, key, val) } hash = update_required_from_global check_requirement!(*hash.keys) end
Remove required property from hash
@param [Hash] options
the options to check
@param [String] key
the key to remove
@param [String] val
the value to assign
@api private
# File lib/github_api/api/arguments.rb, line 178 def remove_required(options, key, val) if @required.include?(key.to_s) assert_presence_of(val) options.delete(key) @assigns[key.to_s] = val end end
Update required property from globals if not present
@return [Hash]
@api private
# File lib/github_api/api/arguments.rb, line 191 def update_required_from_global @required.reduce({}) do |hash, property| if @assigns.key?(property) hash[property] = self[property] elsif api_property?(property) hash[property] = api.send(:"#{property}") self[property] = hash[property] end hash end end
Evaluate a block
@api privte
# File lib/github_api/api/arguments.rb, line 241 def yield_or_eval(&block) return unless block block.arity > 0 ? yield(self) : instance_eval(&block) end