class Marvellous::Client

Constants

DEF_PAGE_NUM
DEF_PAGE_SIZE

Attributes

private_key[RW]
public_key[RW]

Public Class Methods

new(options) click to toggle source
# File lib/marvellous/client.rb, line 13
def initialize(options)
  @public_key = options[:public_key]
  @private_key = options[:private_key]
  @prefix = '/v1/public'
  @routes = Marvellous::ROUTES
end

Private Instance Methods

build_response_object(response) click to toggle source
# File lib/marvellous/client.rb, line 54
def build_response_object(response)
  Response.create(response)
end
build_uri(method, params={}) click to toggle source
# File lib/marvellous/client.rb, line 44
def build_uri(method, params={})
  endpoint = "#{routes[method][:path]}"
  if params.any?
    params.each do |p_key, p_value|
      endpoint.gsub!(":#{p_key.to_s}", p_value.to_s)
    end
  end
  endpoint
end
digest() click to toggle source
# File lib/marvellous/client.rb, line 27
def digest
  Digest::MD5.hexdigest("#{ts}#{private_key}#{public_key}")
end
fetch_response(route, options = {}) click to toggle source
# File lib/marvellous/client.rb, line 58
def fetch_response(route, options = {})
  self.class.get(@prefix+route, query: params(options), headers: { 'Accept-Encoding' => 'gzip' })
end
method_missing(method, *args, &block) click to toggle source
# File lib/marvellous/client.rb, line 66
def method_missing(method, *args, &block)
  if routes.keys.include?(method)
    params = args[0] || {}
    endpoint = build_uri(method, params)
    page_hash = (routes[method][:collection] rescue false) ? paginate(params) : {}
    response = fetch_response(endpoint, page_hash)
    build_response_object(response)
  else
    raise InvalidRouteError, "#{method} is not defined in the routes."
  end
end
paginate(options) click to toggle source
# File lib/marvellous/client.rb, line 37
def paginate(options)
  return {} if options[:paginate] == false
  page_num = options[:page_num] || DEF_PAGE_NUM
  page_size = options[:page_size] || DEF_PAGE_SIZE
  {offset: (page_num-1)*page_size, limit: page_size}
end
params(additional_params = {}) click to toggle source
# File lib/marvellous/client.rb, line 22
def params(additional_params = {})
  base_hash = { :apikey => public_key, :ts => ts, :hash => digest }
  additional_params.merge(base_hash)
end
routes() click to toggle source
# File lib/marvellous/client.rb, line 62
def routes
  @routes
end
ts() click to toggle source
# File lib/marvellous/client.rb, line 31
def ts
  begin
    Time.now.to_i
  end
end