acts_as_amazon_product 1.2 → 1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +18 -14
- data/lib/amazon_product.rb +23 -17
- data/test/acts_as_amazon_product_test.rb +84 -59
- data/test/test.db +0 -0
- metadata +2 -2
data/README
CHANGED
@@ -1,27 +1,29 @@
|
|
1
1
|
= ActsAsAmazonProduct
|
2
2
|
|
3
|
-
This "acts_as" module makes it extremely simple to add Amazon integration to an
|
3
|
+
This "acts_as" module makes it extremely simple to add Amazon integration to an
|
4
|
+
existing model.
|
4
5
|
|
5
6
|
== Setup
|
6
7
|
|
7
|
-
This code does not require changing any current database tables. It only requires
|
8
|
+
This code does not require changing any current database tables. It only requires
|
9
|
+
adding one migration for a single new table used to cache responses from Amazon.
|
10
|
+
Run the following to create the migration:
|
8
11
|
|
9
|
-
|
10
|
-
t.column :asin, :string
|
11
|
-
t.column :xml, :text
|
12
|
-
t.column :created_at, :datetime, :null => false
|
13
|
-
t.column :amazonable_id, :integer, :default => 0, :null => false
|
14
|
-
t.column :amazonable_type, :string, :limit => 15, :default => "", :null => false
|
15
|
-
end
|
12
|
+
script/generate acts_as_amazon_product_migration
|
16
13
|
|
17
14
|
== Using acts_as_amazon_product
|
18
15
|
|
19
|
-
Add the "acts_as" line to your model. Only :access_key is required. The :asin and
|
16
|
+
Add the "acts_as" line to your model. Only :access_key is required. The :asin and
|
17
|
+
:name assignments override what attributes in your class should be used for a direct
|
18
|
+
lookup (item_looup) or general search (item_search). If no value is assigned to :asin
|
19
|
+
then a general search is made and the first item (sorted by salesrank) is assigned
|
20
|
+
(this functionality will likely change).
|
20
21
|
|
21
22
|
require 'acts_as_amazon_product'
|
22
23
|
|
23
24
|
class Book < ActiveRecord::Base
|
24
|
-
acts_as_amazon_product :asin => 'isbn', :name => 'title',
|
25
|
+
acts_as_amazon_product :asin => 'isbn', :name => 'title',
|
26
|
+
:access_key => '0123456', :associate_tag => 'assoc-20'
|
25
27
|
end
|
26
28
|
|
27
29
|
You can now access the Amazon data in your views like so (see):
|
@@ -37,12 +39,14 @@ You can now access the Amazon data in your views like so (see):
|
|
37
39
|
|
38
40
|
== Unit tests
|
39
41
|
|
40
|
-
There are tests you can run in the [gem]/test directory. Just create a
|
42
|
+
There are tests you can run in the [gem]/test directory. Just create a
|
43
|
+
test/config.yml from the example and run "Rake test" from the [gem] directory.
|
41
44
|
|
42
45
|
= Credits
|
43
|
-
acts_as_amazon_product was
|
46
|
+
acts_as_amazon_product was created by Scott Nedderman and Chris Beck from netphase.com
|
44
47
|
|
45
|
-
We'd love to here how you like it. Also, if you'd like us to help out with your next
|
48
|
+
We'd love to here how you like it. Also, if you'd like us to help out with your next
|
49
|
+
project, let us know.
|
46
50
|
|
47
51
|
http://netphase.com
|
48
52
|
|
data/lib/amazon_product.rb
CHANGED
@@ -1,54 +1,60 @@
|
|
1
1
|
require 'hpricot'
|
2
2
|
|
3
3
|
class AmazonProduct < ActiveRecord::Base
|
4
|
-
# belongs_to :amazonable, :polymorphic => true
|
5
4
|
|
6
|
-
def
|
5
|
+
def get(key, separator = ', ')
|
7
6
|
@doc ||= Hpricot.XML(xml)
|
8
|
-
@doc
|
9
|
-
|
10
|
-
|
11
|
-
def get(key)
|
12
|
-
hdoc(key)
|
7
|
+
values = (@doc/key).collect {|e| e.inner_html }
|
8
|
+
values *= separator unless separator.nil?
|
9
|
+
values unless values.blank?
|
13
10
|
end
|
14
11
|
|
15
12
|
def isbn
|
16
|
-
|
13
|
+
get("itemattributes/isbn")
|
17
14
|
end
|
18
15
|
|
19
16
|
def title
|
20
|
-
|
17
|
+
get("itemattributes/title")
|
21
18
|
end
|
22
19
|
|
23
20
|
def author
|
24
|
-
|
21
|
+
get("itemattributes/author")
|
25
22
|
end
|
26
23
|
|
27
24
|
def binding
|
28
|
-
|
25
|
+
get("itemattributes/binding")
|
29
26
|
end
|
30
27
|
|
31
28
|
def price
|
32
|
-
|
29
|
+
get("itemattributes/listprice/amount")
|
33
30
|
end
|
34
31
|
|
35
32
|
def pages
|
36
|
-
|
33
|
+
get("itemattributes/numberofpages")
|
37
34
|
end
|
38
35
|
|
39
36
|
def small_image_url
|
40
|
-
|
37
|
+
get("smallimage/url")
|
41
38
|
end
|
42
39
|
|
43
40
|
def medium_image_url
|
44
|
-
|
41
|
+
get("mediumimage/url")
|
45
42
|
end
|
46
43
|
|
47
44
|
def large_image_url
|
48
|
-
|
45
|
+
get("largeimage/url")
|
49
46
|
end
|
50
47
|
|
51
48
|
def detail_url
|
52
|
-
|
49
|
+
get("detailpageurl")
|
53
50
|
end
|
51
|
+
|
52
|
+
def method_missing(symbol, *args)
|
53
|
+
begin
|
54
|
+
super(symbol, *args)
|
55
|
+
rescue NoMethodError
|
56
|
+
get(symbol, *args)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
54
60
|
end
|
@@ -15,6 +15,7 @@ ActiveRecord::Base.establish_connection(config["database"])
|
|
15
15
|
ActiveRecord::Base.connection.drop_table :amazon_products rescue nil
|
16
16
|
ActiveRecord::Base.connection.drop_table :books rescue nil
|
17
17
|
ActiveRecord::Base.connection.drop_table :movies rescue nil
|
18
|
+
ActiveRecord::Base.connection.drop_table :magazines rescue nil
|
18
19
|
|
19
20
|
ActiveRecord::Base.connection.create_table :books do |t|
|
20
21
|
t.column :title, :string
|
@@ -47,72 +48,96 @@ class Book < ActiveRecord::Base
|
|
47
48
|
end
|
48
49
|
|
49
50
|
class Movie < ActiveRecord::Base
|
50
|
-
acts_as_amazon_product
|
51
|
+
acts_as_amazon_product :access_key => @@access_key, :associate_tag => @@associate_tag
|
51
52
|
end
|
52
53
|
|
53
54
|
class Magazine < ActiveRecord::Base
|
54
|
-
acts_as_amazon_product :search_index => 'Magazines'
|
55
|
+
acts_as_amazon_product :search_index => 'Magazines', :access_key => @@access_key,
|
56
|
+
:associate_tag => @@associate_tag
|
55
57
|
end
|
56
58
|
|
57
59
|
AmazonProduct.delete_all
|
58
60
|
|
59
61
|
class ActAsAmazonProductTest < Test::Unit::TestCase
|
60
62
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
63
|
+
def setup
|
64
|
+
Book.delete_all
|
65
|
+
@book_gtd = Book.create(:title => 'Getting Things Done', :author => 'Dude', :isbn => '0142000280')
|
66
|
+
@book_ror = Book.create(:title => 'Rails Recipes')
|
67
|
+
@book_perl = Book.create(:title => 'Perl')
|
68
|
+
@movie_dh = Movie.create(:name=>'Live Free or Die Hard', :asin=>'B000VNMMRA')
|
69
|
+
Magazine.delete_all
|
70
|
+
@mag_lci = Magazine.create(:name => 'La Cucina Italiana')
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_isbn
|
74
|
+
assert_not_nil(@book_gtd.amazon)
|
75
|
+
assert_equal("0142000280", @book_gtd.amazon.isbn)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_title
|
79
|
+
assert_not_nil(@book_gtd.amazon)
|
80
|
+
assert_equal("Getting Things Done: The Art of Stress-Free Productivity", @book_gtd.amazon.title)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_magazine
|
84
|
+
assert_not_nil(@mag_lci.amazon)
|
85
|
+
assert_equal("B00009XFML", @mag_lci.amazon.asin)
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_small_image
|
89
|
+
assert_not_nil(@book_gtd.amazon)
|
90
|
+
assert_match(/4104N6ME70L\._SL75_\.jpg/, @book_gtd.amazon.small_image_url)
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_author
|
94
|
+
assert_not_nil(@book_gtd.amazon)
|
95
|
+
assert_equal("David Allen", @book_gtd.amazon.author)
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_binding
|
99
|
+
assert_not_nil(@book_gtd.amazon)
|
100
|
+
assert_equal("Paperback", @book_gtd.amazon.binding)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_find_with_multiple
|
104
|
+
assert_equal("Advanced Rails Recipes: 84 New Ways to Build Stunning Rails Apps (Pragmatic Programmers)", @book_ror.amazon.title)
|
105
|
+
assert_equal("Mike Clark", @book_ror.amazon.author)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_returns_nil_if_attribute_not_found
|
109
|
+
assert_equal(nil, @book_ror.amazon.get('contributor'))
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_update
|
113
|
+
assert_not_nil(@book_perl.amazon)
|
114
|
+
isbn = @book_perl.amazon.isbn
|
115
|
+
@book_perl.title = "Websters"
|
116
|
+
@book_perl.save
|
117
|
+
assert_not_equal(isbn, @book_perl.amazon.isbn)
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_product_with_all_defaults
|
121
|
+
assert_not_nil(@movie_dh.amazon)
|
122
|
+
assert_equal 'Bruce Willis, Timothy Olyphant, Justin Long, Maggie Q, Cliff Curtis',
|
123
|
+
@movie_dh.amazon.get('itemattributes/actor')
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_accepts_a_custom_separator
|
127
|
+
assert_equal 'Bruce Willis | Timothy Olyphant | Justin Long | Maggie Q | Cliff Curtis',
|
128
|
+
@movie_dh.amazon.get('itemattributes/actor', ' | ')
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_returns_array_if_separator_is_nil
|
132
|
+
assert_equal ['Bruce Willis', 'Timothy Olyphant', 'Justin Long', 'Maggie Q', 'Cliff Curtis'],
|
133
|
+
@movie_dh.amazon.get('itemattributes/actor', nil)
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_method_missing
|
137
|
+
assert_equal 'Bruce Willis, Timothy Olyphant, Justin Long, Maggie Q, Cliff Curtis', @movie_dh.amazon.actor
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_method_missing_with_separator
|
141
|
+
assert_equal 'Bruce Willis | Timothy Olyphant | Justin Long | Maggie Q | Cliff Curtis', @movie_dh.amazon.actor(' | ')
|
142
|
+
end
|
118
143
|
end
|
data/test/test.db
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_amazon_product
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "1.
|
4
|
+
version: "1.3"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Nedderman
|
@@ -9,7 +9,7 @@ autorequire: acts_as_amazon_product
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-08-11 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|