acts_as_amazon_product 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/acts_as_amazon_product.rb +8 -3
- data/test/acts_as_amazon_product_test.rb +23 -6
- data/test/example.rb +28 -0
- data/test/foo.rb +7 -0
- data/test/test.db +0 -0
- metadata +5 -2
@@ -1,7 +1,7 @@
|
|
1
1
|
# ActsAsAmazonProduct
|
2
2
|
|
3
3
|
require 'rubygems'
|
4
|
-
require 'active_support'
|
4
|
+
#require 'active_support'
|
5
5
|
require 'active_record'
|
6
6
|
require 'amazon/ecs'
|
7
7
|
require 'amazon_product'
|
@@ -19,7 +19,9 @@ module Netphase
|
|
19
19
|
def acts_as_amazon_product(options = {})
|
20
20
|
defaults = {
|
21
21
|
:asin => 'asin',
|
22
|
-
:name => 'name'
|
22
|
+
:name => 'name',
|
23
|
+
:access_key => ENV['AMAZON_ACCESS_KEY_ID'],
|
24
|
+
:associate_tag => ENV['AMAZON_ASSOCIATE_TAG']
|
23
25
|
}
|
24
26
|
options = defaults.merge options
|
25
27
|
|
@@ -55,7 +57,7 @@ module Netphase
|
|
55
57
|
res = Amazon::Ecs.item_lookup(self.send(self.amazon_asin), :response_group => 'Medium')
|
56
58
|
|
57
59
|
self.amazon_product =
|
58
|
-
AmazonProduct.new(:xml => res.doc.to_html, :asin => res.doc.at('
|
60
|
+
AmazonProduct.new(:xml => res.doc.to_html, :asin => res.doc.at('asin').inner_html)
|
59
61
|
self.amazon_product.save
|
60
62
|
elsif !name.nil? && name.length > 0
|
61
63
|
#puts "Searching for #{name}"
|
@@ -70,6 +72,9 @@ module Netphase
|
|
70
72
|
end
|
71
73
|
self.amazon_product
|
72
74
|
end
|
75
|
+
|
76
|
+
#def method_missing(method, *args)
|
77
|
+
#end
|
73
78
|
|
74
79
|
def after_save
|
75
80
|
unless self.amazon_product.nil?
|
@@ -14,11 +14,17 @@ ActiveRecord::Base.establish_connection(config["database"])
|
|
14
14
|
|
15
15
|
ActiveRecord::Base.connection.drop_table :amazon_products rescue nil
|
16
16
|
ActiveRecord::Base.connection.drop_table :books rescue nil
|
17
|
+
ActiveRecord::Base.connection.drop_table :movies rescue nil
|
17
18
|
|
18
19
|
ActiveRecord::Base.connection.create_table :books do |t|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
t.column :title, :string
|
21
|
+
t.column :author, :string
|
22
|
+
t.column :isbn, :string
|
23
|
+
end
|
24
|
+
|
25
|
+
ActiveRecord::Base.connection.create_table :movies do |t|
|
26
|
+
t.column :name, :string
|
27
|
+
t.column :asin, :string
|
22
28
|
end
|
23
29
|
|
24
30
|
ActiveRecord::Base.connection.create_table :amazon_products do |t| # , :id => false
|
@@ -30,7 +36,13 @@ ActiveRecord::Base.connection.create_table :amazon_products do |t| # , :id => f
|
|
30
36
|
end
|
31
37
|
|
32
38
|
class Book < ActiveRecord::Base
|
33
|
-
acts_as_amazon_product
|
39
|
+
acts_as_amazon_product(
|
40
|
+
:asin => 'isbn', :name => 'title',
|
41
|
+
:access_key => @@access_key, :associate_tag => @@associate_tag)
|
42
|
+
end
|
43
|
+
|
44
|
+
class Movie < ActiveRecord::Base
|
45
|
+
acts_as_amazon_product
|
34
46
|
end
|
35
47
|
|
36
48
|
AmazonProduct.delete_all
|
@@ -42,6 +54,7 @@ class ActAsAmazonProductTest < Test::Unit::TestCase
|
|
42
54
|
@book_gtd = Book.create(:title => 'Getting Things Done', :author => 'Dude', :isbn => '0142000280')
|
43
55
|
@book_ror = Book.create(:title => 'Rails Recipes')
|
44
56
|
@book_perl = Book.create(:title => 'Perl')
|
57
|
+
@movie_dh = Movie.create(:name=>'Live Free or Die Hard', :asin=>'B000VNMMRA')
|
45
58
|
end
|
46
59
|
|
47
60
|
def test_isbn
|
@@ -81,5 +94,9 @@ class ActAsAmazonProductTest < Test::Unit::TestCase
|
|
81
94
|
@book_perl.save
|
82
95
|
assert_not_equal(isbn, @book_perl.amazon.isbn)
|
83
96
|
end
|
84
|
-
|
85
|
-
|
97
|
+
|
98
|
+
def test_product_with_all_defaults
|
99
|
+
assert_not_nil(@movie_dh.amazon)
|
100
|
+
assert_equal 'Bruce Willis', @movie_dh.amazon.get('itemattributes/actor')
|
101
|
+
end
|
102
|
+
end
|
data/test/example.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
$: << 'lib'
|
2
|
+
|
3
|
+
require 'acts_as_amazon_product'
|
4
|
+
|
5
|
+
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => 'test.db'})
|
6
|
+
ActiveRecord::Base.connection.create_table :books, :force => true do |t|
|
7
|
+
t.column :name, :string
|
8
|
+
t.column :asin, :string
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
class Book < ActiveRecord::Base
|
13
|
+
acts_as_amazon_product
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
book = Book.create(:name => 'Rails Recipes')
|
18
|
+
|
19
|
+
puts <<EOS
|
20
|
+
Title: #{book.amazon.title}
|
21
|
+
Author: #{book.amazon.author}
|
22
|
+
Price: #{book.amazon.price}
|
23
|
+
Image: #{book.amazon.small_image_url}
|
24
|
+
Details: #{book.amazon.detail_url}
|
25
|
+
EOS
|
26
|
+
|
27
|
+
|
28
|
+
|
data/test/foo.rb
ADDED
data/test/test.db
ADDED
Binary file
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: acts_as_amazon_product
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "1.
|
7
|
-
date: 2007-
|
6
|
+
version: "1.1"
|
7
|
+
date: 2007-11-03 00:00:00 -04:00
|
8
8
|
summary: A package for simplifying use of the Amazon/ECS API
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -33,6 +33,9 @@ files:
|
|
33
33
|
- lib/amazon_product.rb
|
34
34
|
- test/acts_as_amazon_product_test.rb
|
35
35
|
- test/config-example.yml
|
36
|
+
- test/example.rb
|
37
|
+
- test/foo.rb
|
38
|
+
- test/test.db
|
36
39
|
- README
|
37
40
|
test_files: []
|
38
41
|
|