<?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>Netphase &#187; ruby</title>
	<atom:link href="http://blog.netphase.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.netphase.com</link>
	<description>for a connected world</description>
	<lastBuildDate>Mon, 14 Dec 2009 21:40:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AAAP Updated!</title>
		<link>http://blog.netphase.com/2008/12/05/acts_as_amazon_product-update/</link>
		<comments>http://blog.netphase.com/2008/12/05/acts_as_amazon_product-update/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 17:45:02 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Acts As Amazon Product]]></category>
		<category><![CDATA[Netphase News]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[aaap]]></category>
		<category><![CDATA[acts_as_amazon_product]]></category>
		<category><![CDATA[gem]]></category>

		<guid isPermaLink="false">http://www.netphase.com/?p=160</guid>
		<description><![CDATA[It&#8217;s been a while since we have called attention to it, but over the course of the year, our Amazon product integration gem, acts_as_amazon_product, has been improved.
First, we got some participation from David Eisinger to help improve testing and some other critical features. Thanks, David! Recently though, we added some new functionality we had lying [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since we have called attention to it, but over the course of the year, our Amazon product integration gem, <a href="http://github.com/netphase/aaap/tree/master" target="_new">acts_as_amazon_product</a>, has been improved.</p>
<p>First, we got some participation from David Eisinger to help improve testing and some other critical features. Thanks, David! Recently though, we added some new functionality we had lying around in an old plugin called acts_as_amazonable.</p>
<p>The new functionality allows you to load Amazon product attributes into a new or existing model object on your system. You can define which fields are loaded, and which attributes in your model they map to. We are using this functionality on a new double-secret project that we are working on, and it is great. In that project, we are letting our users type in an ISBN code, and we pre-populate a form with all of the Amazon data we want for that product, saving the user from a lot of typing. We also pick up the images, so they don&#8217;t even have  to load an image to sell their item. Pretty cool!</p>
<p>Here&#8217;s how you use aaap&#8217;s new functionality:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'acts_as_amazon_product'</span>
&nbsp;
 <span style="color:#9966CC; font-weight:bold;">class</span> LocalBook <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
   acts_as_amazon_product<span style="color:#006600; font-weight:bold;">&#40;</span>
     <span style="color:#ff3333; font-weight:bold;">:asin</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'isbn'</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
     <span style="color:#ff3333; font-weight:bold;">:access_key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'0123456'</span>, <span style="color:#ff3333; font-weight:bold;">:associate_tag</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'assoc-20'</span>,
     <span style="color:#ff3333; font-weight:bold;">:auto_load_fields</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#123;</span>
         <span style="color:#ff3333; font-weight:bold;">:title</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
         <span style="color:#ff3333; font-weight:bold;">:isbn</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'asin'</span>,
         <span style="color:#ff3333; font-weight:bold;">:publisher_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'manufacturer'</span>,
         <span style="color:#ff3333; font-weight:bold;">:author</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'author'</span><span style="color:#006600; font-weight:bold;">&#125;</span>
   <span style="color:#006600; font-weight:bold;">&#41;</span>
 <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The keys in the auto_load_fields hash are the fields in your object, and the values are their Amazon equivalents. In the true Rails convention over configuration spirit, we have included some of the most popular auto load fields as defaults. If your object contains the following attributes, they will automatically be loaded with their Amazon equivalents:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#ff3333; font-weight:bold;">:auto_load_fields</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#123;</span>
     <span style="color:#ff3333; font-weight:bold;">:title</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
     <span style="color:#ff3333; font-weight:bold;">:isbn</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'asin'</span>,
     <span style="color:#ff3333; font-weight:bold;">:publisher_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'manufacturer'</span>,
     <span style="color:#ff3333; font-weight:bold;">:author</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'author'</span>,
     <span style="color:#ff3333; font-weight:bold;">:binding</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'binding'</span>,
     <span style="color:#ff3333; font-weight:bold;">:list_price</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'listprice/amount'</span>,
     <span style="color:#ff3333; font-weight:bold;">:pages</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'numberofpages'</span>,
     <span style="color:#ff3333; font-weight:bold;">:description</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'content'</span>,
     <span style="color:#ff3333; font-weight:bold;">:small_image_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'smallimage/url'</span>,
     <span style="color:#ff3333; font-weight:bold;">:medium_image_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'mediumimage/url'</span>,
     <span style="color:#ff3333; font-weight:bold;">:large_image_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'largeimage/url'</span>,
     <span style="color:#ff3333; font-weight:bold;">:detail_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'detailpageurl'</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Don&#8217;t worry if your object does not have some or most of these fields; Acts as Amazon Product is smart enough to check to see if the method exists before attempting to load up the Amazon value.</p>
<p>Now, if by some coincidence you have some fields in your local object that have the same name as our auto_load defaults, you can choose to specifically ignore those fields using, you guessed it, :ignore_fields.  :ignore_fields is a simple array of fields in your object that you do NOT want to be replaced with the Amazon equivalent.  You would specify them like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">acts_as_amazon_product<span style="color:#006600; font-weight:bold;">&#40;</span>
    <span style="color:#ff3333; font-weight:bold;">:asin</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'isbn'</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
    <span style="color:#ff3333; font-weight:bold;">:access_key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'0123456'</span>, <span style="color:#ff3333; font-weight:bold;">:associate_tag</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'assoc-20'</span>,
    <span style="color:#ff3333; font-weight:bold;">:ignore_fields</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:small_image_url</span>, <span style="color:#ff3333; font-weight:bold;">:medium_image_url</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>In this instance, the object in question would load Amazon info for any of the default fields that the object has, with the exception of the small_image_url and medium_image_url values.</p>
<p>To load the Amazon values into your object for local storage, you may call the following methods:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">load_from_amazon<span style="color:#006600; font-weight:bold;">&#40;</span>title_or_asin, by_title = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
load_from_amazon!<span style="color:#006600; font-weight:bold;">&#40;</span>title_or_asin, by_title = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>The two methods do exactly the same thing, except the first simply returns a new, unsaved object with its Amazon-like fields loaded with their appropriate Amazon values.  The second version (load_from_amazon!) goes one step further and saves the object for good measure.</p>
<p>You may pass either an isbn (asin) or a title in.  If you are passing a title, you need to specify that you are passing a title by also passing true as the second parameter so we will perform an item_search rather than an item_lookup. Note that passing a title is risky business, since currently the code will take the first result returned and load that into your object. So unless your title is unique, I would recommend sticking with the isbn (asin) value.</p>
<p>Since that is the default, all you need to do would be something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@local_book</span> = LocalBook.<span style="color:#9900CC;">load_from_amazon</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'1590598415'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>If your object was similar to the LocalBook object in our example, for instance, you would not need to specify the auto_load_fields in the acts_as call at all.  The title, isbn, publisher_name and author methods would automatically be called once you call load_from_amazon, and their values populated appropriately.</p>
<p>It is important to note that if you call load_from_amazon with an invalid asin, you will simply get back an empty object.</p>
<p>Now, calling load_from_amazon still allows you to take advantage of the other Acts as Amazon features, like the dynamic loading of Amazon attributes on the fly by using the various amazon.foo methods as it always has.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2008/12/05/acts_as_amazon_product-update/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
