class Gyftwrapper::API

Constants

CALLS

Attributes

config[RW]
result[RW]

Public Class Methods

new() click to toggle source
# File lib/gyftwrapper/api.rb, line 20
def initialize
  self.result = Result.new
  self.config = parse_config_file
end

Public Instance Methods

get_merchants() click to toggle source
# File lib/gyftwrapper/api.rb, line 33
def get_merchants
  send_request do
    response = RestClient.get(url_for(:get_merchants), 'x-sig-timestamp' => timestamp)
    self.result = JSON.parse(response)['details'].map{|m| OpenStruct.new(m)}
  end
end
purchase_card(params) click to toggle source
# File lib/gyftwrapper/api.rb, line 25
def purchase_card(params)
  send_request do
    response = RestClient.post(url_for(:purchase_card), params.merge(default_params), 'x-sig-timestamp' => timestamp)
    self.result.response = OpenStruct.new(JSON.parse(response))
    self.result.successful!
  end
end

Private Instance Methods

default_params() click to toggle source
# File lib/gyftwrapper/api.rb, line 76
def default_params
  {
    return_direct_link: true,
    whitelabel: true
  }
end
parse_config_file() click to toggle source
# File lib/gyftwrapper/api.rb, line 67
def parse_config_file
  file = Rails.root.join("config").join("gyftwrapper.yml")
  data = YAML.load(ERB.new(IO.read(file)).result)[Rails.env] rescue nil

  result.response = 'Provide configs in gyftwrapper.yml file' unless data

  data ? OpenStruct.new(data) : data
end
send_request() { || ... } click to toggle source
# File lib/gyftwrapper/api.rb, line 42
def send_request
  return result unless config

  begin
    yield
  rescue => e
    result.response = e.response
  end

  result
end
signature() click to toggle source
# File lib/gyftwrapper/api.rb, line 62
def signature
  signature_string = config.api_key + config.api_secret + timestamp
  Digest::SHA256.new.hexdigest(signature_string)
end
timestamp() click to toggle source
# File lib/gyftwrapper/api.rb, line 58
def timestamp
  Time.now.getutc.to_i.to_s
end
url_for(call_type) click to toggle source
# File lib/gyftwrapper/api.rb, line 54
def url_for(call_type)
  "#{config.endpoint}#{CALLS[call_type]}?api_key=#{config.api_key}&sig=#{signature}"
end