class Airbox::Resource

Public Class Methods

add_relevant_opts_to_params(opts) click to toggle source

Method to move opts into params hash

# File lib/airbox.rb, line 82
def add_relevant_opts_to_params(opts)
  params = opts[:params] || {}
  opts.except(:params).each do |k,v|
    params[k] = v
  end
  params
end
auth_token=(tok) click to toggle source

Store auth_token as class variable

# File lib/airbox.rb, line 47
def auth_token=(tok)
  @@auth_token = tok
end
ensure_collection(coll) click to toggle source
# File lib/airbox.rb, line 102
def ensure_collection(coll)
  klass = self.class.to_s.split('::').last.downcase
  if coll.has_key?(klass)
    coll = coll[klass]
  end
  coll
end
find(*arguments) click to toggle source

Before standard ActiveResource::Base.find, prep params for request

Calls superclass method
# File lib/airbox.rb, line 52
def find(*arguments)
  if !site
    err = "No site defined. Please run Airbox.site = 'site_url'"
    raise ConfigError, err
  end
  arguments = prepare_params(*arguments)
  super(*arguments)
end
instantiate_collection(collection, original_params = {}) click to toggle source

After standard ActiveResource::Base.instantiate_collection, ensure collection is returned as Airbox::Array for easier use

TODO - needs update to ActiveResource4 + backwards compatibility (Currently conforms to ActiveResource 3.2.13 spec)

Calls superclass method
# File lib/airbox.rb, line 95
def instantiate_collection(collection, original_params = {})
  ary = super(collection, original_params)
  ary = Array.new(ary)
  ary.opts = @opts
  ary
end
prepare_params(*arguments) click to toggle source

Move opts (except for “from”) into params hash Add auth_token to params hash

# File lib/airbox.rb, line 63
def prepare_params(*arguments)
  if !class_variable_defined?(:@@auth_token)
    err = "No auth token. Please run Airbox.auth_token = 'token'"
    raise ConfigError, err
  end

  opts = arguments.last.is_a?(Hash) ? arguments.pop : {}

  # Store pre-transformed opts in instance var
  # Later accessed to ensure continuity on paged results
  @opts = opts.dup

  opts[:params] = add_relevant_opts_to_params(opts.except(:from))
  opts[:params].merge!(auth_token: @@auth_token)
  arguments << opts
  arguments
end