lib/amazon-associates/request.rb in amazon-associates-0.7.1 vs lib/amazon-associates/request.rb in amazon-associates-0.7.2

- old
+ new

@@ -4,12 +4,10 @@ require File.join(File.dirname(__FILE__), file) end require 'net/http' require 'cgi' -require 'hmac' -require 'hmac-sha2' require 'base64' module Amazon module Associates def self.request(actions, &block) @@ -89,10 +87,11 @@ def self.valid_arguments(operation) BASE_ARGS + VALID_ARGS.fetch(operation, OTHER_ARGS) end + HMAC_DIGEST = OpenSSL::Digest::Digest.new('sha256') TLDS = HashWithIndifferentAccess.new( 'us' => 'com', 'uk' => 'co.uk', 'ca' => 'ca', 'de' => 'de', @@ -120,13 +119,12 @@ 'Timestamp' => Time.now.gmtime.iso8601, 'SignatureVersion' => '2', 'SignatureMethod' => "HmacSHA256" ) - unsigned_uri = URI.parse("http://ecs.amazonaws.#{tld(opts.delete("Country"))}/onca/xml?#{params.sort { |a, b| a[0] <=> b[0] }.map { |key, val| "#{key}=#{CGI::escape(val).gsub('+', '%20')}" }.join("&")}") - hmac = HMAC::SHA256.new(ENV['AMAZON_SECRET_ACCESS_KEY']) - hmac.update("GET\n#{unsigned_uri.host}\n#{unsigned_uri.path}\n#{unsigned_uri.query}") - "#{unsigned_uri}&Signature=#{CGI::escape(Base64.encode64(hmac.digest).chomp)}" + unsigned_uri = URI.parse("http://ecs.amazonaws.#{tld(opts.delete(:country))}/onca/xml?#{params.sort { |a, b| a[0] <=> b[0] }.map { |key, val| "#{key}=#{CGI::escape(val).gsub('+', '%20')}" }.join("&")}") + hmac = OpenSSL::HMAC.digest(HMAC_DIGEST, ENV['AMAZON_SECRET_ACCESS_KEY'], "GET\n#{unsigned_uri.host}\n#{unsigned_uri.path}\n#{unsigned_uri.query}") + "#{unsigned_uri}&Signature=#{CGI::escape(Base64.encode64(hmac).chomp)}" end def self.cacheable?(operation) caching_enabled? && !operation.starts_with?('Cart') end