Class: Github::API::Arguments

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

Overview

A class responsible for handling request arguments

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(options = {}, &block) ⇒ Arguments

Initialize an Arguments

Parameters:

  • (defaults to: {})

Options Hash (options):

  • :required (Array[String])

    arguments that must be present before request is fired

  • :api (Github::API)

    the reference to the current api

API:

  • public



38
39
40
41
42
43
44
45
46
47
# File 'lib/github_api/api/arguments.rb', line 38

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

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_api/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

#apiObject (readonly)

The request api



25
26
27
# File 'lib/github_api/api/arguments.rb', line 25

def api
  @api
end

#paramsObject (readonly)

Parameters passed to request



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

def params
  @params
end

#remainingObject (readonly)

The remaining unparsed arguments



22
23
24
# File 'lib/github_api/api/arguments.rb', line 22

def remaining
  @remaining
end

Instance Method Details

#[](property) ⇒ Object

Hash like access to request arguments

Parameters:

  • the property name

API:

  • public



71
72
73
# File 'lib/github_api/api/arguments.rb', line 71

def [](property)
  @assigns[property.to_s]
end

#[]=(property, value) ⇒ Object



75
76
77
# File 'lib/github_api/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.

API:

  • public



126
127
128
129
# File 'lib/github_api/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.

API:

  • public



134
135
136
137
# File 'lib/github_api/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)

API:

  • public



62
63
# File 'lib/github_api/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.

API:

  • public



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/github_api/api/arguments.rb', line 96

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

#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_api/api/arguments.rb', line 118

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

#require(*attrs, &block) ⇒ Object Also known as: required

Specify required attribute(s)

API:

  • public



52
53
54
55
56
# File 'lib/github_api/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

Returns:



87
88
89
# File 'lib/github_api/api/arguments.rb', line 87

def respond_to_missing?(method_name, include_private = false)
  @assigns.key?(method_name) || super
end