vacuum 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2187eb118ab30e2d03a6aa4363456335614b2ea0
4
- data.tar.gz: d627561295a2da45b87030ccb10cef19461070ef
3
+ metadata.gz: d688557c2a30832e793deb6f932f78e38f3fe2eb
4
+ data.tar.gz: 762f70ee270015de03c5ba177724bcea03fcac9f
5
5
  SHA512:
6
- metadata.gz: 50fc2693d3e43a4ca83279d4a39c162ea9bef69f722b4a70fbf9bf0816a7058bd6dc03c919be5c1d95b00e6c6ae7a2e51e88649d4f6083f338c96fbc75ef69fc
7
- data.tar.gz: 27e153888b98328823574adc22f5ff93e9420998b025163d5ae0cfd0e98189747417d194b0d08a5db6f0b5256679ac5a6e00f84e2507db72ab15350ac73de69a
6
+ metadata.gz: 4f4d7fc84ef456bbdc31b6ce81169eb9799ce79c1112ec0a1e9e35cbf6a619dceadda25e4386458b0ecdddec8bd1179216d98dc6451a8de5a511c717bdbfc5ea
7
+ data.tar.gz: c353c71626477896d12385b58c47049cdcdce226bd114c961904a3e3520fc2fcefac383527a3f5b9cc3b52ef5ff0f6abd26042a642c3ea629f688e849be9b0c0
data/README.md CHANGED
@@ -12,59 +12,50 @@ Set up a request:
12
12
 
13
13
  ```ruby
14
14
  req = Vacuum.new
15
- .configure(
16
- aws_access_key_id: 'foo',
17
- aws_secret_access_key: 'secret',
18
- associate_tag: 'biz-val'
19
- )
20
15
  ```
21
16
 
22
- The locale defaults to the US. If you wish to use another locale, specify its
23
- ISO-3166 two-letter code when instantiating the request:
17
+ The locale defaults to the US. If you wish to use another locale, instantiate
18
+ with its ISO-3166 two-letter country code:
24
19
 
25
20
  ```ruby
26
- Vacuum.new('GB')
21
+ req = Vacuum.new('GB')
27
22
  ```
28
23
 
29
- Make a request:
24
+ Configure the request credentials:
30
25
 
31
26
  ```ruby
32
- params = { 'Operation' => 'ItemSearch',
33
- 'SearchIndex' => 'Books',
34
- 'Keywords' => 'Architecture' }
35
-
36
- res = req.get(query: params)
27
+ req.configure(
28
+ aws_access_key_id: 'key',
29
+ aws_secret_access_key: 'secret',
30
+ associate_tag: 'tag'
31
+ )
37
32
  ```
38
33
 
39
- Once you have a response, parse it with your favourite XML parser and parsing
40
- method.
41
-
42
- If you don't mind the performance hit, here is a simplistic solution based on
43
- [`MultiXml`][5]:
34
+ Make a request:
44
35
 
45
36
  ```ruby
46
- require 'forwardable'
47
- require 'multi_xml'
48
-
49
- class Response
50
- extend Forwardable
37
+ params = {
38
+ 'SearchIndex' => 'Books',
39
+ 'Keywords' => 'Architecture'
40
+ }
41
+ res = req.item_search(params)
42
+ ```
51
43
 
52
- def_delegators :@response, :code, :body
44
+ Parse the response into a Ruby hash:
53
45
 
54
- def initialize(response)
55
- @response = response
56
- end
46
+ ```ruby
47
+ res.to_h
48
+ ```
57
49
 
58
- def to_h
59
- MultiXml.parse(body)
60
- end
61
- end
50
+ Allowed requests include `browse_node_lookup`, `cart_add`, `cart_clear`,
51
+ `cart_create`, `cart_get`, `cart_modify`, `item_lookup`, `item_search`,
52
+ `similarity_lookup`.
62
53
 
63
- Response.new(res).to_h
64
- ```
54
+ Vacuum is built with [excon][5] and [multi_xml][6].
65
55
 
66
56
  [1]: https://secure.travis-ci.org/hakanensari/vacuum.png
67
57
  [2]: http://travis-ci.org/hakanensari/vacuum
68
58
  [3]: http://f.cl.ly/items/2k2X0e2u0G3k1c260D2u/vacuum.png
69
59
  [4]: https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html
70
- [5]: https://github.com/sferik/multi_xml
60
+ [5]: https://github.com/geemus/excon
61
+ [6]: https://github.com/sferik/multi_xml
@@ -1,4 +1,5 @@
1
1
  require 'jeff'
2
+ require 'vacuum/response'
2
3
 
3
4
  module Vacuum
4
5
  # An Amazon Product Advertising API request.
@@ -7,7 +8,6 @@ module Vacuum
7
8
 
8
9
  BadLocale = Class.new(ArgumentError)
9
10
 
10
- # A list of Amazon Product Advertising API hosts.
11
11
  HOSTS = {
12
12
  'CA' => 'webservices.amazon.ca',
13
13
  'CN' => 'webservices.amazon.cn',
@@ -21,23 +21,34 @@ module Vacuum
21
21
  'US' => 'webservices.amazon.com'
22
22
  }.freeze
23
23
 
24
+ OPERATIONS = %w(
25
+ BrowseNodeLookup
26
+ CartAdd
27
+ CartClear
28
+ CartCreate
29
+ CartGet
30
+ CartModify
31
+ ItemLookup
32
+ ItemSearch
33
+ SimilarityLookup
34
+ ).freeze
35
+
24
36
  params 'AssociateTag' => -> { associate_tag },
25
37
  'Service' => 'AWSECommerceService',
26
38
  'Version' => '2011-08-01'
27
39
 
40
+ attr_accessor :associate_tag
41
+
28
42
  # Create a new request for given locale.
29
43
  #
30
44
  # locale - The String Product Advertising API locale (default: US).
45
+ # secure - Whether to use the secure version of the endpoint (default:
46
+ # false)
31
47
  #
32
48
  # Raises a Bad Locale error if locale is not valid.
33
- def initialize(locale = nil)
34
- if locale == 'UK'
35
- warn '[DEPRECATION] Use GB instead of UK'
36
- locale = 'GB'
37
- end
38
-
49
+ def initialize(locale = nil, secure = false)
39
50
  host = HOSTS[locale || 'US'] or raise BadLocale
40
- self.aws_endpoint = "http://#{host}/onca/xml"
51
+ self.aws_endpoint = "#{secure ? 'https' : 'http' }://#{host}/onca/xml"
41
52
  end
42
53
 
43
54
  # Configure the Amazon Product Advertising API request.
@@ -54,24 +65,21 @@ module Vacuum
54
65
  self
55
66
  end
56
67
 
57
- # Get/Sets the String Associate Tag.
58
- attr_accessor :associate_tag
59
- # Keep around old attribute for a while for backward compatibility.
60
- alias :tag :associate_tag
61
- alias :tag= :associate_tag=
68
+ OPERATIONS.each do |operation|
69
+ method_name = operation.gsub(/(.)([A-Z])/,'\1_\2').downcase
70
+ define_method(method_name) do |params, excon_options = {}|
71
+ Response.new(get(excon_options.merge(query: params.merge('Operation' => operation))))
72
+ end
73
+ end
62
74
 
63
75
  # Build a URL.
64
76
  #
65
- # params - A Hash of Amazon Product Advertising query params.
77
+ # params - A Hash of Amazon Product Advertising request parameters.
66
78
  #
67
79
  # Returns the built URL String.
68
80
  def url(params)
69
- opts = {
70
- method: :get,
71
- query: params
72
- }
73
-
74
- [aws_endpoint, build_options(opts).fetch(:query)].join('?')
81
+ options = { method: :get, query: params }
82
+ [aws_endpoint, build_options(options).fetch(:query)].join('?')
75
83
  end
76
84
  end
77
85
  end
@@ -0,0 +1,10 @@
1
+ require 'delegate'
2
+ require 'multi_xml'
3
+
4
+ module Vacuum
5
+ class Response < SimpleDelegator
6
+ def to_h
7
+ MultiXml.parse(body)
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Vacuum
2
- VERSION = '0.4.1'
2
+ VERSION = '0.5.0'
3
3
  end
data/lib/vacuum.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'forwardable'
2
-
3
2
  require 'vacuum/request'
4
3
  require 'vacuum/version'
5
4
 
@@ -8,6 +7,6 @@ module Vacuum
8
7
  class << self
9
8
  extend Forwardable
10
9
 
11
- def_delegator 'Vacuum::Request', :new
10
+ def_delegator Vacuum::Request, :new
12
11
  end
13
12
  end
@@ -0,0 +1,29 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/vacuum'
3
+
4
+ class TestVacuum < Minitest::Test
5
+ include Vacuum
6
+
7
+ def test_request_valid_locale
8
+ assert_raises(Request::BadLocale) { Request.new('foo') }
9
+ end
10
+
11
+ def test_defaults_to_us_endpoint
12
+ assert_equal 'http://webservices.amazon.com/onca/xml', Request.new.aws_endpoint
13
+ end
14
+
15
+ def test_returns_url
16
+ req = Request.new
17
+ req.configure(aws_access_key_id: 'key', aws_secret_access_key: 'secret', associate_tag: 'tag')
18
+ assert_match(/webservices.amazon.com.*Foo=Bar/, req.url('Foo' => 'Bar'))
19
+ end
20
+
21
+ def test_fetches_parsable_response
22
+ Excon.stub({}, { body: '<foo>bar</foo>' })
23
+ req = Request.new
24
+ req.configure(aws_access_key_id: 'key', aws_secret_access_key: 'secret', associate_tag: 'tag')
25
+ res = req.item_lookup({}, mock: true)
26
+ refute_empty res.to_h
27
+ Excon.stubs.clear
28
+ end
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vacuum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-12 00:00:00.000000000 Z
11
+ date: 2013-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jeff
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_xml
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -46,10 +60,12 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - lib/vacuum/request.rb
63
+ - lib/vacuum/response.rb
49
64
  - lib/vacuum/version.rb
50
65
  - lib/vacuum.rb
51
66
  - LICENSE
52
67
  - README.md
68
+ - test/test_vacuum.rb
53
69
  homepage: https://github.com/hakanensari/vacuum
54
70
  licenses:
55
71
  - MIT
@@ -74,4 +90,5 @@ rubygems_version: 2.0.3
74
90
  signing_key:
75
91
  specification_version: 4
76
92
  summary: Amazon Product Advertising in Ruby
77
- test_files: []
93
+ test_files:
94
+ - test/test_vacuum.rb