<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The True Tribe &#187; Amazon</title>
	<atom:link href="http://www.thetruetribe.com/tag/amazon/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thetruetribe.com</link>
	<description>Vroom! That&#039;s us leaving IE in the dust.</description>
	<lastBuildDate>Fri, 22 Jan 2010 23:34:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using the Amazon ECS Gem In Your Rails App</title>
		<link>http://www.thetruetribe.com/2010/01/amazon-ecs-gem-in-rails/</link>
		<comments>http://www.thetruetribe.com/2010/01/amazon-ecs-gem-in-rails/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 08:06:00 +0000</pubDate>
		<dc:creator>jdempcy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Amazon]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://www.thetruetribe.com/?p=292</guid>
		<description><![CDATA[Amazon.com offers a tremendous amount of web services to developers looking to display their products. They benefit from this because developers make all sorts of cool apps for browsing Amazon. For example, nifty 3D Flash experience CoolIris (formerly known as &#8216;PicLens&#8217;) added Amazon functionality to their product. It&#8217;s a win-win for developers and Amazon because [...]]]></description>
			<content:encoded><![CDATA[<p>Amazon.com offers a tremendous amount of web services to developers looking to display their products. They benefit from this because developers make all sorts of cool apps for browsing Amazon. For example, nifty 3D Flash experience <a href="http://www.cooliris.com/tab/#c=322&amp;f=Channels">CoolIris</a> (formerly known as &#8216;PicLens&#8217;) <a href="http://www.readwriteweb.com/archives/piclens_review_videos.php">added Amazon functionality to their product</a>. It&#8217;s a win-win for developers and Amazon because it drives traffic to the site while paying developers referral fees.<span id="more-292"></span></p>
<p>If you&#8217;re a Rails developer looking to tap into Amazon&#8217;s massive warehouse of product data, you&#8217;re in luck. <a href="http://www.pluitsolutions.com/projects/amazon-ecs">Pluit Solutions</a> has released the amazon-ecs gem exactly for this purpose. It&#8217;s as easy to install as:</p>
<pre>gem install amazon-ecs</pre>
<p>If that doesn&#8217;t work for you, you can always  <a href="http://github.com/jugend/amazon-ecs">download it from GitHub</a> then install it locally. Or you can clone it at this URL: git://github.com/jugend/amazon-ecs.git</p>
<p>It&#8217;s pretty straightforward to set up. The first thing you&#8217;ll need to do is get an Amazon Web Services account if you don&#8217;t already have one. You can <a href="http://aws.amazon.com/">sign up for AWS here</a>.</p>
<p>You&#8217;ll need to know your developer token to connect to AWS. They recently made it so you need to specify a secret token as well. It would be something like this in your <code>/config/environment.rb</code> file:</p>
<pre># Be sure to restart your server when you modify this file

# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
  config.gem "amazon-ecs", :lib =&gt; "amazon/ecs"
  config.time_zone = 'UTC'
  config.frameworks -= [ :active_record ]
end

require 'rubygems'
require 'amazon/ecs'
Amazon::Ecs.options = {:aWS_access_key_id =&gt; '************************', :aWS_secret_key =&gt; '**********************'}</pre>
<p>Just replace the asterisks with your actual AWS public and private keys and you&#8217;re set!</p>
<p>Say you want to run a search at the /amazon/ controller&#8217;s index action. Let&#8217;s generate the controller now:</p>
<pre>ruby script/generate controller Amazon</pre>
<p>Now in the /app/controllers/amazon_controller.rb file you can define the index action like so:</p>
<pre>class AmazonController &lt; ApplicationController
  def index
  end
end</pre>
<p>This is where we&#8217;ll be putting the service call to AWS to get our data back.</p>
<p>The next step is to add in a hard coded search query. You can make this based on user input later, but for now, let&#8217;s just search for &#8216;Ghostbusters&#8217; and see what we get back.</p>
<pre>class AmazonController &lt; ApplicationController
  def index
    query = params[:q]
    page = params[:p]
    @results = Amazon::Ecs.item_search('Ghostbusters', :response_group =&gt; 'Medium', :search_index =&gt; 'DVD', :item_page =&gt; page).items
   end
end</pre>
<p>To display the data, we&#8217;ll need to make a view for it. Let&#8217;s create a new file in <code>/views/amazon</code> called <code>index.html.erb</code>.</p>
<p>The view can look like this:</p>
<pre>  (
    {
    results:
      [
        &lt;% @results.each_with_index do |result, i| %&gt;
          {
            title: "&lt;%=h result.get('title') %&gt;",
            detailpageurl: "&lt;%=h result.get('detailpageurl') %&gt;",
            imageurl: "&lt;%=h result.get('mediumimage/url') %&gt;",
            largeimageurl: "&lt;%=h result.get('largeimage/url') %&gt;"
          }&lt;% if i != @results.length%&gt;,&lt;% end %&gt;
        &lt;% end %&gt;
      ]
    }
  )</pre>
<p>What&#8217;s that, you might ask? It&#8217;s just simple JSON actually. We get the data back as pure Ruby objects but for Ajax applications it&#8217;s much easier to work with as JSON. You can just as easily make it HTML, but I thought you might want to see the data displayed in JSON first.</p>
<p>Fire up the Ruby server with a <code>ruby script/server</code> call and hit up <code>localhost:3000/Amazon</code> in a browser. You should see a JSON response with a bunch of data pertaining to our query, namely, the movie Ghostbusters.</p>
<p>If you were to display this data as images and links on a page, you could do something like this:</p>
<pre>&lt;% @results.each_with_index do |result, i| %&gt;
	&lt;%= content_tag :li, link_to(image_tag(result.get('mediumimage/url'), {:alt =&gt; result.get('title')}), result.get('detailpageurl')) %&gt;
&lt;% end %&gt;</pre>
<p>If you change the view to be HTML it should render all of the images inside of <code>li</code> elements in the page. The thing is, it&#8217;s still always displaying Ghostbusters data. Now we can make it pull in from a query string so it dynamically displays data instead:</p>
<pre>class AmazonController &lt; ApplicationController
  def index
    query = params[:q]
    @results = Amazon::Ecs.item_search(query, :response_group =&gt; 'Medium', :search_index =&gt; 'DVD', :item_page =&gt; page).items
   end
end</pre>
<p>Now if you go to <code>localhost:3000/amazon?q=2001+a+space+odyssey</code> you should see results pertaining to that masterpiece instead.</p>
<p>And next we can even add a page specification:</p>
<pre>class AmazonController &lt; ApplicationController
  def index
    query = params[:q]
    page = params[:p]
    @results = Amazon::Ecs.item_search(query, :response_group =&gt; 'Medium', :search_index =&gt; 'DVD', :item_page =&gt; page).items
   end
end</pre>
<p>At this point you can get more than one page of results by specifying the q parameter, e.g.: <code>localhost:3000/amazon?q=2001+a+space+odyssey&amp;p=2</code> returns results 11-20.</p>
<p>It&#8217;s hard coded to searching in &#8216;DVD&#8217; right now. If we change it like so we can search everything:</p>
<pre>class AmazonController &lt; ApplicationController
  def index
    query = params[:q]
    page = params[:p]
    @results = Amazon::Ecs.item_search(query, :response_group =&gt; 'Medium', :item_page =&gt; page).items
   end
end</pre>
<p>I just removed the <code>:search_index</code> specification in the service call. It&#8217;s the same thing as specifying <code>:search_index =&gt; 'All'</code> so you can do that instead if you wish.</p>
<p>Valid search indexes are:</p>
<pre> 	%w[ All Apparel Automotive Baby Beauty Blended Books Classical DigitalMusic DVD Electronics ForeignBooks GourmetFood Grocery HealthPersonalCare Hobbies HomeGarden HomeImprovement Industrial Jewelry KindleStore Kitchen Magazines Merchants Miscellaneous MP3Downloads Music MusicalInstruments MusicTracks OfficeProducts OutdoorLiving PCHardware PetSupplies Photo Shoes SilverMerchants Software SoftwareVideoGames SportingGoods Tools Toys UnboxVideo VHS Video VideoGames Watches Wireless WirelessAccessories ]</pre>
<p>Enjoy and best of luck with your Amazon app!</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.thetruetribe.com%2F2010%2F01%2Famazon-ecs-gem-in-rails%2F&amp;linkname=Using%20the%20Amazon%20ECS%20Gem%20In%20Your%20Rails%20App"><img src="http://www.thetruetribe.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.thetruetribe.com/2010/01/amazon-ecs-gem-in-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
