mirror of
https://github.com/dkam/paapi.git
synced 2025-12-28 07:04:53 +00:00
Update readme
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
`paapi` is a slim wrapper around [Amazon's Product Advertising API 5.0](https://webservices.amazon.com/paapi5/documentation/).
|
||||
|
||||
This gem is under development and currently incomplete.
|
||||
This gem is under heavy development and currently incomplete.
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -24,7 +24,7 @@ Or install it yourself as:
|
||||
|
||||
### Configuration
|
||||
|
||||
The library can be initialised with a Rails initializer such as
|
||||
The library can be configured with an initializer. For Rails, create the file `config/initializers/paapi.rb`
|
||||
|
||||
```ruby
|
||||
Paapi.configure do |config|
|
||||
|
||||
@@ -81,4 +81,9 @@ module Paapi
|
||||
end
|
||||
alias_method :config, :configure
|
||||
end
|
||||
|
||||
def symbolize_keys(hash)
|
||||
Hash[hash.map{|k,v| v.is_a?(Hash) ? [k.to_sym, symbolize_keys(v)] : [k.to_sym, v] }]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -36,20 +36,25 @@ module Paapi
|
||||
|
||||
res = do_request(op: :get_items, payload: payload)
|
||||
|
||||
Response.new(res)
|
||||
# Errors, ItemResults -> Items -> Item
|
||||
return Response.new(res)
|
||||
end
|
||||
|
||||
def get_variations(asin:, **options )
|
||||
payload = { ASIN: asin, Resources: @resources }
|
||||
|
||||
res = do_request(op: :get_variations, payload: payload)
|
||||
|
||||
# Errors, VariationsResult->Items
|
||||
Response.new(res)
|
||||
end
|
||||
|
||||
def search_items(keywords:, **options )
|
||||
search_index = 'All'
|
||||
|
||||
# %i[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 )
|
||||
raise ArgumentError("Missing keywords") unless (options.keys | SEARCH_PARAMS).length.positive?
|
||||
|
||||
search_index = options.dig(:SearchIndex) ||'All'
|
||||
|
||||
payload = { Keywords: keywords, Resources: @resources, ItemCount: 10, ItemPage: 1, SearchIndex: search_index }.merge(options)
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require 'nameable'
|
||||
|
||||
module Paapi
|
||||
class Item
|
||||
attr_accessor :raw
|
||||
@@ -21,12 +23,50 @@ module Paapi
|
||||
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)
|
||||
end
|
||||
|
||||
def contributors
|
||||
get(%w{ItemInfo ByLineInfo Contributors})
|
||||
end
|
||||
|
||||
def contributors_of(kind)
|
||||
contributors&.select { |e| e['Role'] == kind }&.map { |e| Nameable(e['Name'])}
|
||||
end
|
||||
|
||||
def authors
|
||||
a = contributors.select { |e| e['Role'] == 'Author' }.map { |e| Nameable(e['Name'])}
|
||||
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'
|
||||
end
|
||||
|
||||
def release_date
|
||||
@@ -45,6 +85,10 @@ module Paapi
|
||||
get(%w{ItemInfo Features DisplayValues})&.join(' ')
|
||||
end
|
||||
|
||||
def brand
|
||||
get(%w{ItemInfo ByLineInfo Brand DisplayValue})
|
||||
end
|
||||
|
||||
def part_number
|
||||
get(%w{ItemInfo ManufactureInfo ItemPartNumber DisplayValue})
|
||||
end
|
||||
@@ -61,5 +105,9 @@ module Paapi
|
||||
@raw.dig(*keys)
|
||||
end
|
||||
|
||||
def self.to_items(data)
|
||||
data.map {|d| Item.new(d)}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -2,10 +2,41 @@ require 'json'
|
||||
|
||||
module Paapi
|
||||
class Response
|
||||
attr_reader :http_response, :data
|
||||
attr_reader :http_response, :data, :datas, :doc, :items
|
||||
|
||||
def initialize(response)
|
||||
@http_response = response
|
||||
@data = JSON.parse( response.body.to_s )
|
||||
@data = 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 ||= []
|
||||
|
||||
@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)|
|
||||
memo.tap { |m| m[snake_case(k)] = symbolise(v) }
|
||||
end
|
||||
elsif obj.is_a? Array
|
||||
return obj.map { |memo| symbolise(memo) }
|
||||
end
|
||||
obj
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,3 +1,3 @@
|
||||
module Paapi
|
||||
VERSION = '0.0.2'
|
||||
VERSION = '0.0.3'
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user