Class: Github::Arguments

Inherits:
Object
  • Object
show all
Includes:
Normalizer, ParameterFilter, Validations
Defined in:
lib/github_api/arguments.rb

Overview

Request arguments handler

Constant Summary collapse

AUTO_PAGINATION =
'auto_pagination'.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


37
38
39
40
41
42
# File 'lib/github_api/arguments.rb', line 37

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



20
21
22
# File 'lib/github_api/arguments.rb', line 20

def api
  @api
end

#paramsObject (readonly)

Parameters passed to request



14
15
16
# File 'lib/github_api/arguments.rb', line 14

def params
  @params
end

#remainingObject (readonly)

Returns the value of attribute remaining.



16
17
18
# File 'lib/github_api/arguments.rb', line 16

def remaining
  @remaining
end

Instance Method Details

#assert_required(required) ⇒ Object

Check if required keys are present inside parameters hash.



76
77
78
79
# File 'lib/github_api/arguments.rb', line 76

def assert_required(required)
  assert_required_keys required, params
  self
end

#assert_values(values, key = nil) ⇒ Object

Check if parameters match expected values.



83
84
85
86
# File 'lib/github_api/arguments.rb', line 83

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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/github_api/arguments.rb', line 47

def parse(*args, &block)
  options = ParamsHash.new(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)
  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


69
70
71
72
# File 'lib/github_api/arguments.rb', line 69

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