public
Description: Acts as Amazon Product
Clone URL: git://github.com/netphase/aaap.git
Search Repo:
Accepts search_index parameter to work better with Magazines (patch from 
Parker Morse)
scottned (author)
Thu Apr 24 11:47:27 -0700 2008
commit  48a80e7bedbb0d5f24a31b93278419da681207bd
tree    15f76c2eabce2e6e9ceaa210fb60de04c06abc27
parent  d797ef9d7fea6f2512ee8db7c701910ca9bcaf91
...
21
22
23
24
 
 
25
26
27
28
29
30
31
 
 
32
33
 
34
35
36
...
51
52
53
 
54
55
56
 
57
58
59
60
61
62
63
64
 
 
65
66
67
 
68
69
70
...
21
22
23
 
24
25
26
27
28
29
30
31
 
32
33
34
 
35
36
37
38
...
53
54
55
56
57
58
 
59
60
61
62
63
64
65
 
 
66
67
68
69
 
70
71
72
73
0
@@ -21,16 +21,18 @@ module Netphase
0
             :asin => 'asin',
0
             :name => 'name',
0
             :access_key => ENV['AMAZON_ACCESS_KEY_ID'],
0
- :associate_tag => ENV['AMAZON_ASSOCIATE_TAG']
0
+ :associate_tag => ENV['AMAZON_ASSOCIATE_TAG'],
0
+ :search_index => 'Books'
0
           }
0
           options = defaults.merge options
0
 
0
           Amazon::Ecs.options = {:aWS_access_key_id => options[:access_key], :associate_tag => options[:associate_tag] }
0
 
0
           write_inheritable_attribute(:amazon_asin, options[:asin])
0
- write_inheritable_attribute(:amazon_name, options[:name])
0
+ write_inheritable_attribute(:amazon_name, options[:name])
0
+ write_inheritable_attribute(:amazon_search_index, options[:search_index])
0
           write_inheritable_attribute(:amazon_associate_key, options[:associate_key])
0
- class_inheritable_reader :amazon_asin, :amazon_name, :amazon_associate_key
0
+ class_inheritable_reader :amazon_asin, :amazon_name, :amazon_search_index, :amazon_associate_key
0
           
0
           has_one :amazon_product, :as => :amazonable #, :dependent => :delete
0
           include Netphase::Acts::Amazonable::InstanceMethods
0
@@ -51,20 +53,21 @@ module Netphase
0
           if self.amazon_product.nil?
0
             asin = (self.respond_to?('amazon_asin')) ? self.send(self.amazon_asin) : nil
0
             name = (self.respond_to?('amazon_name')) ? self.send(self.amazon_name) : nil
0
+ search_index = (self.respond_to?('amazon_search_index')) ? self.amazon_search_index : 'Books'
0
             
0
             if !asin.nil? && asin.length > 0
0
- #puts "Looking up #{asin}"
0
+ # puts "Looking up #{asin}"
0
               res = Amazon::Ecs.item_lookup(self.send(self.amazon_asin), :response_group => 'Medium')
0
               
0
               self.amazon_product =
0
                 AmazonProduct.new(:xml => res.doc.to_html, :asin => res.doc.at('asin').inner_html)
0
               self.amazon_product.save
0
             elsif !name.nil? && name.length > 0
0
- #puts "Searching for #{name}"
0
- res = Amazon::Ecs.item_search(self.send(self.amazon_name), :response_group => 'Medium') #, :sort => 'salesrank'
0
+ # puts "Searching for #{name}"
0
+ res = Amazon::Ecs.item_search(self.send(self.amazon_name), :search_index => self.amazon_search_index, :response_group => 'Medium') #, :sort => 'salesrank'
0
               res = res.doc.at('items/item')
0
               self.amazon_product =
0
- AmazonProduct.new(:xml => res.to_html, :asin => res.at('itemattributes/isbn').inner_html)
0
+ AmazonProduct.new(:xml => res.to_html, :asin => (res.at('itemattributes/isbn').nil? ? res.at('asin').inner_html : res.at('itemattributes/isbn').inner_html))
0
               self.amazon_product.save
0
             else
0
               logger.error "No known attributes to search by"
...
27
28
29
 
 
 
 
 
30
31
32
...
45
46
47
 
 
 
 
48
49
50
...
55
56
57
 
 
58
59
60
...
67
68
69
 
 
 
 
 
70
71
72
...
27
28
29
30
31
32
33
34
35
36
37
...
50
51
52
53
54
55
56
57
58
59
...
64
65
66
67
68
69
70
71
...
78
79
80
81
82
83
84
85
86
87
88
0
@@ -27,6 +27,11 @@ ActiveRecord::Base.connection.create_table :movies do |t|
0
   t.column :asin, :string
0
 end
0
 
0
+ActiveRecord::Base.connection.create_table :magazines do |t|
0
+ t.column :name, :string
0
+ t.column :asin, :string
0
+end
0
+
0
 ActiveRecord::Base.connection.create_table :amazon_products do |t| # , :id => false
0
   t.column :asin, :string
0
   t.column :xml, :text
0
@@ -45,6 +50,10 @@ class Movie < ActiveRecord::Base
0
   acts_as_amazon_product
0
 end
0
 
0
+class Magazine < ActiveRecord::Base
0
+ acts_as_amazon_product :search_index => 'Magazines'
0
+end
0
+
0
 AmazonProduct.delete_all
0
 
0
 class ActAsAmazonProductTest < Test::Unit::TestCase
0
@@ -55,6 +64,8 @@ class ActAsAmazonProductTest < Test::Unit::TestCase
0
       @book_ror = Book.create(:title => 'Rails Recipes')
0
       @book_perl = Book.create(:title => 'Perl')
0
       @movie_dh = Movie.create(:name=>'Live Free or Die Hard', :asin=>'B000VNMMRA')
0
+ Magazine.delete_all
0
+ @mag_lci = Magazine.create(:name => 'La Cucina Italiana')
0
     end
0
     
0
     def test_isbn
0
@@ -67,6 +78,11 @@ class ActAsAmazonProductTest < Test::Unit::TestCase
0
       assert_equal("Getting Things Done: The Art of Stress-Free Productivity", @book_gtd.amazon.title)
0
     end
0
     
0
+ def test_magazine
0
+ assert_not_nil(@mag_lci.amazon)
0
+ assert_equal("B00009XFML", @mag_lci.amazon.asin)
0
+ end
0
+
0
     def test_small_image
0
       assert_not_nil(@book_gtd.amazon)
0
       assert_match(/4104N6ME70L\._SL75_\.jpg/, @book_gtd.amazon.small_image_url)

Comments

    No one has commented yet.