Skip to content

Instantly share code, notes, and snippets.

@frankie-loves-jesus
Created July 27, 2014 03:33
Show Gist options
  • Save frankie-loves-jesus/f8d9d03364f6acc1aea6 to your computer and use it in GitHub Desktop.
Save frankie-loves-jesus/f8d9d03364f6acc1aea6 to your computer and use it in GitHub Desktop.

Vacuum with Rails

Goal: Displaying products from the Amazon Product Advertising API.

Status:

NOT WORKING

undefined method `any?' for nil:NilClass
Extracted source (around line #2):
  <h1>Products from the Amazon API</h1>
  <% if @products.any? %>
    <% @products.each do |product| %>
      <div class="product">
        <%= link_to image_tag(product.image.url), product.url %>
class MainController < ApplicationController
def index
@products = ThirdPartyProducts.fetch 'Books', 'Ruby on Rails'
end
end
class ThirdPartyProducts
def self.fetch category, topic
Amazon.fetch(category, topic) # + Other.fetch(category, topic)
end
end
class ThirdPartyProducts::Amazon
def self.fetch category, topic
request = Vacuum.new('GB')
# TODO: Move to `config/secrets.yml`
request.configure(
aws_access_key_id: '',
aws_secret_access_key: '',
associate_tag: ''
)
params = {
'SearchIndex' => category,
'Keywords'=> topic,
'ResponseGroup' => "ItemAttributes,Images"
}
raw_products = request.item_search(query: params)
hashed_products = raw_products.to_h
@products = []
hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
product = OpenStruct.new
product.name = item['ItemAttributes']['Title']
product.url = item['DetailPageURL']
product.image_url = item['LargeImage']['URL']
@products << product
end
end
end
<h1>Products from the Amazon API</h1>
<% if @products.any? %>
<% @products.each do |product| %>
<div class="product">
<%= link_to image_tag(product.image_url), product.url %>
<%= link_to product.name, product.url %>
</div>
<% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment