Last active
December 19, 2015 15:29
-
-
Save eljojo/5976753 to your computer and use it in GitHub Desktop.
Simple Amazon Product Advertisement api client, using Vacuum and Nokogiri.
https://github.com/hakanensari/vacuum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AmazonItem | |
attr_accessor :asin, :url, :manufacturer, :category, :title, \ | |
:ean, :item_number, :model, :lowest_price, :image_url, \ | |
:list_price, :offer_price | |
def initialize(attributes = {}) | |
update_attributes(attributes) | |
end | |
def update_attributes(new_attributes) | |
return update_attributes_from_xml(new_attributes) if new_attributes.respond_to?(:css) | |
new_attributes.each do |key, value| | |
if self.respond_to?("#{key}=".to_sym) then | |
self.send("#{key}=".to_sym, value) unless value.nil? | |
else | |
raise ArgumentError, "unkown :#{key} attribute" | |
end | |
end | |
self | |
end | |
def update_attributes_from_xml(item) | |
return self unless item | |
update_attributes( | |
ean: item.css('ItemAttributes EAN').first.try(:text), | |
item_number: item.css('ItemAttributes ItemPartNumber').first.try(:text), | |
model: item.css('ItemAttributes Model').first.try(:text), | |
lowest_price: get_price(item.css('LowestNewPrice Amount').first), | |
list_price: get_price(item.css('ListPrice Amount').first), | |
offer_price: get_price(item.css('Offer Price Amount').first), | |
image_url: item.css('LargeImage URL').first.try(:text), | |
asin: item.css('ASIN').first.text, | |
url: item.css('DetailPageURL').first.text, | |
manufacturer: item.css('ItemAttributes Manufacturer').first.text, | |
category: item.css('ItemAttributes ProductGroup').first.text, | |
title: item.css('ItemAttributes Title').first.text, | |
) | |
end | |
def lookup | |
return unless asin or ean | |
req = self.class.get_vacuum | |
to_request = %w{AlternateVersions Images ItemAttributes Similarities Offers} | |
res = req.get query: { 'Operation' => 'ItemLookup', | |
'ResponseGroup' => to_request.join(','), | |
'IdType' => asin ? 'ASIN' : 'EAN', | |
'ItemId' => asin || ean, | |
'SearchIndex' => asin ? '' : 'All' } | |
lookup_doc = Nokogiri::XML(res.body) | |
return unless lookup_doc | |
update_attributes_from_xml lookup_doc.css('Item').first | |
end | |
class << self | |
def search(search_query) | |
req = get_vacuum | |
search_query = search_query.join(' ') if search_query.is_a?(Array) | |
res = req.get query: { 'Operation' => 'ItemSearch', | |
'SearchIndex' => 'All', | |
'Keywords' => search_query } | |
search_doc = Nokogiri::XML(res.body) | |
items = search_doc.css('Item') | |
items.map{ |item| new(item) } | |
end | |
def get_vacuum | |
Thread.current[:amazon_vacuum] = begin | |
vacuum = Vacuum.new('DE') | |
vacuum.configure key: ENV['AMAZON_ACCESS_KEY'], | |
secret: ENV['AMAZON_SECRET'], | |
tag: ENV['AMAZON_TAG'] | |
vacuum | |
end | |
end | |
end | |
private | |
def get_price(doc) | |
return unless doc | |
doc.text.to_f / 100 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment