class Marvelite::API::Client

Attributes

api_version[RW]
private_key[RW]
public_key[RW]
router[RW]

Public Class Methods

new(attrs) click to toggle source
# File lib/marvelite/api/client.rb, line 11
def initialize(attrs)
  check_for_errors(attrs)
  @public_key = attrs.fetch(:public_key)
  @private_key = attrs.fetch(:private_key)
  @api_version = attrs.fetch(:api_version) { 'v1' }
  @router = attrs.fetch(:router) { Marvelite.router(:api_version => @api_version) }
  build_methods
end

Private Instance Methods

build_methods() click to toggle source
# File lib/marvelite/api/client.rb, line 98
def build_methods
  @router.routes.each do |_, hash|
    name = hash[:name]
    self.class.send(:define_method, name) do |*args|
      params, headers = process_arguments(*args)
      response = fetch_response(name, params, headers)
      build_response_object(response)
    end
  end
end
build_response_object(response) click to toggle source
# File lib/marvelite/api/client.rb, line 42
def build_response_object(response)
  Response.make(response)
end
check_for_errors(attrs) click to toggle source
# File lib/marvelite/api/client.rb, line 21
def check_for_errors(attrs)
  [:public_key, :private_key].each do |key|
    raise InvalidClientError, "You need to provide a :#{key} param." unless attrs[key]
  end
end
fetch_resource_id(route, id) click to toggle source
# File lib/marvelite/api/client.rb, line 91
def fetch_resource_id(route, id)
  endpoint = route.split("_")[0]+"s"
  resource = find_by_name_or_title(endpoint.to_sym, :name, id)
  return false unless resource
  resource[:id]
end
fetch_response(route, params_hash = {}, headers = {}) click to toggle source
# File lib/marvelite/api/client.rb, line 74
def fetch_response(route, params_hash = {}, headers = {})
  id = params_hash[:id]
  options = params_hash[:options]
  path = find_path(route, id)

  self.class.get(path, query: params(options), headers: headers)
end
find_by_name_or_title(endpoint, column, value) click to toggle source
# File lib/marvelite/api/client.rb, line 46
def find_by_name_or_title(endpoint, column, value)
  response = self.send(endpoint, { column.to_sym => value })
  return false unless response[:data][:count] > 0
  response[:id] = response[:data][:results][0][:id]
  response
end
find_path(route, id) click to toggle source
# File lib/marvelite/api/client.rb, line 82
def find_path(route, id)
  if id.nil?
    router.send("#{route}_path".to_sym)
  else
    id = fetch_resource_id(route, id) if id.is_a?(String)
    router.send("#{route}_path".to_sym, {:id => id})
  end
end
params(additional_params = {}) click to toggle source
# File lib/marvelite/api/client.rb, line 27
def params(additional_params = {})
  base_hash = { :apikey => public_key, :ts => ts, :hash => request_hash }
  additional_params.merge(base_hash)
end
process_arguments(*args) click to toggle source
# File lib/marvelite/api/client.rb, line 59
def process_arguments(*args)
  temp_id, temp_options = *args
  if temp_id && temp_id.is_a?(Hash)
    id = nil
    options = temp_id || {}
  elsif temp_id
    id = temp_id
    options = temp_options || {}
  else
    options = {}
  end
  params, headers = pull_headers(options)
  [{ id: id, options: params}, headers]
end
pull_headers(options) click to toggle source
# File lib/marvelite/api/client.rb, line 53
def pull_headers(options)
  headers = options.delete(:headers) || {}
  headers.merge!({ 'Accept-Encoding' => 'gzip' }) unless headers['Accept-Encoding']
  [options, headers]
end
request_hash() click to toggle source
# File lib/marvelite/api/client.rb, line 32
def request_hash
  Digest::MD5.hexdigest("#{ts}#{private_key}#{public_key}")
end
ts() click to toggle source
# File lib/marvelite/api/client.rb, line 36
def ts
  begin
    Time.now.to_i
  end
end