phoet / asin

:books: :package: Amazon Simple INterface - Support for ItemLookup, SimilarityLookup, Search, BrowseNode and Cart Operations
http://asin.herokuapp.com/
167 stars 59 forks source link

How can I pull image url(s) ? #41

Closed avadhbsd closed 9 years ago

avadhbsd commented 9 years ago

Hey, thanks for making this gem. newbie here.

I was able to get up and running with the gem and able to fetch the Item details. But "item_attributes" does not seem to have images to it. Is there a way I can pull images ?

Thanks again.

kurtfunai commented 9 years ago

Hi @avadhbsd, funny enough, one of my first rails projects also happened to use asin. I've open sourced it, and you can see how I am grabbing the images out here: https://github.com/kurtfunai/Giftsmash/blob/master/app/models/amazon_affiliate.rb#L6-L16

Disclaimer: my code is pretty old, but it does still appear to be working

You can find more about the Amazon Product API endpoints here: http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_response_elements.html

avadhbsd commented 9 years ago

@kurtfunai thanks so much for the help. I can see a lot of content when i render the @item instance variable. but still dont know how to pull individual attributes. ie.

@detail_page_url = @client.lookup(asin).first.item_attributes.detail_page_url  {this does not seem to work}

@item = @client.lookup(asin)

Thanks again for the help.

kurtfunai commented 9 years ago

Whoops, my apologies, it appears that I was using an old version of the gem in that project.

Using the new gem, you are able to grab images like so:

items = client.lookup '1430218150'
items.first.large_image.url
# there are other sizes like small, medium, thumbnail, etc
kurtfunai commented 9 years ago

Hash::Rash is the gem that is used to format and make the data accessible, see more here: https://github.com/tcocca/rash

Basically it takes the response from the Amazon Product API and formats it into a Ruby object (more or less).

An easy way to figure out how to access certain data is like this:

items = client.lookup '1430218150'
item = items.first
# this will give you a big scary return type that looks something like:
#=> [#<Hashie::Rash asin="1430218150" detail_page_url="http..... etc

# To see what is information is available here, you can do something like:
item.keys
#=> ["asin", "detail_page_url", "item_links", "sales_rank", "small_image", "medium_image", "large_image", "image_sets", "item_attributes", "offer_summary", "editorial_reviews"]

# You can use any of those keys to get more information, like so:
item.small_image
#=> #<Hashie::Rash height="75" url="http://ecx.images-amazon.com/images/I/41BEdnL69qL._SL75_.jpg" width="57">

# And we can use that method again:
item.small_image.keys
#=> ["url", "height", "width"]

item.small_image.url
=> "http://ecx.images-amazon.com/images/I/41BEdnL69qL._SL75_.jpg"
kurtfunai commented 9 years ago

@avadhbsd Regarding your question about detail_page_url, using the strategy above, you can see how to grab this:

item.keys
=> ["asin", "detail_page_url", "item_links", "sales_rank", "small_image", "medium_image", "large_image", "image_sets", "item_attributes", "offer_summary", "editorial_reviews"]

item.detail_page_url
=> "http://www.amazon.com/Learn-Objective-C-Mac-Series/dp/1430218150%3FSubscriptionId%3DAKIAJQXNACBNYYT47F5Q%26tag%3Dgiftsmash-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D1430218150"
avadhbsd commented 9 years ago

@kurtfunai Awesome! thanks so much for helping me out. I am giving it a try.

You are da man!! Thanks again!

kurtfunai commented 9 years ago

@avadhbsd happy to help :smile_cat: