lib/amazon-associates/request.rb in amazon-associates-0.6.3 vs lib/amazon-associates/request.rb in amazon-associates-0.7.0

- old
+ new

@@ -35,11 +35,11 @@ end request_url = prepare_url(opts) response = nil - if cacheable?(opts['Operation']) + if cache_it = cacheable?(opts[:operation]) FilesystemCache.sweep response = FilesystemCache.get(request_url) end @@ -48,14 +48,14 @@ response = Net::HTTP.get_response(URI::parse(request_url)) unless response.kind_of? Net::HTTPSuccess raise RequestError, "HTTP Response: #{response.inspect}" end - cache_response(request_url, response) if cacheable?(opts['Operation']) + cache_response(request_url, response) if cache_it end - doc = ROXML::XML::Parser.parse(response.body).root + doc = ROXML::XML::Node.from(response.body) eval(doc.name).from_xml(doc, request_url) end BASE_ARGS = [:aWS_access_key_id, :operation, :associate_tag, :response_group] CART_ARGS = [:cart_id, :hMAC] @@ -106,24 +106,22 @@ def self.prepare_url(opts) opts = opts.to_hash.to_options! raise opts.inspect if opts.has_key?(:cart) opts.assert_valid_keys(*valid_arguments(opts[:operation])) - params = opts.each_pair do |k, v| - opts.delete(k) + params = Hash[opts.map do |(k, v)| v *= ',' if v.is_a? Array - opts[k.to_s.camelize] = v.to_s - params - end + [k.to_s.camelize, v.to_s] + end] params.merge!( 'Service' => 'AWSECommerceService', 'Timestamp' => Time.now.gmtime.iso8601, 'SignatureVersion' => '2', 'SignatureMethod' => "HmacSHA256" ) - unsigned_uri = URI.parse("http://webservices.amazon.#{tld(opts.delete("Country"))}/onca/xml?#{params.sort { |a, b| a[0] <=> b[0] }.map { |key, val| "#{key}=#{CGI::escape(val).gsub('+', '%20')}" }.join("&")}") + 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)}" end