Class: AWS::PAAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/pa_api.rb

Defined Under Namespace

Classes: Market

Constant Summary collapse

MARKETS =
{
	au: Market.new('webservices.amazon.com.au', 'us-west-2'),
	br: Market.new('webservices.amazon.com.br'  'us-east-1'),
	ca: Market.new('webservices.amazon.ca',     'us-east-1'),
	fr: Market.new('webservices.amazon.fr',     'eu-west-1'),
	de: Market.new('webservices.amazon.de',     'eu-west-1'),
	in: Market.new('webservices.amazon.in',     'eu-west-1'),
	it: Market.new('webservices.amazon.it',     'eu-west-1'),
	jp: Market.new('webservices.amazon.co.jp',  'us-west-2'),
	mx: Market.new('webservices.amazon.com.mx', 'us-east-1'),
	es: Market.new('webservices.amazon.es',     'eu-west-1'),
	tr: Market.new('webservices.amazon.com.tr', 'eu-west-1'),
	ae: Market.new('webservices.amazon.ae',     'eu-west-1'),
	uk: Market.new('webservices.amazon.co.uk',  'eu-west-1'),
	us: Market.new('webservices.amazon.com',    'us-east-1'),
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(access_key, secret_key, partner_tag) ⇒ PAAPI

Returns a new instance of PAAPI.



25
26
27
# File 'lib/aws/pa_api.rb', line 25

def initialize(access_key, secret_key, partner_tag)
	@access_key, @secret_key, @partner_tag = access_key, secret_key, partner_tag
end

Instance Method Details

#get_items(asin, locale) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/aws/pa_api.rb', line 29

def get_items(asin, locale)
	asin = isbn13to10(asin) if asin.length == 13
	payload = {
		"PartnerTag" => @partner_tag,
		"PartnerType" => "Associates",
		"Marketplace" => MARKETS[locale].host.sub('webservices', 'www'),
		"ItemIds" => [asin],
		"Resources" => [
			"Images.Primary.Small",
			"Images.Primary.Medium",
			"Images.Primary.Large",
			"ItemInfo.ByLineInfo",
			"ItemInfo.Title",
			"Offers.Listings.Price"
		]
	}.to_json
	time_stamp = Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
	signature = AWS::SigV4.signature(@secret_key, time_stamp, MARKETS[locale].region, MARKETS[locale].host, payload)
	authorization = "AWS4-HMAC-SHA256 Credential=#{@access_key}/#{time_stamp[0,8]}/#{MARKETS[locale].region}/ProductAdvertisingAPI/aws4_request, SignedHeaders=content-encoding;host;x-amz-content-sha256;x-amz-date;x-amz-target, Signature=#{signature}"

	headers = {
		"X-Amz-Target" => "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems",
		"Content-Encoding" => "amz-1.0",
		"Host" => MARKETS[locale].host,
		"X-Amz-Date" => time_stamp,
		"X-Amz-Content-Sha256" => OpenSSL::Digest::SHA256.hexdigest(payload),
		"Authorization" => authorization,
		"Content-Type" => "application/json; charset=utf-8"
	}
	uri = URI("https://#{MARKETS[locale].host}/paapi5/getitems")
	http = Net::HTTP.new(uri.host, uri.port)
	http.use_ssl = true
	response = http.post(uri.path, payload, headers)
	response.value # raise on errors
	return response.body
end