amazon_product 3.0.0.pre.2 → 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +29 -16
- data/lib/amazon_product.rb +3 -7
- data/lib/amazon_product/{hash_builder.rb → builder.rb} +1 -1
- data/lib/amazon_product/operations.rb +9 -9
- data/lib/amazon_product/request.rb +10 -8
- data/lib/amazon_product/response.rb +8 -10
- data/lib/amazon_product/version.rb +1 -1
- data/spec/amazon_product/{hash_builder_spec.rb → builder_spec.rb} +5 -5
- data/spec/amazon_product/operations_spec.rb +97 -0
- data/spec/amazon_product/request_spec.rb +1 -0
- data/spec/spec_helper.rb +5 -0
- metadata +16 -27
- data/lib/amazon_product.rbc +0 -439
- data/lib/amazon_product/error.rbc +0 -197
- data/lib/amazon_product/hash_builder.rbc +0 -788
- data/lib/amazon_product/locale.rbc +0 -647
- data/lib/amazon_product/operations.rbc +0 -1660
- data/lib/amazon_product/request.rbc +0 -3389
- data/lib/amazon_product/response.rbc +0 -1168
- data/lib/amazon_product/version.rbc +0 -130
- data/spec/amazon_product/hash_builder_spec.rbc +0 -863
- data/spec/amazon_product/request_spec.rbc +0 -7210
- data/spec/amazon_product/response_spec.rbc +0 -2250
- data/spec/spec_helper.rbc +0 -204
data/README.md
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
# Amazon Product
|
2
2
|
|
3
|
-
Amazon Product is a [Nokogiri][1]-backed Ruby wrapper to the [Amazon
|
3
|
+
Amazon Product is a [Nokogiri][1]-backed Ruby wrapper to the [Amazon
|
4
|
+
Product Advertising API] [2].
|
4
5
|
|
5
|
-
[![travis](
|
6
|
+
[![travis](http://travis-ci.org/hakanensari/amazon_product.png)](http://travis-ci.org/hakanensari/amazon_product)
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add to your Gemfile.
|
11
|
+
|
12
|
+
gem 'amazon_product'
|
6
13
|
|
7
14
|
## Usage
|
8
15
|
|
@@ -10,40 +17,46 @@ Set up a request.
|
|
10
17
|
|
11
18
|
require "amazon_product"
|
12
19
|
|
13
|
-
|
20
|
+
req = AmazonProduct["us"]
|
14
21
|
|
15
|
-
|
16
|
-
c.key =
|
17
|
-
c.secret =
|
18
|
-
c.tag =
|
22
|
+
req.configure do |c|
|
23
|
+
c.key = AMAZON_KEY
|
24
|
+
c.secret = AMAZON_SECRET
|
25
|
+
c.tag = AMAZON_ASSOCIATE_TAG
|
19
26
|
end
|
20
27
|
|
21
28
|
Look up a product.
|
22
29
|
|
23
|
-
|
24
|
-
|
25
|
-
|
30
|
+
req << { :operation' => 'ItemLookup',
|
31
|
+
:item_id' => '0679753354' }
|
32
|
+
resp = request.get
|
26
33
|
|
27
34
|
[Or use a shorthand] [3].
|
28
35
|
|
29
|
-
|
36
|
+
resp = req.find('0679753354')
|
30
37
|
|
31
38
|
Consume the entire response.
|
32
39
|
|
33
|
-
|
40
|
+
resp.to_hash
|
34
41
|
|
35
42
|
Quickly drop down to a particular node.
|
36
43
|
|
37
|
-
|
44
|
+
resp['Item']
|
45
|
+
|
46
|
+
[Please see the project page] [4] for further detail.
|
47
|
+
|
48
|
+
## Adapters
|
38
49
|
|
39
|
-
|
50
|
+
Amazon Product defaults to the Net::HTTP library but can be configured
|
51
|
+
to use Curb or EM-HTTP-Request.
|
40
52
|
|
41
53
|
## Branding is a delicate art
|
42
54
|
|
43
|
-
|
55
|
+
Amazon Product descends from [Sucker][5]. While I still like the vacuum
|
56
|
+
metaphor, the name felt tiring after a while.
|
44
57
|
|
45
58
|
[1]: http://nokogiri.org/
|
46
59
|
[2]: https://affiliate-program.amazon.co.uk/gp/advertising/api/detail/main.html
|
47
60
|
[3]: https://github.com/hakanensari/amazon_product/blob/master/lib/amazon_product/operations.rb
|
48
61
|
[4]: http://code.papercavalier.com/amazon_product/
|
49
|
-
[5]: http://github.com/papercavalier/sucker/
|
62
|
+
[5]: http://github.com/papercavalier/sucker/
|
data/lib/amazon_product.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
|
-
require 'net/http'
|
2
1
|
require 'nokogiri'
|
3
2
|
require 'openssl'
|
4
3
|
|
5
|
-
|
6
|
-
require
|
7
|
-
|
8
|
-
require 'amazon_product/operations'
|
9
|
-
require 'amazon_product/request'
|
10
|
-
require 'amazon_product/response'
|
4
|
+
%w{builder error locale operations request response}.each do |f|
|
5
|
+
require "amazon_product/#{f}"
|
6
|
+
end
|
11
7
|
|
12
8
|
# Amazon Product is a Ruby wrapper to the Amazon Product Advertising API.
|
13
9
|
module AmazonProduct
|
@@ -39,8 +39,8 @@ module AmazonProduct
|
|
39
39
|
def find(*item_ids)
|
40
40
|
reset
|
41
41
|
params = item_ids.last.is_a?(Hash) ? item_ids.pop : {}
|
42
|
-
self.<<
|
43
|
-
|
42
|
+
self.<<({ 'Operation' => 'ItemLookup',
|
43
|
+
'ItemId' => item_ids }.merge(params))
|
44
44
|
get
|
45
45
|
end
|
46
46
|
|
@@ -48,8 +48,8 @@ module AmazonProduct
|
|
48
48
|
# children, and ancestors.
|
49
49
|
def find_browse_node(browse_node_id, params = {})
|
50
50
|
reset
|
51
|
-
self.<<
|
52
|
-
|
51
|
+
self.<<({ 'Operation' => 'BrowseNodeLookup',
|
52
|
+
'BrowseNodeId' => browse_node_id }.merge(params))
|
53
53
|
get
|
54
54
|
end
|
55
55
|
|
@@ -58,8 +58,8 @@ module AmazonProduct
|
|
58
58
|
def find_similar(*item_ids)
|
59
59
|
reset
|
60
60
|
params = item_ids.last.is_a?(Hash) ? item_ids.pop : {}
|
61
|
-
self.<<
|
62
|
-
|
61
|
+
self.<<({ 'Operation' => 'SimilarityLookup',
|
62
|
+
'ItemId' => item_ids }.merge(params))
|
63
63
|
get
|
64
64
|
end
|
65
65
|
|
@@ -94,8 +94,8 @@ module AmazonProduct
|
|
94
94
|
params = { 'Keywords' => params }
|
95
95
|
end
|
96
96
|
|
97
|
-
self.<<
|
98
|
-
|
97
|
+
self.<<({ 'Operation' => 'ItemSearch',
|
98
|
+
'SearchIndex' => search_index }.merge(params))
|
99
99
|
get
|
100
100
|
end
|
101
101
|
|
@@ -103,7 +103,7 @@ module AmazonProduct
|
|
103
103
|
|
104
104
|
def cart(operation, params)
|
105
105
|
reset
|
106
|
-
self.<<
|
106
|
+
self.<<({ 'Operation' => "Cart#{operation}" }.merge(params))
|
107
107
|
get
|
108
108
|
end
|
109
109
|
end
|
@@ -29,8 +29,9 @@ module AmazonProduct
|
|
29
29
|
require 'em-synchrony'
|
30
30
|
require 'em-synchrony/em-http'
|
31
31
|
when :net_http
|
32
|
+
require 'net/http'
|
32
33
|
else
|
33
|
-
raise ArgumentError, "
|
34
|
+
raise ArgumentError, ":#{client} is not a valid HTTP client"
|
34
35
|
end
|
35
36
|
|
36
37
|
@adapter = client
|
@@ -38,7 +39,7 @@ module AmazonProduct
|
|
38
39
|
end
|
39
40
|
|
40
41
|
# Set HTTP client to Net::HTTP.
|
41
|
-
|
42
|
+
Request.adapter = :net_http
|
42
43
|
|
43
44
|
# Creates a new request for specified locale.
|
44
45
|
def initialize(locale)
|
@@ -48,7 +49,7 @@ module AmazonProduct
|
|
48
49
|
|
49
50
|
# Merges a hash of request parameters into the query.
|
50
51
|
#
|
51
|
-
# request << { :key => 'value }
|
52
|
+
# request << { :key => 'value' }
|
52
53
|
#
|
53
54
|
def <<(hash)
|
54
55
|
hash.each do |k, v|
|
@@ -62,12 +63,17 @@ module AmazonProduct
|
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
66
|
+
# The HTTP client.
|
67
|
+
def adapter
|
68
|
+
Request.adapter
|
69
|
+
end
|
70
|
+
|
65
71
|
# Performs an asynchronous request with the EM async HTTP client.
|
66
72
|
#
|
67
73
|
# Yields response to given block.
|
68
74
|
def aget(&block)
|
69
75
|
unless adapter == :synchrony
|
70
|
-
raise TypeError, "Set HTTP client to
|
76
|
+
raise TypeError, "Set HTTP client to :synchrony"
|
71
77
|
end
|
72
78
|
|
73
79
|
http = EM::HttpRequest.new(url).aget
|
@@ -153,10 +159,6 @@ module AmazonProduct
|
|
153
159
|
|
154
160
|
private
|
155
161
|
|
156
|
-
def adapter
|
157
|
-
Request.adapter
|
158
|
-
end
|
159
|
-
|
160
162
|
def escape(value)
|
161
163
|
value.gsub(/([^a-zA-Z0-9_.~-]+)/) do
|
162
164
|
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
|
@@ -13,8 +13,8 @@ module AmazonProduct
|
|
13
13
|
@code = code.to_i
|
14
14
|
end
|
15
15
|
|
16
|
-
# A shorthand that queries for a specified attribute and yields to
|
17
|
-
# block each matching document.
|
16
|
+
# A shorthand that queries for a specified attribute and yields to
|
17
|
+
# a given block each matching document.
|
18
18
|
#
|
19
19
|
# response.each('Item') { |item| puts item }
|
20
20
|
#
|
@@ -27,15 +27,13 @@ module AmazonProduct
|
|
27
27
|
find('Error')
|
28
28
|
end
|
29
29
|
|
30
|
-
# Queries for a specified attribute and returns an array of
|
31
|
-
# documents.
|
30
|
+
# Queries for a specified attribute and returns an array of
|
31
|
+
# matching documents.
|
32
32
|
#
|
33
33
|
# items = response.find('Item')
|
34
34
|
#
|
35
35
|
def find(attribute)
|
36
|
-
xml.xpath("//xmlns:#{attribute}").map
|
37
|
-
HashBuilder.from_xml(element)
|
38
|
-
end
|
36
|
+
xml.xpath("//xmlns:#{attribute}").map { |e| Builder.from_xml(e) }
|
39
37
|
end
|
40
38
|
alias [] find
|
41
39
|
|
@@ -44,8 +42,8 @@ module AmazonProduct
|
|
44
42
|
errors.count > 0
|
45
43
|
end
|
46
44
|
|
47
|
-
# A shorthand that queries for a specifed attribute, yields to a
|
48
|
-
# block matching documents, and collects final values.
|
45
|
+
# A shorthand that queries for a specifed attribute, yields to a
|
46
|
+
# given block matching documents, and collects final values.
|
49
47
|
#
|
50
48
|
# items = response.map('Item') { |item| # do something }
|
51
49
|
#
|
@@ -55,7 +53,7 @@ module AmazonProduct
|
|
55
53
|
|
56
54
|
# Parses the response into a simple hash.
|
57
55
|
def to_hash
|
58
|
-
|
56
|
+
Builder.from_xml(xml)
|
59
57
|
end
|
60
58
|
|
61
59
|
# Checks if the HTTP response is OK.
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module AmazonProduct
|
4
|
-
describe
|
4
|
+
describe Builder do
|
5
5
|
let(:xml) do
|
6
6
|
xml = <<-XML.gsub!(/>\s+</, '><').strip!
|
7
7
|
<?xml version=\"1.0\" ?>
|
@@ -17,19 +17,19 @@ module AmazonProduct
|
|
17
17
|
|
18
18
|
describe '.from_xml' do
|
19
19
|
it 'returns a hash' do
|
20
|
-
|
20
|
+
Builder.from_xml(xml).should be_an_instance_of Hash
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'handles only childs' do
|
24
|
-
|
24
|
+
Builder.from_xml(xml)['Title'].should eql 'Anti-Oedipus'
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'handles arrays' do
|
28
|
-
|
28
|
+
Builder.from_xml(xml)['Author'].should be_a Array
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'handles attributes' do
|
32
|
-
node =
|
32
|
+
node = Builder.from_xml(xml)['Creator']
|
33
33
|
node['Role'].should eql 'Translator'
|
34
34
|
node['__content__'].should eql 'Robert Hurley'
|
35
35
|
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AmazonProduct
|
4
|
+
describe Operations do
|
5
|
+
subject { Request.new('us') }
|
6
|
+
|
7
|
+
before do
|
8
|
+
subject.configure do |c|
|
9
|
+
c.key = 'foo'
|
10
|
+
c.tag = 'bar'
|
11
|
+
end
|
12
|
+
subject.stub!(:get)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#find" do
|
16
|
+
before do
|
17
|
+
subject.find('1', '2', :foo => 'bar')
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'merges item ids' do
|
21
|
+
subject.params['ItemId'].should eql '1,2'
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'merges additional parameters' do
|
25
|
+
subject.params['Foo'].should eql 'bar'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#find_browse_node" do
|
30
|
+
before do
|
31
|
+
subject.find_browse_node('123', :foo => 'bar')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'merges item ids' do
|
35
|
+
subject.params['BrowseNodeId'].should eql '123'
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'merges additional parameters' do
|
39
|
+
subject.params['Foo'].should eql 'bar'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#find_similar" do
|
44
|
+
before do
|
45
|
+
subject.find_similar('1', '2')
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'merges item ids' do
|
49
|
+
subject.params['ItemId'].should eql '1,2'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#search" do
|
54
|
+
context "when given a keyword" do
|
55
|
+
before do
|
56
|
+
subject.search('foo')
|
57
|
+
end
|
58
|
+
|
59
|
+
it "does a keyword search" do
|
60
|
+
subject.params['Keywords'].should eql 'foo'
|
61
|
+
end
|
62
|
+
|
63
|
+
it "searches all products" do
|
64
|
+
subject.params["SearchIndex"].should eql 'All'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "when given a search index and a keyword" do
|
69
|
+
before do
|
70
|
+
subject.search('foo', 'bar')
|
71
|
+
end
|
72
|
+
|
73
|
+
it "does a keyword search" do
|
74
|
+
subject.params['Keywords'].should eql 'bar'
|
75
|
+
end
|
76
|
+
|
77
|
+
it "sets the search index" do
|
78
|
+
subject.params["SearchIndex"].should eql 'foo'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "when given a search index and parameters" do
|
83
|
+
before do
|
84
|
+
subject.search('foo', :bar => 'baz')
|
85
|
+
end
|
86
|
+
|
87
|
+
it "sets the parameters" do
|
88
|
+
subject.params['Bar'].should eql 'baz'
|
89
|
+
end
|
90
|
+
|
91
|
+
it "sets the search index" do
|
92
|
+
subject.params["SearchIndex"].should eql 'foo'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amazon_product
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
5
|
-
prerelease:
|
4
|
+
version: 3.0.1
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Hakan Ensari
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-09-01 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
16
|
-
requirement: &
|
16
|
+
requirement: &70238910833080 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '1.4'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70238910833080
|
25
25
|
description: Amazon Product is a Ruby wrapper to the Amazon Product Advertising API.
|
26
26
|
email:
|
27
27
|
- code@papercavalier.com
|
@@ -29,33 +29,22 @@ executables: []
|
|
29
29
|
extensions: []
|
30
30
|
extra_rdoc_files: []
|
31
31
|
files:
|
32
|
+
- lib/amazon_product/builder.rb
|
32
33
|
- lib/amazon_product/error.rb
|
33
|
-
- lib/amazon_product/error.rbc
|
34
|
-
- lib/amazon_product/hash_builder.rb
|
35
|
-
- lib/amazon_product/hash_builder.rbc
|
36
34
|
- lib/amazon_product/locale.rb
|
37
|
-
- lib/amazon_product/locale.rbc
|
38
35
|
- lib/amazon_product/operations.rb
|
39
|
-
- lib/amazon_product/operations.rbc
|
40
36
|
- lib/amazon_product/request.rb
|
41
|
-
- lib/amazon_product/request.rbc
|
42
37
|
- lib/amazon_product/response.rb
|
43
|
-
- lib/amazon_product/response.rbc
|
44
38
|
- lib/amazon_product/version.rb
|
45
|
-
- lib/amazon_product/version.rbc
|
46
39
|
- lib/amazon_product.rb
|
47
|
-
- lib/amazon_product.rbc
|
48
40
|
- LICENSE
|
49
41
|
- README.md
|
50
|
-
- spec/amazon_product/
|
51
|
-
- spec/amazon_product/
|
42
|
+
- spec/amazon_product/builder_spec.rb
|
43
|
+
- spec/amazon_product/operations_spec.rb
|
52
44
|
- spec/amazon_product/request_spec.rb
|
53
|
-
- spec/amazon_product/request_spec.rbc
|
54
45
|
- spec/amazon_product/response_spec.rb
|
55
|
-
- spec/amazon_product/response_spec.rbc
|
56
46
|
- spec/fixtures/http_response
|
57
47
|
- spec/spec_helper.rb
|
58
|
-
- spec/spec_helper.rbc
|
59
48
|
homepage: http://code.papercavalier.com/amazon_product/
|
60
49
|
licenses: []
|
61
50
|
post_install_message:
|
@@ -70,13 +59,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
59
|
version: '0'
|
71
60
|
segments:
|
72
61
|
- 0
|
73
|
-
hash:
|
62
|
+
hash: 1428334579751744401
|
74
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
64
|
none: false
|
76
65
|
requirements:
|
77
|
-
- - ! '
|
66
|
+
- - ! '>='
|
78
67
|
- !ruby/object:Gem::Version
|
79
|
-
version:
|
68
|
+
version: '0'
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
hash: 1428334579751744401
|
80
72
|
requirements: []
|
81
73
|
rubyforge_project:
|
82
74
|
rubygems_version: 1.8.6
|
@@ -84,12 +76,9 @@ signing_key:
|
|
84
76
|
specification_version: 3
|
85
77
|
summary: A Ruby wrapper to the Amazon Product Advertising API
|
86
78
|
test_files:
|
87
|
-
- spec/amazon_product/
|
88
|
-
- spec/amazon_product/
|
79
|
+
- spec/amazon_product/builder_spec.rb
|
80
|
+
- spec/amazon_product/operations_spec.rb
|
89
81
|
- spec/amazon_product/request_spec.rb
|
90
|
-
- spec/amazon_product/request_spec.rbc
|
91
82
|
- spec/amazon_product/response_spec.rb
|
92
|
-
- spec/amazon_product/response_spec.rbc
|
93
83
|
- spec/fixtures/http_response
|
94
84
|
- spec/spec_helper.rb
|
95
|
-
- spec/spec_helper.rbc
|