Commit 06a03905

mo khan <mo@mokhan.ca>
2014-10-13 18:48:45
create amazon product abstraction to parse the xml response.
1 parent d9bedf4
Changed files (2)
app
services
infrastructure
views
admin
app/services/infrastructure/amazon_api.rb
@@ -7,6 +7,38 @@ class AmazonAPI
 
   def search(query)
     return [] if query.blank?
-    client.search(Keywords: query, SearchIndex: :Kitchen, Sort: :salesrank, Availability: :Available, MerchantId: :Amazon)
+    results = client.search(Keywords: query, SearchIndex: :Kitchen, Sort: :salesrank, Availability: :Available, MerchantId: :Amazon)
+    results.map { |x| AmazonProduct.new(x) }
+  end
+end
+
+class AmazonProduct
+  attr_reader :item
+  delegate :asin, :detail_page_url, :item_attributes, :item_links, to: :item
+
+  def initialize(item)
+    @item = item
+  end
+
+  def title
+    item.item_attributes.title
+  end
+
+  def url
+    item.detail_page_url
+  end
+
+  def manufacturer
+    item.item_attributes.manufacturer
+  end
+
+  def category
+    item.item_attributes.product_group
+  end
+
+  def links
+    item.item_links.item_link.map do |link|
+      OpenStruct.new(description: link.description, url: link.url)
+    end
   end
 end
app/views/admin/products/index.html.erb
@@ -13,13 +13,13 @@
       <tbody>
       <% @products.each do |product| %>
         <tr>
-          <td><%= link_to product.asin, product.detail_page_url %></td>
+          <td><%= link_to product.asin, product.url %></td>
           <td>
-            <%= link_to product.item_attributes.title, product.detail_page_url %> is made by <%= product.item_attributes.manufacturer %> in category <%= product.item_attributes.product_group %>
+            <%= link_to product.title, product.url %> is made by <%= product.manufacturer %> in category <%= product.category %>
           </td>
           <td>
             <ul>
-              <% product.item_links.item_link.each do |link| %>
+              <% product.links.each do |link| %>
                 <li><%= link_to link.description, link.url %></li>
               <% end %>
             </ul>