techdreams techdreams - 7 months ago 83
Ruby Question

fetch ItemId in Amazon Product Api using Rails Vacuum gem

I am integrating Amazon Product Api in my rails app. I am using the vacuum gem for this integration. There are two main pages. I am fetching the products through this code.

requestd = Vacuum.new

requestd.configure(
aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
associate_tag: ENV['ASSOCIATE_TAG']
)

response = requestd.item_search(
query: {
'SearchIndex' => @category.search_index,
'Keywords' => @category.keyword,
'ResponseGroup' => "ItemAttributes,Images"
}
)

hashed_products = response.to_h

@products = []

hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
product = OpenStruct.new
product.name = item['ItemAttributes']['Title']
product.price = item['ItemAttributes']['ListPrice']['FormattedPrice'] if item['ItemAttributes']['ListPrice']
product.url = item['DetailPageURL']
product.feature = item['ItemAttributes']['Feature']
product.image_url = item['LargeImage']['URL'] if item['LargeImage']
product.link = item['ItemLinks']['ItemLink'][5]['URL']
@products << product
end


It gives ten products with matching request. But when I want to go to the detail page of a product in my app and show its respective details in my app I need ItemId of that product as given in the vaccum docs.

response = request.item_lookup(
query: {
'ItemId' => '0679753354'
}
)


But I am not able to fetch this Id. When I write
ItemId
or
ItemIds
in the response group I do not get that although that is an attribute that could be written in ResponseGroup as per the Amazon Docs. I need that ItemId for item loopup operation to be performed so that I get Reviews and other details respective to that product separately.

Answer

I'm not familiar with Ruby, but the approach should be the same for every programming language. The ItemId attribute you are looking for is called ASIN and you should be able to fetch it in the following way:

product.asin = item['ASIN']
Comments