vacuum 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,102 +2,81 @@ require 'spec_helper'
2
2
 
3
3
  module Vacuum
4
4
  describe Request do
5
- let(:req) { Request.new('us') }
6
-
7
- describe '#<<' do
8
- before do
9
- req.configure do |c|
10
- c.key = 'foo'
11
- c.tag = 'bar'
12
- end
5
+ let(:req) do
6
+ Request.new :locale => :us,
7
+ :key => 'foo',
8
+ :secret => 'bar',
9
+ :tag => 'baz'
10
+ end
13
11
 
14
- req.reset!
12
+ describe ".new" do
13
+ it 'raises an error if key is missing' do
14
+ expect do
15
+ Request.new :secret => 'foo',
16
+ :tag => 'bar'
17
+ end.to raise_error MissingKey
15
18
  end
16
19
 
17
- it 'merges parameters into the query' do
18
- req << { 'Key' => 'value' }
19
-
20
- req.params['Key'].should eql 'value'
20
+ it 'raises an error if secret is missing' do
21
+ expect do
22
+ Request.new :key => 'foo',
23
+ :tag => 'bar'
24
+ end.to raise_error MissingSecret
21
25
  end
22
26
 
23
- it 'camelizes keys' do
24
- req << { :some_key => 'value' }
25
-
26
- req.params.should have_key 'SomeKey'
27
+ it 'raises an error if tag is missing' do
28
+ expect do
29
+ Request.new :key => 'foo',
30
+ :secret => 'bar'
31
+ end.to raise_error MissingTag
27
32
  end
28
33
 
29
- it 'leaves camelized keys as is' do
30
- req << { 'SomeKey' => 'value' }
31
-
32
- req.params.should have_key 'SomeKey'
34
+ it 'raises an error if locale is not valid' do
35
+ expect do
36
+ Request.new :key => 'foo',
37
+ :secret => 'bar',
38
+ :tag => 'baz',
39
+ :locale => 'bad'
40
+ end.to raise_error BadLocale
33
41
  end
42
+ end
34
43
 
35
- it 'casts numeric values to string' do
36
- req << { 'Key' => 1 }
37
-
38
- req.params['Key'].should eql '1'
44
+ describe '#build' do
45
+ it 'merges parameters into the query' do
46
+ req.build 'Key' => 'value'
47
+ req.params['Key'].should eql 'value'
39
48
  end
40
49
 
41
- it 'converts array values to string' do
42
- req << { 'Key' => ['foo', 'bar'] }
50
+ it 'casts values to string' do
51
+ req.build 'Key' => 1
52
+ req.params['Key'].should eql '1'
43
53
 
54
+ req.build 'Key' => ['foo', 'bar']
44
55
  req.params['Key'].should eql 'foo,bar'
45
56
  end
46
57
 
47
- it 'removes whitespace after commas in values' do
48
- req << { 'Key' => 'foo, bar' }
49
-
50
- req.params['Key'].should eql 'foo,bar'
58
+ it 'returns self' do
59
+ req.build({}).should eql req
51
60
  end
52
61
  end
53
62
 
54
- describe '#configure' do
55
- it 'yields the locale' do
56
- req.configure(&:class).should eql Locale
63
+ describe '#build!' do
64
+ it 'clears existing query' do
65
+ req.build 'Key' => 'value'
66
+ req.params.should have_key 'Key'
67
+
68
+ req.build!.params.should_not have_key 'Key'
57
69
  end
58
70
  end
59
71
 
60
72
  describe '#get' do
61
- before do
62
- req.configure do |c|
63
- c.key = 'foo'
64
- c.secret = 'bar'
65
- c.tag = 'baz'
66
- end
67
- end
68
-
69
73
  it 'returns a response' do
70
74
  req.get.should be_a Response
71
75
  end
72
-
73
- it 'raises an error if secret is missing' do
74
- req.configure { |c| c.secret = nil }
75
-
76
- expect { req.get }.to raise_error MissingSecret
77
- end
78
76
  end
79
77
 
80
78
  describe '#params' do
81
- before do
82
- req.configure do |c|
83
- c.key = 'foo'
84
- c.tag = 'bar'
85
- end
86
- end
87
-
88
- it 'raises an error if key is missing' do
89
- req.configure { |c| c.key = nil }
90
-
91
- expect { req.params }.to raise_error MissingKey
92
- end
93
-
94
- it 'raises an error if tag is missing' do
95
- req.configure { |c| c.tag = nil }
96
-
97
- expect { req.params }.to raise_error MissingTag
98
- end
99
-
100
- it 'includes common request parameters' do
79
+ it 'includes shared request parameters' do
101
80
  req.params['Service'].should eql 'AWSECommerceService'
102
81
  end
103
82
 
@@ -110,46 +89,21 @@ module Vacuum
110
89
  req.params['Timestamp'].should =~ /^\d+-\d+-\d+T\d+:\d+:\d+Z$/
111
90
  end
112
91
 
113
- context 'when no API version is specified' do
92
+ context 'when no API version is given' do
114
93
  it 'includes the current API version' do
115
94
  req.params['Version'].should eql Request::CURRENT_API_VERSION
116
95
  end
117
96
  end
118
97
 
119
- context 'when an API version is specified' do
120
- it 'includes the specified API version' do
121
- req << { 'Version' => '1' }
98
+ context 'when an API version is given' do
99
+ it 'includes the given API version' do
100
+ req.build 'Version' => '1'
122
101
  req.params['Version'].should eql '1'
123
102
  end
124
103
  end
125
104
  end
126
105
 
127
- describe '#reset!' do
128
- before do
129
- req.configure do |c|
130
- c.key = 'foo'
131
- c.tag = 'bar'
132
- end
133
- end
134
-
135
- it 'resets the request parameters' do
136
- req << { 'Key' => 'value' }
137
- req.params.should have_key 'Key'
138
-
139
- req.reset!
140
- req.params.should_not have_key 'Key'
141
- end
142
- end
143
-
144
106
  describe '#url' do
145
- before do
146
- req.configure do |c|
147
- c.key = 'foo'
148
- c.secret = 'bar'
149
- c.tag = 'baz'
150
- end
151
- end
152
-
153
107
  it 'builds a URL' do
154
108
  req.url.should be_a URI::HTTP
155
109
  end
@@ -159,25 +113,18 @@ module Vacuum
159
113
  end
160
114
 
161
115
  it 'sorts the request parameters' do
162
- req << { 'A' => 1 }
116
+ req.build 'A' => 1
163
117
  req.url.query.should match /^A=1&/
164
118
  end
165
119
 
166
120
  it 'URL-encodes values' do
167
- req << { :key => 'foo,bar' }
121
+ req.build 'Key' => 'foo,bar'
168
122
  req.url.query.should match /foo%2Cbar/
169
123
  end
170
124
 
171
- it 'signs the query' do
125
+ it 'is signed' do
172
126
  req.url.query.should match /&Signature=/
173
127
  end
174
-
175
- it 'raises an error if no secret is specified' do
176
- expect do
177
- req.configure { |c| c.secret = nil }
178
- req.url
179
- end.to raise_error MissingSecret
180
- end
181
128
  end
182
129
  end
183
130
  end
@@ -2,94 +2,78 @@ require 'spec_helper'
2
2
 
3
3
  module Vacuum
4
4
  describe Response do
5
- let(:resp) do
5
+ let(:res) do
6
6
  body = File.read(File.expand_path('../../fixtures/http_response', __FILE__))
7
- code = '200'
8
- Response.new(body, code)
9
- end
10
-
11
- describe '#each' do
12
- it 'yields matches to given block' do
13
- yielded = false
14
- resp.each('Item') do |item|
15
- yielded = true
16
- end
17
-
18
- yielded.should be_true
19
- end
7
+ Response.new(body, '200')
20
8
  end
21
9
 
22
10
  describe '#errors' do
23
11
  it 'returns an array of errors' do
24
- resp.body = <<-EOF.gsub!(/>\s+</, '><').strip!
12
+ res.body = <<-EOF.gsub!(/>\s+</, '><').strip!
25
13
  <?xml version=\"1.0\" ?>
26
- <resp xmlns="http://example.com">
14
+ <Response xmlns="http://example.com">
27
15
  <Errors>
28
16
  <Error>foo</Error>
29
17
  </Errors>
30
- </resp>
18
+ </Response>
31
19
  EOF
32
20
 
33
- resp.errors.should =~ ['foo']
21
+ res.errors.should =~ ['foo']
34
22
  end
35
23
  end
36
24
 
37
- describe '#has_errors?' do
38
- context 'when a resp does not contain any errors' do
39
- it 'returns false' do
40
- resp.stub!(:errors).and_return([])
41
-
42
- resp.should_not have_errors
43
- end
25
+ describe '#find' do
26
+ it 'returns an array of matching nodes' do
27
+ res.find('ASIN').should_not be_empty
44
28
  end
45
29
 
46
- context 'when a resp contains errors' do
47
- it 'returns true' do
48
- resp.stub!(:errors).and_return([1])
49
-
50
- resp.should have_errors
51
- end
30
+ it 'yields matches to a block if given one' do
31
+ titles = res.find('Item') { |item| item['ItemAttributes']['Title'] }
32
+ titles.count.should eql 2
33
+ titles.each { |title| title.should be_a String }
52
34
  end
53
35
  end
54
36
 
55
- describe '#find' do
56
- it 'returns an array of matching nodes' do
57
- resp.find('ASIN').should_not be_empty
37
+ describe '#has_errors?' do
38
+ context 'when response does not contain any errors' do
39
+ it 'returns false' do
40
+ res.stub!(:errors).and_return([])
41
+ res.should_not have_errors
42
+ end
58
43
  end
59
- end
60
-
61
- describe "#map" do
62
- it "yields each match to a block and maps returned values" do
63
- titles = resp.map('Item') { |i| i['ItemAttributes']['Title'] }
64
44
 
65
- titles.count.should eql 2
45
+ context 'when response contains errors' do
46
+ it 'returns true' do
47
+ res.stub!(:errors).and_return([1])
48
+ res.should have_errors
49
+ end
66
50
  end
67
51
  end
68
52
 
69
53
  describe '#to_hash' do
70
- it 'casts resp to a hash' do
71
- resp.to_hash.should be_a Hash
54
+ it 'casts response to a hash' do
55
+ res.to_hash.should be_a Hash
72
56
  end
73
57
  end
74
58
 
75
59
  describe '#valid?' do
76
60
  context 'when HTTP status is OK' do
77
61
  it 'returns true' do
78
- resp.should be_valid
62
+ res.should be_valid
79
63
  end
80
64
  end
81
65
 
82
66
  context 'when HTTP status is not OK' do
83
67
  it 'returns false' do
84
- resp.code = 403
85
- resp.should_not be_valid
68
+ res.code = 403
69
+ res.should_not be_valid
86
70
  end
87
71
  end
88
72
  end
89
73
 
90
74
  describe '#xml' do
91
75
  it 'returns a Nokogiri document' do
92
- resp.xml.should be_an_instance_of Nokogiri::XML::Document
76
+ res.xml.should be_an_instance_of Nokogiri::XML::Document
93
77
  end
94
78
  end
95
79
  end
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.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-27 00:00:00.000000000 Z
12
+ date: 2012-03-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
- requirement: &70206980398600 !ruby/object:Gem::Requirement
16
+ requirement: &70211676691980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,37 +21,28 @@ dependencies:
21
21
  version: '1.4'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70206980398600
24
+ version_requirements: *70211676691980
25
25
  description: Vacuum is a Ruby wrapper to the Amazon Product Advertising API.
26
26
  email:
27
- - code@papercavalier.com
27
+ - hakan.ensari@papercavalier.com
28
28
  executables: []
29
29
  extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
32
- - lib/vacuum/builder.rb
33
- - lib/vacuum/cart.rb
34
- - lib/vacuum/cart_operations.rb
35
- - lib/vacuum/error.rb
36
- - lib/vacuum/locale.rb
37
- - lib/vacuum/lookup_operations.rb
32
+ - lib/vacuum/em.rb
33
+ - lib/vacuum/hash_builder.rb
38
34
  - lib/vacuum/request.rb
39
35
  - lib/vacuum/response.rb
40
- - lib/vacuum/search_operations.rb
41
36
  - lib/vacuum/version.rb
42
37
  - lib/vacuum.rb
43
38
  - LICENSE
44
39
  - README.md
45
40
  - spec/fixtures/http_response
46
41
  - spec/spec_helper.rb
47
- - spec/vacuum/builder_spec.rb
48
- - spec/vacuum/cart_operations_spec.rb
49
- - spec/vacuum/cart_spec.rb
50
- - spec/vacuum/lookup_operations_spec.rb
42
+ - spec/vacuum/hash_builder_spec.rb
51
43
  - spec/vacuum/request_spec.rb
52
44
  - spec/vacuum/response_spec.rb
53
- - spec/vacuum/search_operations_spec.rb
54
- homepage: http://code.papercavalier.com/vacuum/
45
+ homepage: http://github.com/hakanensari/vacuum
55
46
  licenses: []
56
47
  post_install_message:
57
48
  rdoc_options: []
@@ -71,17 +62,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
62
  version: '0'
72
63
  requirements: []
73
64
  rubyforge_project:
74
- rubygems_version: 1.8.10
65
+ rubygems_version: 1.8.11
75
66
  signing_key:
76
67
  specification_version: 3
77
68
  summary: A Ruby wrapper to the Amazon Product Advertising API
78
69
  test_files:
79
70
  - spec/fixtures/http_response
80
71
  - spec/spec_helper.rb
81
- - spec/vacuum/builder_spec.rb
82
- - spec/vacuum/cart_operations_spec.rb
83
- - spec/vacuum/cart_spec.rb
84
- - spec/vacuum/lookup_operations_spec.rb
72
+ - spec/vacuum/hash_builder_spec.rb
85
73
  - spec/vacuum/request_spec.rb
86
74
  - spec/vacuum/response_spec.rb
87
- - spec/vacuum/search_operations_spec.rb
data/lib/vacuum/cart.rb DELETED
@@ -1,90 +0,0 @@
1
- module Vacuum
2
- class Cart
3
- # @return [String] cart_id
4
- attr :id
5
-
6
- # @return [String] hmac
7
- attr :hmac
8
-
9
- attr :items
10
-
11
- # @return [Vacuum::Response] last_response last response
12
- # returned by the Amazon API
13
- attr :last_response
14
-
15
- # @return [String] purchase_url
16
- attr :purchase_url
17
-
18
- attr :sub_total
19
-
20
- # Creates a new cart
21
- #
22
- # @param [Vacuum::Request] req an API request
23
- # @param [Hash] params a hash of parameters
24
- def initialize(req, params)
25
- @req = req
26
- get 'Create', params
27
- end
28
-
29
- # Clears the cart
30
- #
31
- # @param [Hash] params a hash of parameters
32
- def clear(params = {})
33
- get 'Clear', params
34
- end
35
-
36
- private
37
-
38
- def get(operation, params)
39
- @req.reset!
40
-
41
- if id
42
- @req << { 'CartId' => id,
43
- 'HMAC' => hmac }
44
- end
45
-
46
- @req << { 'Operation' => "Cart#{operation}" }.merge(params)
47
-
48
- @last_response = @req.get
49
- @items = @last_response.find('CartItems')
50
- @id = @last_response.find('CartId').first
51
- @hmac = @last_response.find('HMAC').first
52
- @purchase_url = @last_response.find('PurchaseURL').first
53
- @sub_total = @last_response.find('SubTotal').first
54
- end
55
-
56
- # Add items to cart
57
- #
58
- # @param [String] cart_id
59
- # @param [String] hmac
60
- # @param [Hash] params
61
- # @return [Vacuum::Cart] a response
62
- # def add_to_cart(cart_id, hmac, params)
63
- # cartify 'Add', { 'CartId' => cart_id,
64
- # 'HMAC' => hmac }.merge(params)
65
- # end
66
-
67
-
68
- # Gets an existing cart
69
- #
70
- # @param [String] cart_id
71
- # @param [String] hmac
72
- # @param [Hash] params
73
- # @return [Vacuum::Cart] a response
74
- # def get_cart(cart_id, hmac, params)
75
- # cartify 'Get', { 'CartId' => cart_id,
76
- # 'HMAC' => hmac }.merge(params)
77
- # end
78
-
79
- # Modifies an existing cart
80
- #
81
- # @param [String] cart_id
82
- # @param [String] hmac
83
- # @param [Hash] params
84
- # @return [Vacuum::Cart] a response
85
- # def modify_cart(cart_id, hmac, params)
86
- # cartify 'Modify', { 'CartId' => cart_id,
87
- # 'HMAC' => hmac }.merge(params)
88
- # end
89
- end
90
- end
@@ -1,12 +0,0 @@
1
- module Vacuum
2
- # Cart operations
3
- module CartOperations
4
- # Creates a cart
5
- #
6
- # @param [Hash] params
7
- # @return [Vacuum::Cart] a cart
8
- def create_cart(params)
9
- Cart.new(self, params)
10
- end
11
- end
12
- end
data/lib/vacuum/error.rb DELETED
@@ -1,13 +0,0 @@
1
- module Vacuum
2
- # Raised when a bad locale is specified
3
- class BadLocale < ArgumentError; end
4
-
5
- # Raised when the Amazon key is not specified
6
- class MissingKey < ArgumentError; end
7
-
8
- # Raised when the Amazon secret is not specified
9
- class MissingSecret < ArgumentError; end
10
-
11
- # Raised when the Amazon associate tag is not specified
12
- class MissingTag < ArgumentError; end
13
- end
data/lib/vacuum/locale.rb DELETED
@@ -1,39 +0,0 @@
1
- module Vacuum
2
- # An Amazon locale
3
- class Locale
4
- # Amazon hosts
5
- HOSTS = { :ca => 'ecs.amazonaws.ca',
6
- :cn => 'webservices.amazon.cn',
7
- :de => 'ecs.amazonaws.de',
8
- :es => 'webservices.amazon.es',
9
- :fr => 'ecs.amazonaws.fr',
10
- :it => 'webservices.amazon.it',
11
- :jp => 'ecs.amazonaws.jp',
12
- :us => 'ecs.amazonaws.com',
13
- :uk => 'ecs.amazonaws.co.uk' }
14
-
15
- # Country codes for Amazon locales
16
- LOCALES = HOSTS.keys
17
-
18
- # @return [String] the Amazon Web Services access key
19
- attr_accessor :key
20
-
21
- # @return [String] the Amazon Web Services secret
22
- attr_accessor :secret
23
-
24
- # @return [String] the Amazon associate tag
25
- attr_accessor :tag
26
-
27
- # @param [Symbol] locale the locale key
28
- # @raise [Vacuum::BadLocale] locale is bad
29
- def initialize(locale)
30
- raise BadLocale unless LOCALES.include?(locale)
31
- @locale = locale
32
- end
33
-
34
- # @return [String] the Amazon host
35
- def host
36
- HOSTS[@locale]
37
- end
38
- end
39
- end
@@ -1,62 +0,0 @@
1
- module Vacuum
2
- # Lookup operations
3
- module LookupOperations
4
- # Given up to ten item ids, returns some or all of the item
5
- # attributes, depending on the response group specified in the
6
- # request.
7
- #
8
- # @param [Array] item_ids splat of item IDs and an optional hash of
9
- # parameters
10
- # @return [Vacuum::Response] a response
11
- #
12
- # Id Type defaults to ASIN.
13
- #
14
- # @example The following returns some basic information for the
15
- # ASIN 0679753354.
16
- #
17
- # req.find('0679753354')
18
- #
19
- # @example The following request returns cover art for the same
20
- # ASIN.
21
- #
22
- # req.find('0679753354', :response_group => 'Images')
23
- #
24
- def find(*item_ids)
25
- reset!
26
- params = item_ids.last.is_a?(Hash) ? item_ids.pop : {}
27
- self.<<({ 'Operation' => 'ItemLookup',
28
- 'ItemId' => item_ids }.merge(params))
29
-
30
- get
31
- end
32
-
33
- # Given a browse node ID, returns the specified browse node’s name,
34
- # children, and ancestors.
35
- #
36
- # @param [String] browse_node_id browse node ID
37
- # @params [Hash] params hash of parameters
38
- # @return [Vacuum::Response] a response
39
- def find_browse_node(browse_node_id, params = {})
40
- reset!
41
- self.<<({ 'Operation' => 'BrowseNodeLookup',
42
- 'BrowseNodeId' => browse_node_id }.merge(params))
43
-
44
- get
45
- end
46
-
47
- # Given up to ten item ids, returns up to ten products per page
48
- # that are similar to those items
49
- #
50
- # @param [Array] item_ids splat of item IDs and an optional hash of
51
- # parameters
52
- # @return [Vacuum::Response] a response
53
- def find_similar(*item_ids)
54
- reset!
55
- params = item_ids.last.is_a?(Hash) ? item_ids.pop : {}
56
- self.<<({ 'Operation' => 'SimilarityLookup',
57
- 'ItemId' => item_ids }.merge(params))
58
-
59
- get
60
- end
61
- end
62
- end