amazon-ecs 2.2.5 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +9 -9
  2. data/CHANGELOG +4 -0
  3. data/Readme.rdoc +15 -14
  4. data/lib/amazon/ecs.rb +62 -62
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTQxOWYzOWY2NjQ3YjBiZTFhNTdkMTQ0ODExYWFmOTUwYTEyODkzYw==
4
+ YmFlM2Q2N2Q3NWMzMDhmMzI2ZjZlYjIyOTM0ZWU1MmRmNjc1ZTg5NQ==
5
5
  data.tar.gz: !binary |-
6
- MDdkYTdlNmJiZjc1NmY1NzNlNDU3ZTI4MWJkNjRiMzJmNWU5MzQ3Yw==
7
- !binary "U0hBNTEy":
6
+ OGQwYzFiNTUyMzkxNTUyNzNmOTliNTVmNWM1NjZjNjE3ZGE2ZTE0ZA==
7
+ SHA512:
8
8
  metadata.gz: !binary |-
9
- MWZhN2E1ODllOTBjN2FkODI3OWIzMTQ3MjZmY2ViYWJhNTRlYzY0ZTkxYTRi
10
- Y2JhYjNkNTFlYWIwYTFjZjlhZDNhNTg2YmY1OGQwZDNhZGJlOGRhMzdlMTgw
11
- NmIwZDg4ZTI2ZmQyNzAzN2ZhZWI0M2QyNjRhYTQ4NWFkMmY5ZTI=
9
+ NDA2YjEyOGE4ZDE5NGZkYTY0NWU2ZWRhZmUzODJiYTAyODM2YmVhYzY4NWM1
10
+ ODc2ZDc0NjMyYmZlMjJkMjZmOTEzYmE1MzQ5NWRmODY1YTVlYzFlMzU4ZWJk
11
+ NWFiMmRiOWNmZTVmYzYwNWM2MjFlZGNiNzAwZjQyODNhMGYyZWM=
12
12
  data.tar.gz: !binary |-
13
- ZTgyMTI5MmJjMTAyMWU0ZTUwMmUxNmRiMGNlZjBmOWExYzcyZDhjMGIxMzky
14
- ZWQwZGI2OWI3NjM3OGUwZGM5MGJmODEwZmQzZWMxZGNmOTA3MDViNWNjOTY0
15
- ZDYwZmE0OWVjNWEwNjU3ODkyMDI0NTg5Mzg4YjQzZTE0OWY3ODc=
13
+ OWFlZjc0YWVlZWYyMmMzMThkNzAwNjUxN2U4MDM3ZDY0Y2E5MmYxY2VkMjc3
14
+ NzRiNDBlYzFkMDBmZTVhYjQyNTYxYjYxOTlkN2RmZmJkYWQ4MDEwNzU0OGQ1
15
+ ODIzOWEzZDQxZjZkNTJjYTY3NTBkZTkyZTlkYTU4ZDFjMDZiOGE=
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 2.3.0 2014-12-05
2
+ ----------------
3
+ * Replace deprecated URI::escape with CGI::escape
4
+
1
5
  2.2.5 2014-03-08
2
6
  ----------------
3
7
  * Replaced Digest::Digest deprecated call
@@ -20,25 +20,26 @@ For HPricot dependency implementation, please install 1.2.x version or checkout
20
20
  == EXAMPLE
21
21
 
22
22
  require 'amazon/ecs'
23
-
24
- # Set the default options; options will be camelized and converted to REST request parameters.
25
- # associate_tag and AWS_access_key_id are required options, associate_tag is required option
26
- # since API version 2011-08-01.
27
- #
28
- # To sign your request, include AWS_secret_key.
29
- Amazon::Ecs.options = {
30
- :associate_tag => '[your associate tag]',
31
- :AWS_access_key_id => '[your developer token]',
32
- :AWS_secret_key => '[your secret access key]'
33
- }
34
-
35
- # alternatively,
23
+
24
+ # Configure your access key, secret key and other options such as the associate tag.
25
+ # Options set in the configure block will add/update to the default options, i.e.
26
+ # options[:version] => "2011-08-01"
27
+ # options[:service] => "AWSECommerceService"
36
28
  Amazon::Ecs.configure do |options|
37
- options[:associate_tag] = '[your associate tag]'
38
29
  options[:AWS_access_key_id] = '[your access key]'
39
30
  options[:AWS_secret_key] = '[you secret key]'
31
+ options[:associate_tag] = '[your associate tag]'
40
32
  end
41
33
 
34
+ # Or if you need to replace the default options, set the options value directly.
35
+ # Amazon::Ecs.options = {
36
+ # :version => "2013-08-01",
37
+ # :service => "AWSECommerceService"
38
+ # :associate_tag => '[your associate tag]',
39
+ # :AWS_access_key_id => '[your developer token]',
40
+ # :AWS_secret_key => '[your secret access key]'
41
+ # }
42
+
42
43
  # options provided on method call will merge with the default options
43
44
  res = Amazon::Ecs.item_search('ruby', {:response_group => 'Medium', :sort => 'salesrank'})
44
45
 
@@ -30,10 +30,10 @@ require 'openssl'
30
30
 
31
31
  module Amazon
32
32
  class RequestError < StandardError; end
33
-
33
+
34
34
  class Ecs
35
- VERSION = '2.2.5'
36
-
35
+ VERSION = '2.3.0'
36
+
37
37
  SERVICE_URLS = {
38
38
  :us => 'http://ecs.amazonaws.com/onca/xml',
39
39
  :uk => 'http://ecs.amazonaws.co.uk/onca/xml',
@@ -45,57 +45,57 @@ module Amazon
45
45
  :cn => 'http://webservices.amazon.cn/onca/xml',
46
46
  :es => 'http://webservices.amazon.es/onca/xml'
47
47
  }
48
-
48
+
49
49
  OPENSSL_DIGEST_SUPPORT = OpenSSL::Digest.constants.include?( 'SHA256' ) ||
50
50
  OpenSSL::Digest.constants.include?( :SHA256 )
51
-
51
+
52
52
  OPENSSL_DIGEST = OpenSSL::Digest.new( 'sha256' ) if OPENSSL_DIGEST_SUPPORT
53
-
53
+
54
54
  @@options = {
55
55
  :version => "2011-08-01",
56
56
  :service => "AWSECommerceService"
57
57
  }
58
-
58
+
59
59
  @@debug = false
60
60
 
61
61
  # Default search options
62
62
  def self.options
63
63
  @@options
64
64
  end
65
-
65
+
66
66
  # Set default search options
67
67
  def self.options=(opts)
68
68
  @@options = opts
69
69
  end
70
-
70
+
71
71
  # Get debug flag.
72
72
  def self.debug
73
73
  @@debug
74
74
  end
75
-
75
+
76
76
  # Set debug flag to true or false.
77
77
  def self.debug=(dbg)
78
78
  @@debug = dbg
79
79
  end
80
-
80
+
81
81
  def self.configure(&proc)
82
82
  raise ArgumentError, "Block is required." unless block_given?
83
83
  yield @@options
84
84
  end
85
-
85
+
86
86
  # Search amazon items with search terms. Default search index option is 'Books'.
87
87
  # For other search type other than keywords, please specify :type => [search type param name].
88
88
  def self.item_search(terms, opts = {})
89
89
  opts[:operation] = 'ItemSearch'
90
90
  opts[:search_index] = opts[:search_index] || 'Books'
91
-
91
+
92
92
  type = opts.delete(:type)
93
- if type
93
+ if type
94
94
  opts[type.to_sym] = terms
95
- else
95
+ else
96
96
  opts[:keywords] = terms
97
97
  end
98
-
98
+
99
99
  self.send_request(opts)
100
100
  end
101
101
 
@@ -103,42 +103,42 @@ module Amazon
103
103
  def self.item_lookup(item_id, opts = {})
104
104
  opts[:operation] = 'ItemLookup'
105
105
  opts[:item_id] = item_id
106
-
106
+
107
107
  self.send_request(opts)
108
- end
108
+ end
109
109
 
110
110
  # Search a browse node by BrowseNodeId
111
111
  def self.browse_node_lookup(browse_node_id, opts = {})
112
112
  opts[:operation] = 'BrowseNodeLookup'
113
113
  opts[:browse_node_id] = browse_node_id
114
-
114
+
115
115
  self.send_request(opts)
116
- end
117
-
116
+ end
117
+
118
118
  # Generic send request to ECS REST service. You have to specify the :operation parameter.
119
119
  def self.send_request(opts)
120
120
  opts = self.options.merge(opts) if self.options
121
-
121
+
122
122
  # Include other required options
123
123
  opts[:timestamp] = Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
124
124
 
125
125
  request_url = prepare_url(opts)
126
126
  log "Request URL: #{request_url}"
127
-
127
+
128
128
  res = Net::HTTP.get_response(URI::parse(request_url))
129
129
  unless res.kind_of? Net::HTTPSuccess
130
130
  raise Amazon::RequestError, "HTTP Response: #{res.code} #{res.message}"
131
131
  end
132
132
  Response.new(res.body)
133
133
  end
134
-
135
- def self.validate_request(opts)
134
+
135
+ def self.validate_request(opts)
136
136
  raise Amazon::RequestError, "" if opts[:associate_tag]
137
137
  end
138
138
 
139
139
  # Response object returned after a REST call to Amazon service.
140
140
  class Response
141
-
141
+
142
142
  # XML input is in string format
143
143
  def initialize(xml)
144
144
  @doc = Nokogiri::XML(xml, nil, 'UTF-8')
@@ -166,22 +166,22 @@ module Amazon
166
166
  def error
167
167
  Element.get(@doc, "//Error/Message")
168
168
  end
169
-
169
+
170
170
  # Return error code
171
171
  def error_code
172
172
  Element.get(@doc, "//Error/Code")
173
173
  end
174
-
174
+
175
175
  # Return an array of Amazon::Element item objects.
176
176
  def items
177
177
  @items ||= (@doc/"Item").collect { |item| Element.new(item) }
178
178
  end
179
-
179
+
180
180
  # Return the first item (Amazon::Element)
181
181
  def first_item
182
182
  items.first
183
183
  end
184
-
184
+
185
185
  # Return current page no if :item_page option is when initiating the request.
186
186
  def item_page
187
187
  @item_page ||= Element.get(@doc, "//ItemPage").to_i
@@ -191,7 +191,7 @@ module Amazon
191
191
  def total_results
192
192
  @total_results ||= Element.get(@doc, "//TotalResults").to_i
193
193
  end
194
-
194
+
195
195
  # Return total pages.
196
196
  def total_pages
197
197
  @total_pages ||= Element.get(@doc, "//TotalPages").to_i
@@ -205,7 +205,7 @@ module Amazon
205
205
  initialize(xml)
206
206
  end
207
207
  end
208
-
208
+
209
209
  protected
210
210
  def self.log(s)
211
211
  return unless self.debug
@@ -217,8 +217,8 @@ module Amazon
217
217
  puts s
218
218
  end
219
219
  end
220
-
221
- private
220
+
221
+ private
222
222
  def self.prepare_url(opts)
223
223
  country = opts.delete(:country)
224
224
  country = (country.nil?) ? 'us' : country
@@ -227,18 +227,18 @@ module Amazon
227
227
 
228
228
  secret_key = opts.delete(:AWS_secret_key)
229
229
  request_host = URI.parse(request_url).host
230
-
230
+
231
231
  qs = ''
232
-
233
- opts = opts.collect do |a,b|
234
- [camelize(a.to_s), b.to_s]
232
+
233
+ opts = opts.collect do |a,b|
234
+ [camelize(a.to_s), b.to_s]
235
235
  end
236
-
237
- opts = opts.sort do |c,d|
236
+
237
+ opts = opts.sort do |c,d|
238
238
  c[0].to_s <=> d[0].to_s
239
239
  end
240
-
241
- opts.each do |e|
240
+
241
+ opts.each do |e|
242
242
  log "Adding #{e[0]}=#{e[1]}"
243
243
  next unless e[1]
244
244
  e[1] = e[1].join(',') if e[1].is_a? Array
@@ -247,7 +247,7 @@ module Amazon
247
247
  qs << "&" unless qs.length == 0
248
248
  qs << "#{e[0]}=#{v}"
249
249
  end
250
-
250
+
251
251
  signature = ''
252
252
  unless secret_key.nil?
253
253
  request_to_sign="GET\n#{request_host}\n/onca/xml\n#{qs}"
@@ -256,27 +256,27 @@ module Amazon
256
256
 
257
257
  "#{request_url}?#{qs}#{signature}"
258
258
  end
259
-
259
+
260
260
  def self.url_encode(string)
261
261
  string.gsub( /([^a-zA-Z0-9_.~-]+)/ ) do
262
262
  '%' + $1.unpack( 'H2' * $1.bytesize ).join( '%' ).upcase
263
263
  end
264
264
  end
265
-
265
+
266
266
  def self.camelize(s)
267
267
  s.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
268
268
  end
269
-
269
+
270
270
  def self.sign_request(url, key)
271
271
  return nil if key.nil?
272
-
273
- if (OPENSSL_DIGEST_SUPPORT)
272
+
273
+ if OPENSSL_DIGEST_SUPPORT
274
274
  signature = OpenSSL::HMAC.digest(OPENSSL_DIGEST, key, url)
275
275
  signature = [signature].pack('m').chomp
276
276
  else
277
- signature = Base64.encode64( HMAC::SHA256.digest(key, url) ).strip
277
+ signature = Base64.encode64(HMAC::SHA256.digest(key, url)).strip
278
278
  end
279
- signature = URI.escape(signature, Regexp.new("[+=]"))
279
+ signature = CGI.escape(signature)
280
280
  return signature
281
281
  end
282
282
  end
@@ -291,7 +291,7 @@ module Amazon
291
291
  result = result.inner_html if result
292
292
  result
293
293
  end
294
-
294
+
295
295
  # Return an unescaped text value of an element.
296
296
  def get_unescaped(element, path='.')
297
297
  result = self.get(element, path)
@@ -301,7 +301,7 @@ module Amazon
301
301
  # Return an array of values based on the given path.
302
302
  def get_array(element, path='.')
303
303
  return unless element
304
-
304
+
305
305
  result = element/path
306
306
  if (result.is_a? Nokogiri::XML::NodeSet) || (result.is_a? Array)
307
307
  result.collect { |item| self.get(item) }
@@ -313,29 +313,29 @@ module Amazon
313
313
  # Return child element text values of the given path.
314
314
  def get_hash(element, path='.')
315
315
  return unless element
316
-
316
+
317
317
  result = element.at_xpath(path)
318
318
  if result
319
319
  hash = {}
320
320
  result = result.children
321
321
  result.each do |item|
322
322
  hash[item.name] = item.inner_html
323
- end
323
+ end
324
324
  hash
325
325
  end
326
326
  end
327
327
  end
328
-
328
+
329
329
  # Pass Nokogiri::XML::Element object
330
330
  def initialize(element)
331
331
  @element = element
332
332
  end
333
333
 
334
- # Returns Nokogiri::XML::Element object
334
+ # Returns Nokogiri::XML::Element object
335
335
  def elem
336
336
  @element
337
337
  end
338
-
338
+
339
339
  # Returns a Nokogiri::XML::NodeSet of elements matching the given path. Example: element/"author".
340
340
  def /(path)
341
341
  elements = @element/path
@@ -349,7 +349,7 @@ module Amazon
349
349
  return unless elements
350
350
  elements = elements.map{|element| Element.new(element)}
351
351
  end
352
-
352
+
353
353
  # Similar with search_and_convert but always return first element if more than one elements found
354
354
  def get_element(path)
355
355
  elements = get_elements(path)
@@ -360,12 +360,12 @@ module Amazon
360
360
  def get(path='.')
361
361
  Element.get(@element, path)
362
362
  end
363
-
363
+
364
364
  # Get the unescaped HTML text of the given path.
365
365
  def get_unescaped(path='.')
366
366
  Element.get_unescaped(@element, path)
367
367
  end
368
-
368
+
369
369
  # Get the array values of the given path.
370
370
  def get_array(path='.')
371
371
  Element.get_array(@element, path)
@@ -375,12 +375,12 @@ module Amazon
375
375
  def get_hash(path='.')
376
376
  Element.get_hash(@element, path)
377
377
  end
378
-
378
+
379
379
  def attributes
380
380
  return unless self.elem
381
381
  self.elem.attributes
382
382
  end
383
-
383
+
384
384
  def to_s
385
385
  elem.to_s if elem
386
386
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon-ecs
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.5
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Herryanto Siatono
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-08 00:00:00.000000000 Z
11
+ date: 2014-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  version: '0'
73
73
  requirements: []
74
74
  rubyforge_project:
75
- rubygems_version: 2.0.6
75
+ rubygems_version: 2.4.5
76
76
  signing_key:
77
77
  specification_version: 2
78
78
  summary: Generic Amazon Product Advertising Ruby API.