amazon-ecs 0.5.6 → 0.5.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/CHANGELOG +4 -0
  2. data/README +1 -1
  3. data/lib/amazon/ecs.rb +14 -2
  4. data/test/amazon/ecs_test.rb +103 -100
  5. metadata +2 -2
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 0.5.7 2009-08-28
2
+ ----------------
3
+ * Added support for to sign request using openssl with fallback on ruby-hmac
4
+
1
5
  0.5.6 2009-07-21
2
6
  ----------------
3
7
  * Update parameter value encoding to support special characters
data/README CHANGED
@@ -12,7 +12,7 @@ If in the future, there is a change in REST XML output structure,
12
12
  no changes will be required on <tt>amazon-ecs</tt> library,
13
13
  instead you just need to change the element path.
14
14
 
15
- Version: 0.5.6
15
+ Version: 0.5.7
16
16
 
17
17
  == INSTALLATION
18
18
 
@@ -26,6 +26,7 @@ require 'hpricot'
26
26
  require 'cgi'
27
27
  require 'hmac-sha2'
28
28
  require 'base64'
29
+ require 'openssl'
29
30
 
30
31
  module Amazon
31
32
  class RequestError < StandardError; end
@@ -39,6 +40,11 @@ module Amazon
39
40
  :fr => 'http://webservices.amazon.fr/onca/xml?'
40
41
  }
41
42
 
43
+ OPENSSL_DIGEST_SUPPORT = OpenSSL::Digest.constants.include?( 'SHA256' ) ||
44
+ OpenSSL::Digest.constants.include?( :SHA256 )
45
+
46
+ OPENSSL_DIGEST = OpenSSL::Digest::Digest.new( 'sha256' ) if OPENSSL_DIGEST_SUPPORT
47
+
42
48
  @@options = {
43
49
  :version => "2009-01-06",
44
50
  :service => "AWSECommerceService"
@@ -245,10 +251,16 @@ module Amazon
245
251
 
246
252
  def self.sign_request(url, key)
247
253
  return nil if key.nil?
248
- signature = URI.escape( Base64.encode64( HMAC::SHA256.digest(key, url) ).strip, Regexp.new("[+=]") )
254
+
255
+ if (OPENSSL_DIGEST_SUPPORT)
256
+ signature = OpenSSL::HMAC.digest(OPENSSL_DIGEST, key, url)
257
+ signature = [signature].pack('m').chomp
258
+ else
259
+ signature = Base64.encode64( HMAC::SHA256.digest(key, url) ).strip
260
+ end
261
+ signature = URI.escape(signature, Regexp.new("[+=]"))
249
262
  return signature
250
263
  end
251
-
252
264
  end
253
265
 
254
266
  # Internal wrapper class to provide convenient method to access Hpricot element value.
@@ -16,108 +16,111 @@ class Amazon::EcsTest < Test::Unit::TestCase
16
16
 
17
17
  ## Test item_search
18
18
  def test_item_search
19
- resp = Amazon::Ecs.item_search('ruby')
20
-
21
- assert(resp.is_valid_request?)
22
- assert(resp.total_results >= 3600)
23
- assert(resp.total_pages >= 360)
24
-
25
- signature_elements = (resp.doc/"arguments/argument").select {|ele| ele.attributes['name'] == 'Signature' }.length
26
- assert(signature_elements == 1)
27
- end
28
-
29
- def test_item_search_with_special_characters
30
- Amazon::Ecs.debug = true
31
- resp = Amazon::Ecs.item_search('()*&^%$')
32
- assert(resp.is_valid_request?)
33
- end
34
-
35
- def test_item_search_with_paging
36
- resp = Amazon::Ecs.item_search('ruby', :item_page => 2)
37
- assert resp.is_valid_request?
38
- assert 2, resp.item_page
39
- end
40
-
41
- def test_item_search_with_invalid_request
42
- resp = Amazon::Ecs.item_search(nil)
43
- assert !resp.is_valid_request?
44
- end
45
-
46
- def test_item_search_with_no_result
47
- resp = Amazon::Ecs.item_search('afdsafds')
48
-
49
- assert resp.is_valid_request?
50
- assert_equal "We did not find any matches for your request.",
51
- resp.error
52
- end
53
-
54
- def test_item_search_uk
55
- resp = Amazon::Ecs.item_search('ruby', :country => :uk)
56
- assert resp.is_valid_request?
57
- end
19
+ resp = Amazon::Ecs.item_search('ruby')
20
+
21
+ assert(resp.is_valid_request?)
22
+ assert(resp.total_results >= 3600)
23
+ assert(resp.total_pages >= 360)
24
+
25
+ signature_elements = (resp.doc/"arguments/argument").select do |ele|
26
+ ele.attributes['name'] == 'Signature' || ele.attributes['Name'] == 'Signature'
27
+ end.length
28
+
29
+ assert(signature_elements == 1)
30
+ end
31
+
32
+ def test_item_search_with_special_characters
33
+ Amazon::Ecs.debug = true
34
+ resp = Amazon::Ecs.item_search('()*&^%$')
35
+ assert(resp.is_valid_request?)
36
+ end
58
37
 
59
- def test_item_search_by_author
60
- resp = Amazon::Ecs.item_search('dave', :type => :author)
61
- assert resp.is_valid_request?
62
- end
38
+ def test_item_search_with_paging
39
+ resp = Amazon::Ecs.item_search('ruby', :item_page => 2)
40
+ assert resp.is_valid_request?
41
+ assert 2, resp.item_page
42
+ end
63
43
 
64
- def test_item_get
65
- resp = Amazon::Ecs.item_search("0974514055")
66
- item = resp.first_item
67
-
68
- # test get
69
- assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition",
70
- item.get("itemattributes/title")
71
-
72
- # test get_array
73
- assert_equal ['Dave Thomas', 'Chad Fowler', 'Andy Hunt'],
74
- item.get_array("author")
75
-
76
- # test get_hash
77
- small_image = item.get_hash("smallimage")
78
-
79
- assert_equal 3, small_image.keys.size
80
- assert_match ".jpg", small_image[:url]
81
- assert_equal "75", small_image[:height]
82
- assert_equal "59", small_image[:width]
83
-
84
- # test /
85
- reviews = item/"editorialreview"
86
- reviews.each do |review|
87
- # returns unescaped HTML content, Hpricot escapes all text values
88
- assert Amazon::Element.get_unescaped(review, 'source')
89
- assert Amazon::Element.get_unescaped(review, 'content')
90
- end
91
- end
44
+ def test_item_search_with_invalid_request
45
+ resp = Amazon::Ecs.item_search(nil)
46
+ assert !resp.is_valid_request?
47
+ end
92
48
 
93
- ## Test item_lookup
94
- def test_item_lookup
95
- resp = Amazon::Ecs.item_lookup('0974514055')
96
- assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition",
97
- resp.first_item.get("itemattributes/title")
98
- end
49
+ def test_item_search_with_no_result
50
+ resp = Amazon::Ecs.item_search('afdsafds')
51
+
52
+ assert resp.is_valid_request?
53
+ assert_equal "We did not find any matches for your request.",
54
+ resp.error
55
+ end
56
+
57
+ def test_item_search_uk
58
+ resp = Amazon::Ecs.item_search('ruby', :country => :uk)
59
+ assert resp.is_valid_request?
60
+ end
61
+
62
+ def test_item_search_by_author
63
+ resp = Amazon::Ecs.item_search('dave', :type => :author)
64
+ assert resp.is_valid_request?
65
+ end
66
+
67
+ def test_item_get
68
+ resp = Amazon::Ecs.item_search("0974514055")
69
+ item = resp.first_item
70
+
71
+ # test get
72
+ assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition",
73
+ item.get("itemattributes/title")
74
+
75
+ # test get_array
76
+ assert_equal ['Dave Thomas', 'Chad Fowler', 'Andy Hunt'],
77
+ item.get_array("author")
99
78
 
100
- def test_item_lookup_with_invalid_request
101
- resp = Amazon::Ecs.item_lookup(nil)
102
- assert resp.has_error?
103
- assert resp.error
104
- end
105
-
106
- def test_item_lookup_with_no_result
107
- resp = Amazon::Ecs.item_lookup('abc')
108
-
109
- assert resp.is_valid_request?
110
- assert_match(/ABC is not a valid value for ItemId/, resp.error)
111
- end
79
+ # test get_hash
80
+ small_image = item.get_hash("smallimage")
81
+
82
+ assert_equal 3, small_image.keys.size
83
+ assert_match ".jpg", small_image[:url]
84
+ assert_equal "75", small_image[:height]
85
+ assert_equal "59", small_image[:width]
86
+
87
+ # test /
88
+ reviews = item/"editorialreview"
89
+ reviews.each do |review|
90
+ # returns unescaped HTML content, Hpricot escapes all text values
91
+ assert Amazon::Element.get_unescaped(review, 'source')
92
+ assert Amazon::Element.get_unescaped(review, 'content')
93
+ end
94
+ end
95
+
96
+ ## Test item_lookup
97
+ def test_item_lookup
98
+ resp = Amazon::Ecs.item_lookup('0974514055')
99
+ assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition",
100
+ resp.first_item.get("itemattributes/title")
101
+ end
102
+
103
+ def test_item_lookup_with_invalid_request
104
+ resp = Amazon::Ecs.item_lookup(nil)
105
+ assert resp.has_error?
106
+ assert resp.error
107
+ end
112
108
 
113
- def test_search_and_convert
114
- resp = Amazon::Ecs.item_lookup('0974514055')
115
- title = resp.first_item.get("itemattributes/title")
116
- authors = resp.first_item.search_and_convert("author")
117
-
118
- assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition", title
119
- assert authors.is_a?(Array)
120
- assert 3, authors.size
121
- assert_equal "Dave Thomas", authors.first.get
122
- end
123
- end
109
+ def test_item_lookup_with_no_result
110
+ resp = Amazon::Ecs.item_lookup('abc')
111
+
112
+ assert resp.is_valid_request?
113
+ assert_match(/ABC is not a valid value for ItemId/, resp.error)
114
+ end
115
+
116
+ def test_search_and_convert
117
+ resp = Amazon::Ecs.item_lookup('0974514055')
118
+ title = resp.first_item.get("itemattributes/title")
119
+ authors = resp.first_item.search_and_convert("author")
120
+
121
+ assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition", title
122
+ assert authors.is_a?(Array)
123
+ assert 3, authors.size
124
+ assert_equal "Dave Thomas", authors.first.get
125
+ end
126
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon-ecs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Herryanto Siatono
@@ -9,7 +9,7 @@ autorequire: name
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-21 00:00:00 +08:00
12
+ date: 2009-08-28 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency