From b09d895ff29a96c1f78d13e6bdc634213f7cb301 Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Thu, 3 Oct 2019 15:14:25 +1000 Subject: [PATCH] Rubocop --- lib/paapi/client.rb | 6 +++--- lib/paapi/item.rb | 48 +++++++++++++++++++++++++------------------ lib/paapi/response.rb | 16 +++++++-------- test/paapi_test.rb | 2 -- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/lib/paapi/client.rb b/lib/paapi/client.rb index f44df07..29b1054 100644 --- a/lib/paapi/client.rb +++ b/lib/paapi/client.rb @@ -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? diff --git a/lib/paapi/item.rb b/lib/paapi/item.rb index 59abfc3..13e7953 100644 --- a/lib/paapi/item.rb +++ b/lib/paapi/item.rb @@ -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 \ No newline at end of file diff --git a/lib/paapi/response.rb b/lib/paapi/response.rb index 6e12706..d059e09 100644 --- a/lib/paapi/response.rb +++ b/lib/paapi/response.rb @@ -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 \ No newline at end of file diff --git a/test/paapi_test.rb b/test/paapi_test.rb index 5972271..db5d185 100644 --- a/test/paapi_test.rb +++ b/test/paapi_test.rb @@ -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