lib/peddler/marketplace.rb in peddler-1.6.7 vs lib/peddler/marketplace.rb in peddler-2.0.0
- old
+ new
@@ -5,21 +5,40 @@
# @see https://docs.developer.amazonservices.com/en_US/dev_guide/DG_Endpoints.html
Marketplace = Struct.new(:id, :country_code, :host) do
class << self
attr_reader :all
- def find(id)
- all.find { |marketplace| marketplace.id == id } || begin
- message = if id
- %("#{id}" is not a valid MarketplaceId)
- else
- 'missing MarketplaceId'
- end
+ def find(key)
+ marketplace = if key.nil?
+ missing_key!
+ elsif key.size == 2
+ find_by_country_code(key)
+ else
+ find_by_id(key)
+ end
- raise ArgumentError, message
- end
+ marketplace || not_found!(key)
end
+
+ private
+
+ def find_by_country_code(country_code)
+ country_code = 'GB' if country_code == 'UK'
+ all.find { |marketplace| marketplace.country_code == country_code }
+ end
+
+ def find_by_id(id)
+ all.find { |marketplace| marketplace.id == id }
+ end
+
+ def missing_key!
+ raise ArgumentError, 'missing marketplace'
+ end
+
+ def not_found!(country_code)
+ raise ArgumentError, %("#{country_code}" is not a valid marketplace)
+ end
end
# Caveat: We use the supersets Windows-31J and CP1252 in place of Shift_JIS
# and ISO 8859-1 respectively to handle edge cases where latter will not
# support some characters. The supersets should be safe to use as drop-in
@@ -42,10 +61,10 @@
['A2Q3Y263D00KWC', 'BR', 'mws.amazonservices.com'],
['A1PA6795UKMFR9', 'DE', 'mws-eu.amazonservices.com'],
['A1RKKUPIHCS9HS', 'ES', 'mws-eu.amazonservices.com'],
['A13V1IB3VIYZZH', 'FR', 'mws-eu.amazonservices.com'],
['APJ6JRA9NG5V4', 'IT', 'mws-eu.amazonservices.com'],
- ['A1F83G8C2ARO7P', 'UK', 'mws-eu.amazonservices.com'],
+ ['A1F83G8C2ARO7P', 'GB', 'mws-eu.amazonservices.com'],
['A21TJRUUN4KGV', 'IN', 'mws.amazonservices.in'],
['A39IBJ37TRP1C6', 'AU', 'mws.amazonservices.com.au'],
['A1VC38T7YXB528', 'JP', 'mws.amazonservices.jp'],
['AAHKV2X7AFYLW', 'CN', 'mws.amazonservices.com.cn']
].map do |values|