Sha256: 348aeeec78797edf5d7a0cb289eb2721e91355a2ff6b26371d1f06647e0f6c17

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

require 'jeff'
require 'crack/xml'

module A2z
  class Client
    ErrorResponse = Class.new(ArgumentError)
    
    include Jeff
    
    attr_reader :country, :tag
    
    HOSTS = {
        ca: 'ecs.amazonaws.ca',
        cn: 'webservices.amazon.cn',
        de: 'ecs.amazonaws.de',
        es: 'webservices.amazon.es',
        fr: 'ecs.amazonaws.fr',
        it: 'webservices.amazon.it',
        jp: 'ecs.amazonaws.jp',
        uk: 'ecs.amazonaws.co.uk',
        us: 'ecs.amazonaws.com',
    }.freeze
    
    params 'AssociateTag' => -> { tag },
           'Service'      => 'AWSECommerceService',
           'Version'      => '2011-08-01'
    
    def initialize(opts = {})
      self.country  = opts.fetch(:country, :us)
      self.key      = opts[:key]
      self.secret   = opts[:secret]
      self.tag      = opts[:tag]
    end
    
    def country=(code)
      raise ArgumentError.new("Country code must be one of #{HOSTS.keys.join(', ')}.") if code.nil? || HOSTS[code.to_sym].nil?
      @country = code.to_sym
      @endpoint = "http://#{HOSTS[@country]}/onca/xml"
    end
    
    def item_search(&block)
      response = request(Requests::ItemSearch.new(&block))
      Responses::ItemSearch.from_response(response)
    end
    
    def item_lookup(&block)
      response = request(Requests::ItemLookup.new(&block))
      Responses::ItemLookup.from_response(response)
    end
    
    private
    
    def tag=(tag)
      @tag = tag
    end
    
    def request(req)
      response = get(query: req.params)
      response = Crack::XML.parse(response.body)
      
      # strip the root xml node
      response.values.first
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
a2z-0.0.3 lib/a2z/client.rb
a2z-0.0.2 lib/a2z/client.rb
a2z-0.0.1 lib/a2z/client.rb