Class: BitBucket::API::Arguments

Inherits:
Object
  • Object
show all
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

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:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :required (Array[String])

    arguments that must be present before request is fired

  • :api (BitBucket::API)

    the reference to the current api



33
34
35
36
37
38
39
40
41
42
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 33

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



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

#apiObject (readonly)

The request api



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

def api
  @api
end

#paramsObject (readonly)

Parameters passed to request



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

def params
  @params
end

#remainingObject (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

Parameters:

  • property (String, Symbol)

    the property name



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)
  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


113
114
115
116
# File 'lib/bitbucket_rest_api/api/arguments.rb', line 113

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)



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

Returns:

  • (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