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,6 +14,7 @@ module Paapi
partner_type: DEFAULT_PARTNER_TYPE partner_type: DEFAULT_PARTNER_TYPE
) )
raise ArgumentError unless MARKETPLACES.keys.include?(market.to_sym) raise ArgumentError unless MARKETPLACES.keys.include?(market.to_sym)
@access_key = access_key @access_key = access_key
@secret_key = secret_key @secret_key = secret_key
@partner_type = partner_type @partner_type = partner_type
@@ -49,7 +50,6 @@ module Paapi
Response.new(res) Response.new(res)
end end
# TODO: Currently we assume Keywords, but we need one of the follow: [Keywords Actor Artist Author Brand Title ] # TODO: Currently we assume Keywords, but we need one of the follow: [Keywords Actor Artist Author Brand Title ]
def search_items(keywords: nil, **options ) def search_items(keywords: nil, **options )
raise ArgumentError("Missing keywords") unless (options.keys | SEARCH_PARAMS).length.positive? raise ArgumentError("Missing keywords") unless (options.keys | SEARCH_PARAMS).length.positive?

View File

@@ -2,9 +2,13 @@ require 'nameable'
module Paapi module Paapi
class Item class Item
attr_accessor :json attr_accessor :hash
def initialize(data) def initialize(data)
@json = data @hash = data
end
def listings
get(['Offers', 'Listings']).map {|d| Listing.new(d)}
end end
def asin def asin
@@ -46,7 +50,15 @@ module Paapi
end end
def contributors_of(kind) 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 end
def authors def authors
@@ -57,10 +69,6 @@ module Paapi
contributors_of 'Illustrator' contributors_of 'Illustrator'
end end
def actors
contributors_of 'Actor'
end
def narrators def narrators
contributors_of 'Narrator' contributors_of 'Narrator'
end end
@@ -102,7 +110,7 @@ module Paapi
end end
def get(keys) def get(keys)
@json.dig(*keys) @hash.dig(*keys)
end end
def self.to_items(data) def self.to_items(data)

View File

@@ -2,17 +2,17 @@ require 'json'
module Paapi module Paapi
class Response class Response
attr_reader :http_response, :data, :datas, :doc, :items attr_reader :http_response, :json, :datas, :doc, :items
def initialize(response) def initialize(response)
@http_response = 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)) @datas = symbolise(JSON.parse(response.body.to_s))
@doc = JSON.parse(@datas.to_json, object_class: OpenStruct) @doc = JSON.parse(@datas.to_json, object_class: OpenStruct)
@items_data = @data.dig('ItemsResult', 'Items') @items_data = @json.dig('ItemsResult', 'Items')
@items_data ||= @data.dig('SearchResult', 'Items') @items_data ||= @json.dig('SearchResult', 'Items')
@items_data ||= [] @items_data ||= []
@items = @items_data.map {|d| Item.new(d)} @items = @items_data.map {|d| Item.new(d)}
@@ -21,6 +21,7 @@ module Paapi
def snake_case(s) def snake_case(s)
return s.downcase if s.match(/\A[A-Z]+\z/) return s.downcase if s.match(/\A[A-Z]+\z/)
s.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). s.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
gsub(/([a-z])([A-Z])/, '\1_\2'). gsub(/([a-z])([A-Z])/, '\1_\2').
downcase downcase
@@ -37,6 +38,5 @@ module Paapi
obj obj
end end
end end
end end

View File

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