This commit is contained in:
Dan Milne
2019-10-03 15:14:25 +10:00
parent 0439072b52
commit b09d895ff2
4 changed files with 39 additions and 33 deletions

View File

@@ -14,13 +14,14 @@ module Paapi
partner_type: DEFAULT_PARTNER_TYPE
)
raise ArgumentError unless MARKETPLACES.keys.include?(market.to_sym)
@access_key = access_key
@secret_key = secret_key
@partner_type = partner_type
@resources = resources unless resources.nil?
self.market = market
@partner_tag = partner_tag if !partner_tag.nil?
@partner_tag = partner_tag if !partner_tag.nil?
end
def market=(a_market)
@@ -44,12 +45,11 @@ module Paapi
payload = { ASIN: asin, Resources: @resources }
res = do_request(op: :get_variations, payload: payload)
# Errors, VariationsResult->Items
Response.new(res)
end
# TODO: Currently we assume Keywords, but we need one of the follow: [Keywords Actor Artist Author Brand Title ]
def search_items(keywords: nil, **options )
raise ArgumentError("Missing keywords") unless (options.keys | SEARCH_PARAMS).length.positive?

View File

@@ -2,9 +2,13 @@ require 'nameable'
module Paapi
class Item
attr_accessor :json
attr_accessor :hash
def initialize(data)
@json = data
@hash = data
end
def listings
get(['Offers', 'Listings']).map {|d| Listing.new(d)}
end
def asin
@@ -22,20 +26,20 @@ module Paapi
def title
get(%w{ItemInfo Title DisplayValue})
end
def manufacturer
get(%w{ItemInfo ByLineInfo Manufacturer DisplayValue})
end
def publisher
manufacturer
end
def publication_date
d = get(%w{ItemInfo ContentInfo PublicationDate DisplayValue})
return d.nil? ? nil : Date.parse(d)
end
def release_date
d = get(%w{ItemInfo ProductInfo ReleaseDate DisplayValue})
return d.nil? ? nil : Date.parse(d)
@@ -44,29 +48,33 @@ module Paapi
def contributors
get(%w{ItemInfo ByLineInfo Contributors})
end
def contributors_of(kind)
contributors&.select { |e| e['Role'] == kind }&.map { |e| Nameable(e['Name'])}
contributors&.select { |e| e['Role'] == kind.to_s.titlecase }&.map { |e| Nameable(e['Name'])}
end
def actors
contributors_of 'Actor'
end
def artists
contributors_of 'Artist'
end
def authors
contributors_of 'Author'
end
def illustrators
contributors_of 'Illustrator'
end
def actors
contributors_of 'Actor'
end
def narrators
contributors_of 'Narrator'
end
def publishers
contributors_of 'Publisher'
contributors_of 'Publisher'
end
def release_date
@@ -84,7 +92,7 @@ module Paapi
def features
get(%w{ItemInfo Features DisplayValues})&.join(' ')
end
def brand
get(%w{ItemInfo ByLineInfo Brand DisplayValue})
end
@@ -102,12 +110,12 @@ module Paapi
end
def get(keys)
@json.dig(*keys)
@hash.dig(*keys)
end
def self.to_items(data)
data.map {|d| Item.new(d)}
end
end
end

View File

@@ -2,30 +2,31 @@ require 'json'
module Paapi
class Response
attr_reader :http_response, :data, :datas, :doc, :items
attr_reader :http_response, :json, :datas, :doc, :items
def initialize(response)
@http_response = response
@data = JSON.parse(response.body.to_s)
@json = JSON.parse(response.body.to_s)
@datas = symbolise(JSON.parse(response.body.to_s))
@doc = JSON.parse(@datas.to_json, object_class: OpenStruct)
@items_data = @data.dig('ItemsResult', 'Items')
@items_data ||= @data.dig('SearchResult', 'Items')
@items_data = @json.dig('ItemsResult', 'Items')
@items_data ||= @json.dig('SearchResult', 'Items')
@items_data ||= []
@items = @items_data.map {|d| Item.new(d)}
end
def snake_case(s)
return s.downcase if s.match(/\A[A-Z]+\z/)
s.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
gsub(/([a-z])([A-Z])/, '\1_\2').
downcase
end
def symbolise(obj)
if obj.is_a? Hash
return obj.inject({}) do |memo, (k, v)|
@@ -37,6 +38,5 @@ module Paapi
obj
end
end
end

View File

@@ -37,7 +37,6 @@ class PaapiTest < Minitest::Test
c.market = :gb
assert_equal orig_tag, c.partner_tag
end
def test_configuration_is_correctly_set_using
@@ -74,5 +73,4 @@ class PaapiTest < Minitest::Test
assert_equal resources, c.resources
end
end