Skip to content

Instantly share code, notes, and snippets.

@tyrel86
Created May 19, 2014 19:00
Show Gist options
  • Save tyrel86/1f281dda8ec74ec8accc to your computer and use it in GitHub Desktop.
Save tyrel86/1f281dda8ec74ec8accc to your computer and use it in GitHub Desktop.
Sunlight API access
s = Sunlight::UrlGenerator.new("inventory/904655", {format: "json"})
At this test run the URL evaulated to https://services.sunlightsupply.com/v1/inventory/904655?format=json&X-ApiKey=gMgF47gaFdncTcE2GLoTEdmAcXHWakNXyKdPZjKSoiMAWgWLCR&time=2014-05-19T18:59:49Z&signature=113295BAAD176C82CD09127F7CB5E4135B995FA1
I got an invalid signature
module Sunlight
class << self
def public_key
"gMgF47gaFdncTcE2GLoTEdmAcXHWakNXyKdPZjKSoiMAWgWLCR"
end
def secret_key
"8w8HP3T3yF2z3pgyjic6"
end
end
class UrlGenerator
attr_reader :url
def initialize( uri_suffix, get_params={} )
@base_url = "https://services.sunlightsupply.com/v1/#{uri_suffix}?"
@get_params = get_params
@time_stamp = Time.zone.now.strftime("%Y-%m-%dT%H:%M:%SZ")
append_get_params
append_api_key
append_time_stamp
append_signature
end
def append_get_params
@get_params.each_with_index do |(key,value), i|
@base_url += "#{key}=#{value}"
@base_url += "&" unless i == (@get_params.size - 1)
end
end
def append_api_key
@base_url += "&" if @get_params.size > 0
@base_url += "X-ApiKey=#{Sunlight::public_key}"
end
def append_time_stamp
@base_url += "&time=#{@time_stamp}"
end
def append_signature
@signature = Digest::HMAC.hexdigest(@base_url, Sunlight::secret_key, Digest::SHA1).upcase
@url = "#{@base_url}&signature=#{@signature}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment