lib/sucker/request.rb in sucker-1.4.1 vs lib/sucker/request.rb in sucker-1.5.0

- old
+ new

@@ -1,6 +1,6 @@ -require 'net/http' +require 'httpclient' require 'openssl' require 'sucker/parameters' module Sucker @@ -12,10 +12,15 @@ :de => 'ecs.amazonaws.de', :ca => 'ecs.amazonaws.ca', :fr => 'ecs.amazonaws.fr', :jp => 'ecs.amazonaws.jp' } + # Available Amazon locales. + def self.locales + @locales ||= HOSTS.keys + end + # The Amazon associate tag. attr_accessor :associate_tag # The Amazon Web Services access key. attr_accessor :key @@ -28,50 +33,61 @@ # Initializes a request object. # # Takes an optional hash of attribute and value pairs. # - # worker = Sucker.new( - # :key => 'API KEY', - # :secret => 'API SECRET') + # request = Sucker.new( + # :locale => :us, + # :key => a_key, + # :secret => a_secret, + # :associate_tag => a_tag) # def initialize(args={}) args.each { |k, v| send("#{k}=", v) } end + # The HTTP adapter. + def adapter + @adapter ||= HTTPClient.new + end + # Merges a hash into the existing parameters. # - # worker << { + # request << { # 'Operation' => 'ItemLookup', # 'IdType' => 'ASIN', # 'ItemId' => '0816614024' } # def <<(hash) parameters.merge!(hash) end # Performs a request and returns a response object. # - # response = worker.get + # response = request.get # def get - response = Net::HTTP.start(host) do |http| - query = build_signed_query - http.get("/onca/xml?#{query}") - end - + response = adapter.get(uri) Response.new(response) end # The query parameters. def parameters @parameters ||= Parameters.new end + # Resets parameters and returns itself. + def reset + parameters.clear + parameters.set_up + + self + end + # Sets the Amazon API version. # - # worker.version = '2010-06-01' + # request.version = '2010-06-01' # def version=(version) parameters['Version'] = version end @@ -104,8 +120,15 @@ end end def host HOSTS[locale.to_sym] + end + + def uri + URI::HTTP.build( + :host => host, + :path => '/onca/xml', + :query => build_signed_query) end end end