From 250986d1c7587fb5f94823b8dbc25d8c27512862 Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Tue, 10 Sep 2019 09:25:23 +1000 Subject: [PATCH] Add configuration system and documentation. Clean up tests --- Gemfile | 2 +- README.md | 30 ++++- lib/paapi.rb | 22 +++- lib/paapi/client.rb | 49 ++++--- test/paapi_test.rb | 307 ++++++++++---------------------------------- 5 files changed, 138 insertions(+), 272 deletions(-) diff --git a/Gemfile b/Gemfile index 29f3bea..5a13c7b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source "https://rubygems.org" +source 'https://rubygems.org' # Specify your gem's dependencies in aws_paa.gemspec gemspec diff --git a/README.md b/README.md index 064d5e3..972f44c 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ Or install it yourself as: ## Usage +### Configuration + The library can be initialised with a Rails initializer such as ```ruby @@ -35,14 +37,38 @@ Configurable itemes: * secret_key * partner_tag * partner_type - * marketplace + * market + * partner_market * test_mode +Using the `partner_market` configuration item lets you set a hash of marketplaces and partner_tags. + +```ruby +Paapi.configure do |config| + config.access_key = 'your-access-key' + config.secret_key = 'your-secret-key' + config.partner_market = {au: 'au_partner_tag', us: 'us_partner_tag'} +end + +client = Paapi::Client.new(market: :au) + +client.partner_tag +> 'au_partner_tag' + +client.market = :us + +client.partner_tag +> 'us_partner_tag' +``` + +The full list of market keys is `au, br, ca, fr, de, in, it, jp, mx, es, tr, ae, uk, us` + +### Using the Paapi ```ruby require 'paapi' -client = Paapi::Client.new(access_key: ENV['access_key'], secret_key: ENV['secret_key'], marketplace: :au, partner_tag: ENV['partner_tag']) +client = Paapi::Client.new(access_key: ENV['access_key'], secret_key: ENV['secret_key'], market: :au, partner_tag: ENV['partner_tag']) gi = client.get_items(item_ids: '1857231384') diff --git a/lib/paapi.rb b/lib/paapi.rb index 752760e..6a647fd 100644 --- a/lib/paapi.rb +++ b/lib/paapi.rb @@ -8,13 +8,33 @@ require 'paapi/response' module Paapi class Error < StandardError; end class NotImplemented < StandardError; end + DEFAULT_PARTNER_TYPE = 'Associates' + DEFAULT_MARKET = :us + DEFAULT_RESOURCES = [ + 'Images.Primary.Large', + 'ItemInfo.ContentInfo', + 'ItemInfo.ProductInfo', + 'ItemInfo.Title', + 'ItemInfo.ExternalIds', + 'Offers.Listings.Availability.Message', + 'Offers.Listings.Condition', + 'Offers.Listings.Condition.SubCondition', + 'Offers.Listings.DeliveryInfo.IsAmazonFulfilled', + 'Offers.Listings.DeliveryInfo.IsFreeShippingEligible', + 'Offers.Listings.DeliveryInfo.IsPrimeEligible', + 'Offers.Listings.MerchantInfo', + 'Offers.Listings.Price', + 'Offers.Listings.SavingBasis' + ] class << self attr_accessor :access_key, :secret_key, :partner_tag, :partner_type, - :marketplace, + :market, + :partner_market, + :resources, :test_mode def configure diff --git a/lib/paapi/client.rb b/lib/paapi/client.rb index 53c4495..8a6e635 100644 --- a/lib/paapi/client.rb +++ b/lib/paapi/client.rb @@ -3,40 +3,35 @@ require 'http' module Paapi class Client include AwsRequest - attr_accessor :marketplace, :partner_tag - attr_reader :partner_type, :access_key, :secret_key + attr_accessor :partner_tag, :marketplace, :resources + attr_reader :partner_type, :access_key, :secret_key, :market - def initialize(access_key: Paapi.access_key, - secret_key: Paapi.secret_key, - marketplace: Paapi.marketplace || :us, - partner_tag: Paapi.partner_tag, - resources: nil, - partner_type: 'Associates' + def initialize(access_key: Paapi.access_key, + secret_key: Paapi.secret_key, + partner_tag: Paapi.partner_tag, + market: Paapi.market || DEFAULT_MARKET, + resources: Paapi.resources || DEFAULT_RESOURCES, + partner_type: DEFAULT_PARTNER_TYPE ) - raise ArgumentError unless MARKETPLACES.keys.include?(marketplace.to_sym) + raise ArgumentError unless MARKETPLACES.keys.include?(market.to_sym) @access_key = access_key @secret_key = secret_key - @marketplace = MARKETPLACES[marketplace.to_sym] - @partner_tag = partner_tag @partner_type = partner_type - @resources = resources || [ - "Images.Primary.Large", - "ItemInfo.ContentInfo", - "ItemInfo.ProductInfo", - "ItemInfo.Title", - "ItemInfo.ExternalIds", - "Offers.Listings.Availability.Message", - "Offers.Listings.Condition", - "Offers.Listings.Condition.SubCondition", - "Offers.Listings.DeliveryInfo.IsAmazonFulfilled", - "Offers.Listings.DeliveryInfo.IsFreeShippingEligible", - "Offers.Listings.DeliveryInfo.IsPrimeEligible", - "Offers.Listings.MerchantInfo", - "Offers.Listings.Price", - "Offers.Listings.SavingBasis" - ] + @resources = resources unless resources.nil? + + self.market = market + @partner_tag = partner_tag if !partner_tag.nil? end + def market=(_market) + @market = _market + @marketplace = MARKETPLACES[market.to_sym] + if !Paapi.partner_market.nil? + @partner_tag = Paapi.partner_market.dig(_market) || @partner_tag + end + end + + def get_items(item_ids:, **options) item_ids = Array(item_ids) diff --git a/test/paapi_test.rb b/test/paapi_test.rb index 4e8bbbc..5972271 100644 --- a/test/paapi_test.rb +++ b/test/paapi_test.rb @@ -1,253 +1,78 @@ -require "test_helper" +require 'test_helper' class PaapiTest < Minitest::Test + def setup + Paapi.configure do |config| + config.access_key = nil + config.secret_key = nil + config.market = nil + config.partner_tag = nil + config.resources = nil + config.partner_type = nil + end + end def test_that_it_has_a_version_number refute_nil ::Paapi::VERSION end - def test_it_does_something_useful - assert false + def test_configuration_is_correctly_set_using_partner_market + Paapi.configure do |config| + config.access_key = 'some_key' + config.secret_key = 'some_secret' + config.partner_market = {au: 'au_partner', us: 'us_partner', uk: 'uk_partner', ca: 'ca_partner'} + end + + c = Paapi::Client.new + assert_equal :us, c.market, "Default Market should be US" + assert_equal 'us_partner', c.partner_tag + + c = Paapi::Client.new(market: :au) + assert_equal 'au_partner', c.partner_tag + + c.market = :ca + assert_equal 'ca_partner', c.partner_tag + + # set the market to an unconfigured market, should not change the partner_tag + orig_tag = c.partner_tag + c.market = :gb + assert_equal orig_tag, c.partner_tag + + end - def test_json_payload_generation - payload = { - "Keywords": "9781473619814", - "Resources": [ - "Images.Primary.Large", - "ItemInfo.ByLineInfo", - "ItemInfo.ContentInfo", - "ItemInfo.ContentRating", - "ItemInfo.Classifications", - "ItemInfo.ExternalIds", - "ItemInfo.Features", - "ItemInfo.ManufactureInfo", - "ItemInfo.ProductInfo", - "ItemInfo.TechnicalInfo", - "ItemInfo.Title", - "ItemInfo.TradeInInfo", - "Offers.Listings.Availability.MaxOrderQuantity", - "Offers.Listings.Availability.Message", - "Offers.Listings.Availability.MinOrderQuantity", - "Offers.Listings.Availability.Type", - "Offers.Listings.Condition", - "Offers.Listings.Condition.SubCondition", - "Offers.Listings.DeliveryInfo.IsAmazonFulfilled", - "Offers.Listings.DeliveryInfo.IsFreeShippingEligible", - "Offers.Listings.DeliveryInfo.IsPrimeEligible", - "Offers.Listings.DeliveryInfo.ShippingCharges", - "Offers.Listings.IsBuyBoxWinner", - "Offers.Listings.LoyaltyPoints.Points", - "Offers.Listings.MerchantInfo", - "Offers.Listings.Price", - "Offers.Listings.ProgramEligibility.IsPrimeExclusive", - "Offers.Listings.ProgramEligibility.IsPrimePantry", - "Offers.Listings.Promotions", - "Offers.Listings.SavingBasis", - "Offers.Summaries.HighestPrice", - "Offers.Summaries.LowestPrice", - "Offers.Summaries.OfferCount" - ], - "PartnerTag": "booko01-22", - "PartnerType": "Associates", - "Marketplace": "www.amazon.com.au", - "Operation": "SearchItems" - } + def test_configuration_is_correctly_set_using + Paapi.configure do |config| + config.access_key = 'some_key' + config.secret_key = 'some_secret' + config.partner_tag = 'abc' + config.market = :uk + end - headers = %q{Host: webservices.amazon.com.au - X-Amz-Date: 20190903T014126Z - X-Amz-Target: com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems - Content-Encoding: amz-1.0} + c = Paapi::Client.new - request = %q{https://webservices.amazon.com.au/!YW16LTEuMDtjb20uYW1hem9uLnBhYXBpNS52MS5Qcm9kdWN0QWR2ZXJ0aXNpbmdBUEl2MS5TZWFyY2hJdGVtczt7CiAgICAiS2V5d29yZHMiOiAiOTc4MTQ3MzYxOTgxNCIsCiAgICAiUmVzb3VyY2VzIjogWwogICAgICA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JpbWVFbGlnaWJsZSIsCiAgICAgICAgIk9mZmVycy5MaXN0aW5ncy5EZWxpdmVyeUluZm8uU2hpcHBpbmdDaGFyZ2VzIiwKICAgICAgICAiT2ZmZXJzLkxpc3RpbmdzLklzQnV5Qm94V2lubmVyIiwKICAgICAgICAiT2ZmZXJzLkxpc3RpbmdzLkxveWFsdHlQb2ludHMuUG9pbnRzIiwKICAgICAgICAiT2ZmZXJzLkxpc3RpbmdzLk1lcmNoYW50SW5mbyIsCiAgICAgICAgIk9mZmVycy5MaXN0aW5ncy5QcmljZSIsCiAgICAgICAgIk9mZmVycy5MaXN0aW5ncy5Qcm9ncmFtRWxpZ2liaWxpdHkuSXNQcmltZUV4Y2x1c2l2ZSIsCiAgICAgICAgIk9mZmVycy5MaXN0aW5ncy5Qcm9ncmFtRWxpZ2liaWxpdHkuSXNQcmltZVBhbnRyeSIsCiAgICAgICAgIk9mZmVycy5MaXN0aW5ncy5Qcm9tb3Rpb25zIiwKICAgICAgICAiT2ZmZXJzLkxpc3RpbmdzLlNhdmluZ0Jhc2lzIiwKICAgICAgICAiT2ZmZXJzLlN1bW1hcmllcy5IaWdoZXN0UHJpY2UiLAogICAgICAgICJPZmZlcnMuU3VtbWFyaWVzLkxvd2VzdFByaWNlIiwKICAgICAgICAiT2ZmZXJzLlN1bW1hcmllcy5PZmZlckNvdW50IgogICAgXSwKICAgICJQYXJ0bmVyVGFnIjogImJvb2tvMDEtMjIiLAogICAgIlBhcnRuZXJUeXBlIjogIkFzc29jaWF0ZXMiLAogICAgIk1hcmtldHBsYWNlIjogInd3dy5hbWF6b24uY29tLmF1Igp9} - - response = { - "SearchResult": { - "Items": [ - { - "ASIN": "1473619815", - "DetailPageURL": "https://www.amazon.com.au/dp/1473619815?tag=booko01-22&linkCode=osi&th=1&psc=1", - "Images": { - "Primary": { - "Large": { - "Height": 500, - "URL": "https://m.media-amazon.com/images/I/51HoUcxVnFL.jpg", - "Width": 325 - } - } - }, - "ItemInfo": { - "ByLineInfo": { - "Contributors": [ - { - "Locale": "en_AU", - "Name": "Chambers, Becky", - "Role": "Author" - } - ], - "Manufacturer": { - "DisplayValue": "Hodder Paperbacks", - "Label": "Manufacturer", - "Locale": "en_AU" - } - }, - "Classifications": { - "Binding": { - "DisplayValue": "Paperback", - "Label": "Binding", - "Locale": "en_AU" - }, - "ProductGroup": { - "DisplayValue": "Book", - "Label": "ProductGroup", - "Locale": "en_AU" - } - }, - "ContentInfo": { - "Edition": { - "DisplayValue": "1", - "Label": "Edition", - "Locale": "en_AU" - }, - "Languages": { - "DisplayValues": [ - { - "DisplayValue": "English", - "Type": "Published" - }, - { - "DisplayValue": "English", - "Type": "Original Language" - }, - { - "DisplayValue": "English", - "Type": "Unknown" - } - ], - "Label": "Language", - "Locale": "en_AU" - }, - "PagesCount": { - "DisplayValue": 432, - "Label": "NumberOfPages", - "Locale": "en_US" - }, - "PublicationDate": { - "DisplayValue": "2016-03-08T00:00:01Z", - "Label": "PublicationDate", - "Locale": "en_US" - } - }, - "ExternalIds": { - "EANs": { - "DisplayValues": [ - "9781473619814" - ], - "Label": "EAN", - "Locale": "en_US" - }, - "ISBNs": { - "DisplayValues": [ - "1473619815" - ], - "Label": "ISBN", - "Locale": "en_US" - } - }, - "ManufactureInfo": { - "ItemPartNumber": { - "DisplayValue": "23025256", - "Label": "PartNumber", - "Locale": "en_US" - } - }, - "ProductInfo": { - "ItemDimensions": { - "Weight": { - "DisplayValue": 471, - "Label": "Weight", - "Locale": "en_AU", - "Unit": "Grams" - } - }, - "ReleaseDate": { - "DisplayValue": "2016-03-08T00:00:01Z", - "Label": "ReleaseDate", - "Locale": "en_US" - }, - "UnitCount": { - "DisplayValue": 1, - "Label": "NumberOfItems", - "Locale": "en_US" - } - }, - "Title": { - "DisplayValue": "The Long Way to a Small, Angry Planet: Wayfarers 1", - "Label": "Title", - "Locale": "en_AU" - } - }, - "Offers": { - "Listings": [ - { - "Availability": { - "MaxOrderQuantity": 20, - "Message": "In stock.", - "MinOrderQuantity": 1, - "Type": "Now" - }, - "Condition": { - "SubCondition": { - "Value": "New" - }, - "Value": "New" - }, - "DeliveryInfo": { - "IsAmazonFulfilled": true, - "IsFreeShippingEligible": true, - "IsPrimeEligible": true - }, - "Id": "1UP5v2BpBF5QeBZF7Dge7V%2BN3pdmW4ZkvT%2BvE4GABbmz4koGqDSZFDZwQj5o95bZrvKmWdqZPW0SiDhCIOjyAKlRmsz4hYXtSkQLSlwCc77AiiAbX1ja0CBjRVVdgLJ6", - "IsBuyBoxWinner": true, - "MerchantInfo": { - "Id": "ANEGB3WVEVKZB", - "Name": "Amazon AU" - }, - "Price": { - "Amount": 16.41, - "Currency": "AUD", - "DisplayAmount": "$16.41" - }, - "ProgramEligibility": { - "IsPrimeExclusive": false, - "IsPrimePantry": false - }, - "ViolatesMAP": false - } - ], - "Summaries": [ - { - "Condition": { - "Value": "New" - }, - "HighestPrice": { - "Amount": 49.31, - "Currency": "AUD", - "DisplayAmount": "$49.31" - }, - "LowestPrice": { - "Amount": 15.46, - "Currency": "AUD", - "DisplayAmount": "$15.46" - }, - "OfferCount": 18 - } - ] - } - } - ], - "SearchURL": "https://www.amazon.com.au/s?k=9781473619814&rh=p_n_availability%3A-1&tag=booko01-22&linkCode=osi", - "TotalResultCount": 1 - } - } + assert_equal 'abc', c.partner_tag + assert_equal :uk, c.market + assert_equal 'some_key', c.access_key end + + def test_configuration_defaults_are_correct + Paapi.configure do |config| + config.access_key = 'some_key' + config.secret_key = 'some_secret' + end + c = Paapi::Client.new + assert_equal Paapi::DEFAULT_RESOURCES, c.resources + + resources = ['test1', 'test2'] + + Paapi.configure do |config| + config.access_key = 'some_key' + config.secret_key = 'some_secret' + config.resources = resources + end + c = Paapi::Client.new + assert_equal resources, c.resources + end + + end