vacuum 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -6
- data/lib/vacuum.rb +5 -4
- data/lib/vacuum/product_advertising.rb +1 -1
- data/lib/vacuum/request/base.rb +13 -0
- data/lib/vacuum/request/product_advertising.rb +8 -3
- data/lib/vacuum/version.rb +1 -1
- data/spec/support/shared_examples/request.rb +47 -0
- data/spec/vacuum/request/product_advertising_spec.rb +9 -1
- metadata +11 -11
data/README.md
CHANGED
@@ -35,12 +35,17 @@ req.build operation: 'ItemSearch',
|
|
35
35
|
search_index: 'Books',
|
36
36
|
keywords: 'Deleuze'
|
37
37
|
res = req.get
|
38
|
+
|
39
|
+
res.valid? or raise res.code
|
40
|
+
|
41
|
+
p res.body
|
38
42
|
```
|
39
43
|
|
40
|
-
Or
|
44
|
+
Or run the same search less verbosely:
|
41
45
|
|
42
46
|
```ruby
|
43
47
|
res = req.search :books, 'Deleuze'
|
48
|
+
p res.body
|
44
49
|
```
|
45
50
|
|
46
51
|
The response wraps a [Nokogiri] [6] document:
|
@@ -52,14 +57,12 @@ res.xml
|
|
52
57
|
And lets you drop down to any node:
|
53
58
|
|
54
59
|
```ruby
|
55
|
-
|
56
|
-
|
57
|
-
p item
|
58
|
-
end
|
60
|
+
res.find('Item') do |item|
|
61
|
+
p item
|
59
62
|
end
|
60
63
|
```
|
61
64
|
|
62
|
-
|
65
|
+
Browse more examples [here] [7].
|
63
66
|
|
64
67
|
## Amazon Marketplace Web Services API
|
65
68
|
|
data/lib/vacuum.rb
CHANGED
@@ -21,9 +21,10 @@ require 'vacuum/version'
|
|
21
21
|
|
22
22
|
# Vacuum is a Ruby wrapper to various Amazon Web Services (AWS) APIs.
|
23
23
|
module Vacuum
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
BadLocale = Class.new ArgumentError
|
25
|
+
BadResponse = Class.new StandardError
|
26
|
+
MissingKey = Class.new ArgumentError
|
27
|
+
MissingSecret = Class.new ArgumentError
|
27
28
|
|
28
29
|
class << self
|
29
30
|
def new(api, &blk)
|
@@ -31,7 +32,7 @@ module Vacuum
|
|
31
32
|
when /^mws/
|
32
33
|
require 'vacuum/mws'
|
33
34
|
Request::MWS.new do |config|
|
34
|
-
config.api = api.slice(4, api.size).to_sym
|
35
|
+
config.api = api.slice(4, api.size).to_sym
|
35
36
|
blk.call config
|
36
37
|
end
|
37
38
|
when :product_advertising
|
data/lib/vacuum/request/base.rb
CHANGED
@@ -70,6 +70,19 @@ module Vacuum
|
|
70
70
|
Response.const_get(class_basename).new res.body, res.status
|
71
71
|
end
|
72
72
|
|
73
|
+
# Performs the AWS API request.
|
74
|
+
#
|
75
|
+
# Raises a Bad Response if the response is not valid.
|
76
|
+
#
|
77
|
+
# Returns a Vacuum::Response::Base or a subclass thereof.
|
78
|
+
def get!
|
79
|
+
unless (res = get).valid?
|
80
|
+
raise BadResponse, "#{res.code} #{res['Code'].first}"
|
81
|
+
end
|
82
|
+
|
83
|
+
res
|
84
|
+
end
|
85
|
+
|
73
86
|
# Returns the Hash parameters of the AWS API request.
|
74
87
|
def parameters
|
75
88
|
default_parameters.merge @parameters
|
@@ -11,7 +11,9 @@ module Vacuum
|
|
11
11
|
#
|
12
12
|
# request.find '0679753354', response_group: 'Images'
|
13
13
|
#
|
14
|
-
#
|
14
|
+
# Raises a Bad Response if response is not valid.
|
15
|
+
#
|
16
|
+
# Returns a Vacuum::Response::ProductAdvertising.
|
15
17
|
def look_up(*item_ids)
|
16
18
|
given_params = item_ids.last.is_a?(Hash) ? item_ids.pop : {}
|
17
19
|
|
@@ -36,7 +38,7 @@ module Vacuum
|
|
36
38
|
end
|
37
39
|
build! params
|
38
40
|
|
39
|
-
get
|
41
|
+
get!
|
40
42
|
end
|
41
43
|
|
42
44
|
# Searches for items that satisfy the given criteria, including one or
|
@@ -56,6 +58,9 @@ module Vacuum
|
|
56
58
|
# request.search :books, power: 'author:lacan and not fiction',
|
57
59
|
# sort: 'relevancerank'
|
58
60
|
#
|
61
|
+
# Raises a Bad Response if response is not valid.
|
62
|
+
#
|
63
|
+
# Returns a Vacuum::Response::ProductAdvertising.
|
59
64
|
def search(index, query_or_params = nil)
|
60
65
|
params = case query_or_params
|
61
66
|
when String
|
@@ -66,7 +71,7 @@ module Vacuum
|
|
66
71
|
build! params.merge! 'Operation' => 'ItemSearch',
|
67
72
|
'SearchIndex' => Utils.camelize(index.to_s)
|
68
73
|
|
69
|
-
get
|
74
|
+
get!
|
70
75
|
end
|
71
76
|
|
72
77
|
# Returns the Addressable::URI URL of the Product Advertising API
|
data/lib/vacuum/version.rb
CHANGED
@@ -86,6 +86,53 @@ shared_examples 'a request' do
|
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
|
+
describe '#get!' do
|
90
|
+
let(:response_class) do
|
91
|
+
Vacuum::Response.const_get request.send(:class_basename)
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'when response is bad' do
|
95
|
+
let(:mock_response) do
|
96
|
+
body = <<-XML.gsub!(/>\s+</, '><').strip!
|
97
|
+
<?xml version=\"1.0\" ?>
|
98
|
+
<ErrorResponse>
|
99
|
+
<Error>
|
100
|
+
<Code>RequestThrottled</Code>
|
101
|
+
<Message>Request from 192.168.0.1 is throttled.</Message>
|
102
|
+
</Error>
|
103
|
+
<RequestID>123</RequestID>
|
104
|
+
</ErrorResponse>
|
105
|
+
XML
|
106
|
+
|
107
|
+
response_class.new body, 503
|
108
|
+
end
|
109
|
+
|
110
|
+
before do
|
111
|
+
request.stub!(:get).and_return mock_response
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'raises a Bad Response error' do
|
115
|
+
expect do
|
116
|
+
request.get!
|
117
|
+
end.to raise_error Vacuum::BadResponse, '503 RequestThrottled'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'when response is OK' do
|
122
|
+
let(:mock_response) do
|
123
|
+
response_class.new('', 200)
|
124
|
+
end
|
125
|
+
|
126
|
+
before do
|
127
|
+
request.stub!(:get).and_return mock_response
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'returns the response' do
|
131
|
+
request.get!.should eql mock_response
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
89
136
|
describe '#parameters' do
|
90
137
|
it 'includes default parameters' do
|
91
138
|
request.parameters.should have_key 'AWSAccessKeyId'
|
@@ -3,6 +3,10 @@ require 'spec_helper'
|
|
3
3
|
module Vacuum
|
4
4
|
module Request
|
5
5
|
describe ProductAdvertising do
|
6
|
+
let(:mock_response) do
|
7
|
+
Response::ProductAdvertising.new '', 200
|
8
|
+
end
|
9
|
+
|
6
10
|
let(:request) do
|
7
11
|
described_class.new do |config|
|
8
12
|
config.key = 'key'
|
@@ -15,7 +19,7 @@ module Vacuum
|
|
15
19
|
|
16
20
|
describe '#look_up' do
|
17
21
|
before do
|
18
|
-
request.stub!
|
22
|
+
request.stub!(:get).and_return mock_response
|
19
23
|
end
|
20
24
|
|
21
25
|
let(:parameters) do
|
@@ -71,6 +75,10 @@ module Vacuum
|
|
71
75
|
request.parameters
|
72
76
|
end
|
73
77
|
|
78
|
+
before do
|
79
|
+
request.stub!(:get).and_return mock_response
|
80
|
+
end
|
81
|
+
|
74
82
|
context 'when given a search index and a keyword' do
|
75
83
|
before do
|
76
84
|
request.search :foo, 'bar'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vacuum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-04-04 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70156392348820 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0.9'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70156392348820
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70156392348380 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2.9'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70156392348380
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: addressable
|
38
|
-
requirement: &
|
38
|
+
requirement: &70156392347700 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '2.2'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70156392347700
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: faraday
|
49
|
-
requirement: &
|
49
|
+
requirement: &70156392347120 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 0.7.6
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70156392347120
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: nokogiri
|
60
|
-
requirement: &
|
60
|
+
requirement: &70156392346540 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '1.5'
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70156392346540
|
69
69
|
description: ! 'Vacuum is a wrapper to various Amazon Web Services (AWS) APIs, including
|
70
70
|
|
71
71
|
Product Advertising and Marketplace Web Services (MWS).
|