class Assembla::API::Arguments

A class responsible for handilng request arguments

Constants

AUTO_PAGINATION

Attributes

api[R]

The request api

params[R]

Parameters passed to request

remaining[R]

The remaining unparsed arguments

Public Class Methods

new(options = {}, &block) click to toggle source

Initialize an Arguments

@param [Hash] options

@option options [Array] :required

arguments that must be present before request is fired

@option options [Assembla::API] :api

the reference to the current api

@api public

# File lib/assembla_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

Public Instance Methods

[](property) click to toggle source

Hash like access to request arguments

@param [String, Symbol] property

the property name

@api public

# File lib/assembla_api/api/arguments.rb, line 66
def [](property)
  @assigns[property.to_s]
end
[]=(property, value) click to toggle source
# File lib/assembla_api/api/arguments.rb, line 70
def []=(property, value)
  @assigns[property.to_s] = value
end
assert_required(required) click to toggle source

Check if required keys are present inside parameters hash.

@api public

# File lib/assembla_api/api/arguments.rb, line 121
def assert_required(required)
  assert_required_keys required, params
  self
end
assert_values(values, key=nil) click to toggle source

Check if parameters match expected values.

@api public

# File lib/assembla_api/api/arguments.rb, line 129
def assert_values(values, key=nil)
  assert_valid_values values, (key.nil? ? params : params[key])
  self
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/assembla_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
optional(*attrs, &block) click to toggle source

Specify optional attribute(s)

@api public

# File lib/assembla_api/api/arguments.rb, line 57
def optional(*attrs, &block)
end
parse(*args, &block) click to toggle source

Parse arguments to allow for flexible api calls

Arguments can be part of parameters hash or be simple string arguments.

@api public

# File lib/assembla_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={}) click to toggle source

Remove unknown keys from parameters hash.

Parameters

:recursive - boolean that toggles whether nested filtering should be applied
# File lib/assembla_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) click to toggle source

Specify required attribute(s)

@api public

# File lib/assembla_api/api/arguments.rb, line 47
def require(*attrs, &block)
  attrs_clone = attrs.clone
  @required = Array(attrs_clone)
  self
end
Also aliased as: required
required(*attrs, &block)
Alias for: require
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/assembla_api/api/arguments.rb, line 82
def respond_to_missing?(method_name, include_private = false)
  @assigns.key?(method_name) || super
end

Private Instance Methods

api_property?(property) click to toggle source

Check if api has non-empty property

@param [String] property

the property to check

@return [Boolean]

@api private

# File lib/assembla_api/api/arguments.rb, line 212
def api_property?(property)
  api.respond_to?(:"#{property}") && api.send(:"#{property}")
end
check_requirement!(*args) click to toggle source

Check if required arguments are present.

# File lib/assembla_api/api/arguments.rb, line 219
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
extract_pagination(options) click to toggle source

Find auto_pagination parameter in options hash

# File lib/assembla_api/api/arguments.rb, line 233
def extract_pagination(options)
  if (value = options.delete(AUTO_PAGINATION))
    api.auto_pagination = value
  end
end
parse_array(*args) click to toggle source

Parse array arguments and assign in order to required properties

@param [Array] args

@raise ArgumentError

@return [nil]

@api public

# File lib/assembla_api/api/arguments.rb, line 145
def parse_array(*args)
  assert_presence_of(*args)
  @required.each_with_index do |req, indx|
    @assigns[req] = args[indx]
  end
  check_requirement!(*args)
end
parse_hash(options) click to toggle source

Remove required arguments from parameters and validate their presence(if not nil or empty string).

@param [Hash] options

@return [nil]

@api private

# File lib/assembla_api/api/arguments.rb, line 161
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(options, key, val) click to toggle source

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/assembla_api/api/arguments.rb, line 179
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_from_global() click to toggle source

Update required property from globals if not present

@return [Hash]

@api private

# File lib/assembla_api/api/arguments.rb, line 192
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
yield_or_eval() { |self| ... } click to toggle source

Evaluate a block

@api privte

# File lib/assembla_api/api/arguments.rb, line 242
def yield_or_eval(&block)
  return unless block
  block.arity > 0 ? yield(self) : instance_eval(&block)
end