lib/vacuum/locale.rb in vacuum-3.0.0 vs lib/vacuum/locale.rb in vacuum-3.1.0
- old
+ new
@@ -1,53 +1,62 @@
# frozen_string_literal: true
module Vacuum
- # The target Amazon locale
- # @api private
+ # The target locale
+ #
+ # @see https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
class Locale
- class NotFound < ArgumentError; end
+ # Raised when the provided marketplace does not correspond to an existing
+ # Amazon locale
+ class NotFound < KeyError; end
- attr_reader :code, :domain, :region
+ # @!visibility private
+ HOSTS_AND_REGIONS = {
+ au: ['webservices.amazon.com.au', 'us-west-2'],
+ br: ['webservices.amazon.com.br', 'us-east-1'],
+ ca: ['webservices.amazon.ca', 'us-east-1'],
+ fr: ['webservices.amazon.fr', 'eu-west-1'],
+ de: ['webservices.amazon.de', 'eu-west-1'],
+ in: ['webservices.amazon.in', 'eu-west-1'],
+ it: ['webservices.amazon.it', 'eu-west-1'],
+ jp: ['webservices.amazon.co.jp', 'us-west-2'],
+ mx: ['webservices.amazon.com.mx', 'us-east-1'],
+ es: ['webservices.amazon.es', 'eu-west-1'],
+ tr: ['webservices.amazon.com.tr', 'eu-west-1'],
+ ae: ['webservices.amazon.ae', 'eu-west-1'],
+ gb: ['webservices.amazon.co.uk', 'eu-west-1'],
+ us: ['webservices.amazon.com', 'us-east-1']
+ }.freeze
- def self.find(code)
- code = code.to_sym.downcase
- code = :gb if code == :uk
+ # @return [String]
+ attr_reader :host, :region, :access_key, :secret_key, :partner_tag,
+ :partner_type
- @all.find { |locale| locale.code == code } || raise(NotFound)
+ # Creates a locale
+ #
+ # @param [Symbol,String] marketplace
+ # @param [String] access_key
+ # @param [String] secret_key
+ # @param [String] partner_tag
+ # @param [String] partner_type
+ # @raise [NotFound] if marketplace is not found
+ def initialize(marketplace, access_key:, secret_key:, partner_tag:,
+ partner_type: 'Associates')
+ @host, @region = find_host_and_region(marketplace)
+ @access_key = access_key
+ @secret_key = secret_key
+ @partner_tag = partner_tag
+ @partner_type = partner_type
end
- def initialize(code, domain, region)
- @code = code
- @domain = domain
- @region = region
- end
+ private
- def endpoint
- "webservices.#{domain}"
- end
+ def find_host_and_region(marketplace)
+ marketplace = marketplace.to_sym.downcase
+ marketplace = :gb if marketplace == :uk
- def marketplace
- "www.#{domain}"
+ HOSTS_AND_REGIONS.fetch(marketplace)
+ rescue KeyError
+ raise NotFound, "marketplace not found: :#{marketplace}"
end
-
- def build_url(operation)
- "https://#{endpoint}/paapi5/#{operation.downcase}"
- end
-
- @all = [
- [:au, 'amazon.com.au', 'us-west-2'],
- [:br, 'amazon.com.br', 'us-east-1'],
- [:ca, 'amazon.ca', 'us-east-1'],
- [:fr, 'amazon.fr', 'eu-west-1'],
- [:de, 'amazon.de', 'eu-west-1'],
- [:in, 'amazon.in', 'eu-west-1'],
- [:it, 'amazon.it', 'eu-west-1'],
- [:jp, 'amazon.co.jp', 'us-west-2'],
- [:mx, 'amazon.com.mx', 'us-east-1'],
- [:es, 'amazon.es', 'eu-west-1'],
- [:tr, 'amazon.com.tr', 'eu-west-1'],
- [:ae, 'amazon.ae', 'eu-west-1'],
- [:gb, 'amazon.co.uk', 'eu-west-1'],
- [:us, 'amazon.com', 'us-east-1']
- ].map { |attributes| new(*attributes) }
end
end