lib/amazon_product_advertising_api/operations/base.rb
# This takes care of building request, performing it, storing the results, checking for errors then parsing the data (if the request was valid).
This takes care of building request, performing it, storing the results, checking for errors then parsing the data (if the request was valid).
This takes care of building request performing it storing the results checking for errors then parsing the data ( if the request was valid ) .
def query_amazon(params)
          request_params = {}
          request_params["Service"]          = "AWSECommerceService"
          request_params["SignatureVersion"] = 2
          request_params["SignatureMethod"]  = "HmacSHA256"
          request_params["Timestamp"]        = Time.now.gmtime.iso8601
          request_params["AWSAccessKeyId"]   = AmazonProductAdvertisingApi::Base.access_key_id
          request_params["Operation"]        = self.operation
          request_params["AssociateTag"]     = AmazonProductAdvertisingApi::Base.associate_ids.send(self.region) unless AmazonProductAdvertisingApi::Base.associate_ids.send(self.region).nil?
          request_params["Version"]          = API_VERSION
          request_params.merge!(params)
          request_params = request_params.collect { |var, val| [var.to_s.camelize, val.to_s] }
          request_params = request_params.collect { |var, val| [var, CGI::escape(val).gsub('+', '%20')] }
          unsigned_uri = URI.parse("#{SERVICE_URLS[self.region]}?#{request_params.sort { |a, b| a[0] <=> b[0] }.collect { |var, val| var + "=" + val }.join("&")}")
          hmac = HMAC::SHA256.new(AmazonProductAdvertisingApi::Base.secret_access_key)
          hmac.update("GET\n#{unsigned_uri.host}\n#{unsigned_uri.path}\n#{unsigned_uri.query}")
          self.request_uri = URI.parse("#{unsigned_uri}&Signature=#{CGI::escape(Base64.encode64(hmac.digest).chomp)}")
          result = Net::HTTP::get_response(self.request_uri)
          raise("Error connecting to Amazon - #{result.to_s}") if !result.kind_of?(Net::HTTPSuccess)
          self.raw_data     = result.body
          self.hpricot_data = Hpricot.XML(self.raw_data)
          self.is_valid = self.hpricot_data.at(:IsValid).inner_html == "True"
          self.parse
          if !self.hpricot_data.at(:Errors).nil?
            self.hpricot_data.at(:Errors).search(:Error).each do |error|
              self.errors << Struct.new(:code, :message).new(error.at(:Code).inner_html, error.at(:Message).inner_html)
            end
          end
          self.is_valid ? self.response : false
        end
def query_amazon ( params ) request_params = { } request_params [ "Service" ] = "AWSECommerceService" request_params [ "SignatureVersion" ] = 2 request_params [ "SignatureMethod" ] = "HmacSHA256" request_params [ "Timestamp" ] = Time . now . gmtime . iso8601 request_params [ "AWSAccessKeyId" ] = AmazonProductAdvertisingApi :: Base . access_key_id request_params [ "Operation" ] = self . operation request_params [ "AssociateTag" ] = AmazonProductAdvertisingApi :: Base . associate_ids . send ( self . region ) unless AmazonProductAdvertisingApi :: Base . associate_ids . send ( self . region ) . nil? request_params [ "Version" ] = API_VERSION request_params . merge! ( params ) request_params = request_params . collect { | var , val | [ var . to_s . camelize , val . to_s ] } request_params = request_params . collect { | var , val | [ var , CGI :: escape ( val ) . gsub ( '+' , '%20' ) ] } unsigned_uri = URI . parse ( "#{SERVICE_URLS[self.region]}?#{request_params.sort { |a, b| a[0] <=> b[0] }.collect { |var, val| var + "=" + val }.join("&")}" ) hmac = HMAC :: SHA256 . new ( AmazonProductAdvertisingApi :: Base . secret_access_key ) hmac . update ( "GET\n#{unsigned_uri.host}\n#{unsigned_uri.path}\n#{unsigned_uri.query}" ) self . request_uri = URI . parse ( "#{unsigned_uri}&Signature=#{CGI::escape(Base64.encode64(hmac.digest).chomp)}" ) result = Net :: HTTP :: get_response ( self . request_uri ) raise ( "Error connecting to Amazon - #{result.to_s}" ) if ! result . kind_of? ( Net :: HTTPSuccess ) self . raw_data = result . body self . hpricot_data = Hpricot . XML ( self . raw_data ) self . is_valid = self . hpricot_data . at ( :IsValid ) . inner_html == "True" self . parse if ! self . hpricot_data . at ( :Errors ) . nil? self . hpricot_data . at ( :Errors ) . search ( :Error ) . each do | error | self . errors << Struct . new ( :code , :message ) . new ( error . at ( :Code ) . inner_html , error . at ( :Message ) . inner_html ) end end self . is_valid ? self . response : false end
This takes care of building request, performing it, storing the results, checking for errors then parsing the data (if the request was valid).
This takes care of building request performing it storing the results checking for errors then parsing the data ( if the request was valid ) .
# Process all params - make sure they're all strings, camelize and escape (where appropriate) # Assemble into a full request string # Generate hmac # Store away the raw data for debugging or if more direct access is required # Now parse the xml and build out the reponse elements # is_valid only refers to the request, so we could still have errors - check and parse if present # Return false if it's not a valid request, otherwise return the response
params
<not_specific>