class Gyft::Client

Constants

ENDPOINTS

Attributes

api_key[RW]
api_secret[RW]
environment[RW]
http[RW]
reseller[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/gyft/client.rb, line 19
def initialize(options = {})
  @api_key = options.fetch(:api_key) do
    ENV['GYFT_API_KEY'] || raise(KeyError, "Missing required argument: api_key")
  end

  @api_secret = options.fetch(:api_secret) do
    ENV['GYFT_API_SECRET'] || raise(KeyError, "Missing required argument: api_secret")
  end

  @environment = begin
    environment = options.fetch(:environment) do
      ENV['GYFT_API_ENVIRONMENT'] || 'sandbox'
    end
    unless %w{production sandbox}.include?(environment)
      raise(KeyError, "Invalid argument: environment should be one of 'production' or 'sandbox'")
    end
    environment
  end

  @http = Net::HTTP.new(ENDPOINTS[environment], Net::HTTP.https_default_port)
  http.use_ssl = true
end

Public Instance Methods

cards() click to toggle source
# File lib/gyft/client.rb, line 42
def cards
  reseller.shop_cards
end
get(path) click to toggle source
# File lib/gyft/client.rb, line 58
def get(path)
  uri, timestamp = uri_for(path)
  message = Net::HTTP::Get.new(uri.request_uri)
  message['x-sig-timestamp'] = timestamp
  transmit(message)
end
health() click to toggle source
# File lib/gyft/client.rb, line 46
def health
  Gyft::Client::Health.new(self)
end
partner() click to toggle source
# File lib/gyft/client.rb, line 54
def partner
  Gyft::Client::Partner.new(self)
end
post(path, params = {}) click to toggle source
# File lib/gyft/client.rb, line 65
def post(path, params = {})
  uri, timestamp = uri_for(path, params)
  message = Net::HTTP::Post.new(uri.request_uri)
  message['x-sig-timestamp'] = timestamp
  transmit(message)
end

Private Instance Methods

json?(response) click to toggle source
# File lib/gyft/client.rb, line 113
def json?(response)
  content_type = response['Content-Type']
  json_header = content_type && content_type.split(';').first == 'application/json'
  has_body = response.body && response.body.length > 0
  json_header && has_body
end
parse(response) click to toggle source
# File lib/gyft/client.rb, line 94
def parse response
  case response
  when Net::HTTPSuccess
    json?(response) ? JSON.parse(response.body) : response.body
  when Net::HTTPNotFound
    raise NotFoundError, "Record not found (404)"
  when Net::HTTPBadRequest
    raise BadRequestError, "Bad request (400)"
  when Net::HTTPUnauthorized
    raise UnauthorizedError, "Access Denied (401)"
  else
    if json?(response)
      raise ServerError, "HTTP #{response.code}: #{JSON.parse(response.body)}"
    else
      raise ServerError, "HTTP #{response.code}: #{response.body}"
    end
  end
end
signature() click to toggle source
# File lib/gyft/client.rb, line 84
def signature
  timestamp = Time.now.getutc.to_i.to_s
  plaintext = api_key + api_secret + timestamp
  [Digest::SHA256.new.hexdigest(plaintext), timestamp]
end
transmit(message) click to toggle source
# File lib/gyft/client.rb, line 90
def transmit(message)
  parse(http.request(message))
end
uri_for(path, params = {}) click to toggle source
# File lib/gyft/client.rb, line 74
def uri_for(path, params = {})
  uri = URI("https://#{ENDPOINTS[environment]}/mashery/v1#{path}")
  sig, timestamp = signature
  uri.query = URI.encode_www_form(params.merge({
    api_key: api_key,
    sig: sig
  }))
  [uri, timestamp]
end