amazon-ecs 0.5.1 → 0.5.2

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.
Files changed (5) hide show
  1. data/CHANGELOG +5 -1
  2. data/README +13 -28
  3. data/lib/amazon/ecs.rb +301 -301
  4. data/test/amazon/ecs_test.rb +109 -109
  5. metadata +4 -3
data/CHANGELOG CHANGED
@@ -1,4 +1,8 @@
1
- 0.5.1 2006-02-08
1
+ 0.5.2 2007-09-08
2
+ ----------------
3
+ * Fixed Amazon::Element.get_unescaped error when result returned for given element path is nil
4
+
5
+ 0.5.1 2007-02-08
2
6
  ----------------
3
7
  * Fixed Amazon Japan and France URL error
4
8
  * Removed opts.delete(:search_index) from item_lookup, SearchIndex param is allowed
data/README CHANGED
@@ -14,10 +14,6 @@ instead you just need to change the element path.
14
14
 
15
15
  Version: 0.5.1
16
16
 
17
- Links:
18
- * http://amazon-ecs.rubyforge.org
19
- * http://www.pluitsolutions.com/amazon-ecs
20
-
21
17
  == INSTALLATION
22
18
 
23
19
  $ gem install amazon-ecs
@@ -26,7 +22,7 @@ Links:
26
22
 
27
23
  require 'amazon/ecs'
28
24
 
29
- # set the default options; options will be camelized and converted to REST request parameters.
25
+ # set the default options; options will be camelized and converted to REST request parameters.
30
26
  Amazon::Ecs.options = {:aWS_access_key_id => [your developer token]}
31
27
 
32
28
  # options provided on method call will merge with the default options
@@ -42,12 +38,12 @@ Links:
42
38
 
43
39
  # traverse through each item (Amazon::Element)
44
40
  res.items.each do |item|
45
- # retrieve element text value, following the XML output structure
41
+ # retrieve string value using XML path
46
42
  item.get('asin')
47
43
  item.get('itemattributes/title')
48
44
 
49
- # or you can also do it this way, to retrieve the title
50
- atts = item.get('itemattributes')
45
+ # or return Amazon::Element instance
46
+ atts = item.search_and_convert('itemattributes')
51
47
  atts.get('title')
52
48
 
53
49
  # return first author or a string array of authors
@@ -74,6 +70,9 @@ Links:
74
70
  el.get_unescaped('source')
75
71
  el.get_unescaped('content')
76
72
  end
73
+
74
+ # returns Amazon::Element instead of string
75
+ item.search_and_convert('itemattributes').
77
76
  end
78
77
 
79
78
  Refer to Amazon ECS documentation for more information on Amazon REST request parameters and XML output:
@@ -82,27 +81,13 @@ http://docs.amazonwebservices.com/AWSEcommerceService/2006-09-13/
82
81
  To get a sample of Amazon REST response XML output, use AWSZone.com scratch pad:
83
82
  http://www.awszone.com/scratchpads/aws/ecs.us/index.aws
84
83
 
84
+ == LINKS
85
+
86
+ * http://amazon-ecs.rubyforge.org
87
+ * http://www.pluitsolutions.com/amazon-ecs
88
+
85
89
  == LICENSE
86
90
 
87
91
  (The MIT License)
88
92
 
89
- Copyright (c) 2006 Herryanto Siatono, Pluit Solutions
90
-
91
- Permission is hereby granted, free of charge, to any person obtaining
92
- a copy of this software and associated documentation files (the
93
- "Software"), to deal in the Software without restriction, including
94
- without limitation the rights to use, copy, modify, merge, publish,
95
- distribute, sublicense, and/or sell copies of the Software, and to
96
- permit persons to whom the Software is furnished to do so, subject to
97
- the following conditions:
98
-
99
- The above copyright notice and this permission notice shall be
100
- included in all copies or substantial portions of the Software.
101
-
102
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
103
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
104
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
105
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
106
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
107
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
108
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
93
+ Copyright (c) 2006 Herryanto Siatono, Pluit Solutions
@@ -1,302 +1,302 @@
1
- #--
2
- # Copyright (c) 2006 Herryanto Siatono, Pluit Solutions
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
- # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
- # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
- # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
- # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- #++
23
-
24
- require 'net/http'
25
- require 'hpricot'
26
- require 'cgi'
27
-
28
- module Amazon
29
- class RequestError < StandardError; end
30
-
31
- # :include: README
32
- class Ecs
33
- SERVICE_URLS = {:us => 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService',
34
- :uk => 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
35
- :ca => 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService',
36
- :de => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
37
- :jp => 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService',
38
- :fr => 'http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService'
39
- }
40
-
41
- @@options = {}
42
- @@debug = false
43
-
44
- # Default search options
45
- def self.options
46
- @@options
47
- end
48
-
49
- # Set default search options
50
- def self.options=(opts)
51
- @@options = opts
52
- end
53
-
54
- # Get debug flag.
55
- def self.debug
56
- @@debug
57
- end
58
-
59
- # Set debug flag to true or false.
60
- def self.debug=(dbg)
61
- @@debug = dbg
62
- end
63
-
64
- def self.configure(&proc)
65
- raise ArgumentError, "Block is required." unless block_given?
66
- yield @@options
67
- end
68
-
69
- # Search amazon items with search terms. Default search index option is 'Books'.
70
- # For other search type other than keywords, please specify :type => [search type param name].
71
- def self.item_search(terms, opts = {})
72
- opts = self.options.merge(opts) if self.options
73
- opts[:operation] = 'ItemSearch'
74
- opts[:search_index] = opts[:search_index] || 'Books'
75
-
76
- type = opts.delete(:type)
77
- if type
78
- opts[type.to_sym] = terms
79
- else
80
- opts[:keywords] = terms
81
- end
82
-
83
- self.send_request(opts)
84
- end
85
-
86
- # Search an item by ASIN no.
87
- def self.item_lookup(item_id, opts = {})
88
- opts = self.options.merge(opts) if self.options
89
- opts[:operation] = 'ItemLookup'
90
- opts[:item_id] = item_id
91
-
92
- self.send_request(opts)
93
- end
94
-
95
- # Generic send request to ECS REST service. You have to specify the :operation parameter.
96
- def self.send_request(opts)
97
- request_url = prepare_url(opts)
98
- log "Request URL: #{request_url}"
99
-
100
- res = Net::HTTP.get_response(URI::parse(request_url))
101
- unless res.kind_of? Net::HTTPSuccess
102
- raise Amazon::RequestError, "HTTP Response: #{res.code} #{res.message}"
103
- end
104
- Response.new(res.body)
105
- end
106
-
107
- # Response object returned after a REST call to Amazon service.
108
- class Response
109
- # XML input is in string format
110
- def initialize(xml)
111
- @doc = Hpricot(xml)
112
- end
113
-
114
- # Return Hpricot object.
115
- def doc
116
- @doc
117
- end
118
-
119
- # Return true if request is valid.
120
- def is_valid_request?
121
- (@doc/"isvalid").inner_html == "True"
122
- end
123
-
124
- # Return true if response has an error.
125
- def has_error?
126
- !(error.nil? || error.empty?)
127
- end
128
-
129
- # Return error message.
130
- def error
131
- Element.get(@doc, "error/message")
132
- end
133
-
134
- # Return an array of Amazon::Element item objects.
135
- def items
136
- unless @items
137
- @items = (@doc/"item").collect {|item| Element.new(item)}
138
- end
139
- @items
140
- end
141
-
142
- # Return the first item (Amazon::Element)
143
- def first_item
144
- items.first
145
- end
146
-
147
- # Return current page no if :item_page option is when initiating the request.
148
- def item_page
149
- unless @item_page
150
- @item_page = (@doc/"itemsearchrequest/itempage").inner_html.to_i
151
- end
152
- @item_page
153
- end
154
-
155
- # Return total results.
156
- def total_results
157
- unless @total_results
158
- @total_results = (@doc/"totalresults").inner_html.to_i
159
- end
160
- @total_results
161
- end
162
-
163
- # Return total pages.
164
- def total_pages
165
- unless @total_pages
166
- @total_pages = (@doc/"totalpages").inner_html.to_i
167
- end
168
- @total_pages
169
- end
170
- end
171
-
172
- protected
173
- def self.log(s)
174
- return unless self.debug
175
- if defined? RAILS_DEFAULT_LOGGER
176
- RAILS_DEFAULT_LOGGER.error(s)
177
- elsif defined? LOGGER
178
- LOGGER.error(s)
179
- else
180
- puts s
181
- end
182
- end
183
-
184
- private
185
- def self.prepare_url(opts)
186
- country = opts.delete(:country)
187
- country = (country.nil?) ? 'us' : country
188
- request_url = SERVICE_URLS[country.to_sym]
189
- raise Amazon::RequestError, "Invalid country '#{country}'" unless request_url
190
-
191
- qs = ''
192
- opts.each {|k,v|
193
- next unless v
194
- v = v.join(',') if v.is_a? Array
195
- qs << "&#{camelize(k.to_s)}=#{URI.encode(v.to_s)}"
196
- }
197
- "#{request_url}#{qs}"
198
- end
199
-
200
- def self.camelize(s)
201
- s.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
202
- end
203
- end
204
-
205
- # Internal wrapper class to provide convenient method to access Hpricot element value.
206
- class Element
207
- # Pass Hpricot::Elements object
208
- def initialize(element)
209
- @element = element
210
- end
211
-
212
- # Returns Hpricot::Elments object
213
- def elem
214
- @element
215
- end
216
-
217
- # Find Hpricot::Elements matching the given path. Example: element/"author".
218
- def /(path)
219
- elements = @element/path
220
- return nil if elements.size == 0
221
- elements
222
- end
223
-
224
- # Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
225
- # Returns an array Amazon::Elements if more than Hpricot::Elements size is greater than 1.
226
- def search_and_convert(path)
227
- elements = self./(path)
228
- return unless elements
229
- elements = elements.map{|element| Element.new(element)}
230
- return elements.first if elements.size == 1
231
- elements
232
- end
233
-
234
- # Get the text value of the given path, leave empty to retrieve current element value.
235
- def get(path='')
236
- Element.get(@element, path)
237
- end
238
-
239
- # Get the unescaped HTML text of the given path.
240
- def get_unescaped(path='')
241
- Element.get_unescaped(@element, path)
242
- end
243
-
244
- # Get the array values of the given path.
245
- def get_array(path='')
246
- Element.get_array(@element, path)
247
- end
248
-
249
- # Get the children element text values in hash format with the element names as the hash keys.
250
- def get_hash(path='')
251
- Element.get_hash(@element, path)
252
- end
253
-
254
- # Similar to #get, except an element object must be passed-in.
255
- def self.get(element, path='')
256
- return unless element
257
- result = element.at(path)
258
- result = result.inner_html if result
259
- result
260
- end
261
-
262
- # Similar to #get_unescaped, except an element object must be passed-in.
263
- def self.get_unescaped(element, path='')
264
- CGI::unescapeHTML(get(element, path))
265
- end
266
-
267
- # Similar to #get_array, except an element object must be passed-in.
268
- def self.get_array(element, path='')
269
- return unless element
270
-
271
- result = element/path
272
- if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
273
- parsed_result = []
274
- result.each {|item|
275
- parsed_result << Element.get(item)
276
- }
277
- parsed_result
278
- else
279
- [Element.get(result)]
280
- end
281
- end
282
-
283
- # Similar to #get_hash, except an element object must be passed-in.
284
- def self.get_hash(element, path='')
285
- return unless element
286
-
287
- result = element.at(path)
288
- if result
289
- hash = {}
290
- result = result.children
291
- result.each do |item|
292
- hash[item.name.to_sym] = item.inner_html
293
- end
294
- hash
295
- end
296
- end
297
-
298
- def to_s
299
- elem.to_s if elem
300
- end
301
- end
1
+ #--
2
+ # Copyright (c) 2006 Herryanto Siatono, Pluit Solutions
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require 'net/http'
25
+ require 'hpricot'
26
+ require 'cgi'
27
+
28
+ module Amazon
29
+ class RequestError < StandardError; end
30
+
31
+ class Ecs
32
+ SERVICE_URLS = {:us => 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService',
33
+ :uk => 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
34
+ :ca => 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService',
35
+ :de => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
36
+ :jp => 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService',
37
+ :fr => 'http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService'
38
+ }
39
+
40
+ @@options = {}
41
+ @@debug = false
42
+
43
+ # Default search options
44
+ def self.options
45
+ @@options
46
+ end
47
+
48
+ # Set default search options
49
+ def self.options=(opts)
50
+ @@options = opts
51
+ end
52
+
53
+ # Get debug flag.
54
+ def self.debug
55
+ @@debug
56
+ end
57
+
58
+ # Set debug flag to true or false.
59
+ def self.debug=(dbg)
60
+ @@debug = dbg
61
+ end
62
+
63
+ def self.configure(&proc)
64
+ raise ArgumentError, "Block is required." unless block_given?
65
+ yield @@options
66
+ end
67
+
68
+ # Search amazon items with search terms. Default search index option is 'Books'.
69
+ # For other search type other than keywords, please specify :type => [search type param name].
70
+ def self.item_search(terms, opts = {})
71
+ opts = self.options.merge(opts) if self.options
72
+ opts[:operation] = 'ItemSearch'
73
+ opts[:search_index] = opts[:search_index] || 'Books'
74
+
75
+ type = opts.delete(:type)
76
+ if type
77
+ opts[type.to_sym] = terms
78
+ else
79
+ opts[:keywords] = terms
80
+ end
81
+
82
+ self.send_request(opts)
83
+ end
84
+
85
+ # Search an item by ASIN no.
86
+ def self.item_lookup(item_id, opts = {})
87
+ opts = self.options.merge(opts) if self.options
88
+ opts[:operation] = 'ItemLookup'
89
+ opts[:item_id] = item_id
90
+
91
+ self.send_request(opts)
92
+ end
93
+
94
+ # Generic send request to ECS REST service. You have to specify the :operation parameter.
95
+ def self.send_request(opts)
96
+ request_url = prepare_url(opts)
97
+ log "Request URL: #{request_url}"
98
+
99
+ res = Net::HTTP.get_response(URI::parse(request_url))
100
+ unless res.kind_of? Net::HTTPSuccess
101
+ raise Amazon::RequestError, "HTTP Response: #{res.code} #{res.message}"
102
+ end
103
+ Response.new(res.body)
104
+ end
105
+
106
+ # Response object returned after a REST call to Amazon service.
107
+ class Response
108
+ # XML input is in string format
109
+ def initialize(xml)
110
+ @doc = Hpricot(xml)
111
+ end
112
+
113
+ # Return Hpricot object.
114
+ def doc
115
+ @doc
116
+ end
117
+
118
+ # Return true if request is valid.
119
+ def is_valid_request?
120
+ (@doc/"isvalid").inner_html == "True"
121
+ end
122
+
123
+ # Return true if response has an error.
124
+ def has_error?
125
+ !(error.nil? || error.empty?)
126
+ end
127
+
128
+ # Return error message.
129
+ def error
130
+ Element.get(@doc, "error/message")
131
+ end
132
+
133
+ # Return an array of Amazon::Element item objects.
134
+ def items
135
+ unless @items
136
+ @items = (@doc/"item").collect {|item| Element.new(item)}
137
+ end
138
+ @items
139
+ end
140
+
141
+ # Return the first item (Amazon::Element)
142
+ def first_item
143
+ items.first
144
+ end
145
+
146
+ # Return current page no if :item_page option is when initiating the request.
147
+ def item_page
148
+ unless @item_page
149
+ @item_page = (@doc/"itemsearchrequest/itempage").inner_html.to_i
150
+ end
151
+ @item_page
152
+ end
153
+
154
+ # Return total results.
155
+ def total_results
156
+ unless @total_results
157
+ @total_results = (@doc/"totalresults").inner_html.to_i
158
+ end
159
+ @total_results
160
+ end
161
+
162
+ # Return total pages.
163
+ def total_pages
164
+ unless @total_pages
165
+ @total_pages = (@doc/"totalpages").inner_html.to_i
166
+ end
167
+ @total_pages
168
+ end
169
+ end
170
+
171
+ protected
172
+ def self.log(s)
173
+ return unless self.debug
174
+ if defined? RAILS_DEFAULT_LOGGER
175
+ RAILS_DEFAULT_LOGGER.error(s)
176
+ elsif defined? LOGGER
177
+ LOGGER.error(s)
178
+ else
179
+ puts s
180
+ end
181
+ end
182
+
183
+ private
184
+ def self.prepare_url(opts)
185
+ country = opts.delete(:country)
186
+ country = (country.nil?) ? 'us' : country
187
+ request_url = SERVICE_URLS[country.to_sym]
188
+ raise Amazon::RequestError, "Invalid country '#{country}'" unless request_url
189
+
190
+ qs = ''
191
+ opts.each {|k,v|
192
+ next unless v
193
+ v = v.join(',') if v.is_a? Array
194
+ qs << "&#{camelize(k.to_s)}=#{URI.encode(v.to_s)}"
195
+ }
196
+ "#{request_url}#{qs}"
197
+ end
198
+
199
+ def self.camelize(s)
200
+ s.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
201
+ end
202
+ end
203
+
204
+ # Internal wrapper class to provide convenient method to access Hpricot element value.
205
+ class Element
206
+ # Pass Hpricot::Elements object
207
+ def initialize(element)
208
+ @element = element
209
+ end
210
+
211
+ # Returns Hpricot::Elments object
212
+ def elem
213
+ @element
214
+ end
215
+
216
+ # Find Hpricot::Elements matching the given path. Example: element/"author".
217
+ def /(path)
218
+ elements = @element/path
219
+ return nil if elements.size == 0
220
+ elements
221
+ end
222
+
223
+ # Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
224
+ # Returns an array Amazon::Elements if more than Hpricot::Elements size is greater than 1.
225
+ def search_and_convert(path)
226
+ elements = self./(path)
227
+ return unless elements
228
+ elements = elements.map{|element| Element.new(element)}
229
+ return elements.first if elements.size == 1
230
+ elements
231
+ end
232
+
233
+ # Get the text value of the given path, leave empty to retrieve current element value.
234
+ def get(path='')
235
+ Element.get(@element, path)
236
+ end
237
+
238
+ # Get the unescaped HTML text of the given path.
239
+ def get_unescaped(path='')
240
+ Element.get_unescaped(@element, path)
241
+ end
242
+
243
+ # Get the array values of the given path.
244
+ def get_array(path='')
245
+ Element.get_array(@element, path)
246
+ end
247
+
248
+ # Get the children element text values in hash format with the element names as the hash keys.
249
+ def get_hash(path='')
250
+ Element.get_hash(@element, path)
251
+ end
252
+
253
+ # Similar to #get, except an element object must be passed-in.
254
+ def self.get(element, path='')
255
+ return unless element
256
+ result = element.at(path)
257
+ result = result.inner_html if result
258
+ result
259
+ end
260
+
261
+ # Similar to #get_unescaped, except an element object must be passed-in.
262
+ def self.get_unescaped(element, path='')
263
+ result = get(element, path)
264
+ CGI::unescapeHTML(result) if result
265
+ end
266
+
267
+ # Similar to #get_array, except an element object must be passed-in.
268
+ def self.get_array(element, path='')
269
+ return unless element
270
+
271
+ result = element/path
272
+ if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
273
+ parsed_result = []
274
+ result.each {|item|
275
+ parsed_result << Element.get(item)
276
+ }
277
+ parsed_result
278
+ else
279
+ [Element.get(result)]
280
+ end
281
+ end
282
+
283
+ # Similar to #get_hash, except an element object must be passed-in.
284
+ def self.get_hash(element, path='')
285
+ return unless element
286
+
287
+ result = element.at(path)
288
+ if result
289
+ hash = {}
290
+ result = result.children
291
+ result.each do |item|
292
+ hash[item.name.to_sym] = item.inner_html
293
+ end
294
+ hash
295
+ end
296
+ end
297
+
298
+ def to_s
299
+ elem.to_s if elem
300
+ end
301
+ end
302
302
  end
@@ -1,110 +1,110 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
2
-
3
- class Amazon::EcsTest < Test::Unit::TestCase
4
-
5
- AWS_ACCESS_KEY_ID = ''
6
- raise "Please specify set your AWS_ACCESS_KEY_ID" if AWS_ACCESS_KEY_ID.empty?
7
-
8
- Amazon::Ecs.configure do |options|
9
- options[:response_group] = 'Large'
10
- options[:aWS_access_key_id] = AWS_ACCESS_KEY_ID
11
- end
12
-
13
- ## Test item_search
14
-
15
- def test_item_search
16
- resp = Amazon::Ecs.item_search('ruby')
17
- assert(resp.is_valid_request?)
18
- assert(resp.total_results >= 3600)
19
- assert(resp.total_pages >= 360)
20
- end
21
-
22
- def test_item_search_with_paging
23
- resp = Amazon::Ecs.item_search('ruby', :item_page => 2)
24
- assert resp.is_valid_request?
25
- assert 2, resp.item_page
26
- end
27
-
28
- def test_item_search_with_invalid_request
29
- resp = Amazon::Ecs.item_search(nil)
30
- assert !resp.is_valid_request?
31
- end
32
-
33
- def test_item_search_with_no_result
34
- resp = Amazon::Ecs.item_search('afdsafds')
35
-
36
- assert resp.is_valid_request?
37
- assert_equal "We did not find any matches for your request.",
38
- resp.error
39
- end
40
-
41
- def test_item_search_uk
42
- resp = Amazon::Ecs.item_search('ruby', :country => :uk)
43
- assert resp.is_valid_request?
44
- end
45
-
46
- def test_item_search_by_author
47
- resp = Amazon::Ecs.item_search('dave', :type => :author)
48
- assert resp.is_valid_request?
49
- end
50
-
51
- def test_item_get
52
- resp = Amazon::Ecs.item_search("0974514055")
53
- item = resp.first_item
54
-
55
- # test get
56
- assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition",
57
- item.get("itemattributes/title")
58
-
59
- # test get_array
60
- assert_equal ['Dave Thomas', 'Chad Fowler', 'Andy Hunt'],
61
- item.get_array("author")
62
-
63
- # test get_hash
64
- small_image = item.get_hash("smallimage")
65
-
66
- assert_equal 3, small_image.keys.size
67
- assert_match "0974514055.01", small_image[:url]
68
- assert_equal "75", small_image[:height]
69
- assert_equal "59", small_image[:width]
70
-
71
- # test /
72
- reviews = item/"editorialreview"
73
- reviews.each do |review|
74
- # returns unescaped HTML content, Hpricot escapes all text values
75
- assert Amazon::Element.get_unescaped(review, 'source')
76
- assert Amazon::Element.get_unescaped(review, 'content')
77
- end
78
- end
79
-
80
- ## Test item_lookup
81
- def test_item_lookup
82
- resp = Amazon::Ecs.item_lookup('0974514055')
83
- assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition",
84
- resp.first_item.get("itemattributes/title")
85
- end
86
-
87
- def test_item_lookup_with_invalid_request
88
- resp = Amazon::Ecs.item_lookup(nil)
89
- assert resp.has_error?
90
- assert resp.error
91
- end
92
-
93
- def test_item_lookup_with_no_result
94
- resp = Amazon::Ecs.item_lookup('abc')
95
-
96
- assert resp.is_valid_request?
97
- assert_match(/ABC is not a valid value for ItemId/, resp.error)
98
- end
99
-
100
- def test_search_and_convert
101
- resp = Amazon::Ecs.item_lookup('0974514055')
102
- title = resp.first_item.get("itemattributes/title")
103
- authors = resp.first_item.search_and_convert("author")
104
-
105
- assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition", title
106
- assert authors.is_a?(Array)
107
- assert 3, authors.size
108
- assert_equal "Dave Thomas", authors.first.get
109
- end
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class Amazon::EcsTest < Test::Unit::TestCase
4
+
5
+ AWS_ACCESS_KEY_ID = ''
6
+ raise "Please specify set your AWS_ACCESS_KEY_ID" if AWS_ACCESS_KEY_ID.empty?
7
+
8
+ Amazon::Ecs.configure do |options|
9
+ options[:response_group] = 'Large'
10
+ options[:aWS_access_key_id] = AWS_ACCESS_KEY_ID
11
+ end
12
+
13
+ ## Test item_search
14
+
15
+ def test_item_search
16
+ resp = Amazon::Ecs.item_search('ruby')
17
+ assert(resp.is_valid_request?)
18
+ assert(resp.total_results >= 3600)
19
+ assert(resp.total_pages >= 360)
20
+ end
21
+
22
+ def test_item_search_with_paging
23
+ resp = Amazon::Ecs.item_search('ruby', :item_page => 2)
24
+ assert resp.is_valid_request?
25
+ assert 2, resp.item_page
26
+ end
27
+
28
+ def test_item_search_with_invalid_request
29
+ resp = Amazon::Ecs.item_search(nil)
30
+ assert !resp.is_valid_request?
31
+ end
32
+
33
+ def test_item_search_with_no_result
34
+ resp = Amazon::Ecs.item_search('afdsafds')
35
+
36
+ assert resp.is_valid_request?
37
+ assert_equal "We did not find any matches for your request.",
38
+ resp.error
39
+ end
40
+
41
+ def test_item_search_uk
42
+ resp = Amazon::Ecs.item_search('ruby', :country => :uk)
43
+ assert resp.is_valid_request?
44
+ end
45
+
46
+ def test_item_search_by_author
47
+ resp = Amazon::Ecs.item_search('dave', :type => :author)
48
+ assert resp.is_valid_request?
49
+ end
50
+
51
+ def test_item_get
52
+ resp = Amazon::Ecs.item_search("0974514055")
53
+ item = resp.first_item
54
+
55
+ # test get
56
+ assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition",
57
+ item.get("itemattributes/title")
58
+
59
+ # test get_array
60
+ assert_equal ['Dave Thomas', 'Chad Fowler', 'Andy Hunt'],
61
+ item.get_array("author")
62
+
63
+ # test get_hash
64
+ small_image = item.get_hash("smallimage")
65
+
66
+ assert_equal 3, small_image.keys.size
67
+ assert_match "0974514055.01", small_image[:url]
68
+ assert_equal "75", small_image[:height]
69
+ assert_equal "59", small_image[:width]
70
+
71
+ # test /
72
+ reviews = item/"editorialreview"
73
+ reviews.each do |review|
74
+ # returns unescaped HTML content, Hpricot escapes all text values
75
+ assert Amazon::Element.get_unescaped(review, 'source')
76
+ assert Amazon::Element.get_unescaped(review, 'content')
77
+ end
78
+ end
79
+
80
+ ## Test item_lookup
81
+ def test_item_lookup
82
+ resp = Amazon::Ecs.item_lookup('0974514055')
83
+ assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition",
84
+ resp.first_item.get("itemattributes/title")
85
+ end
86
+
87
+ def test_item_lookup_with_invalid_request
88
+ resp = Amazon::Ecs.item_lookup(nil)
89
+ assert resp.has_error?
90
+ assert resp.error
91
+ end
92
+
93
+ def test_item_lookup_with_no_result
94
+ resp = Amazon::Ecs.item_lookup('abc')
95
+
96
+ assert resp.is_valid_request?
97
+ assert_match(/ABC is not a valid value for ItemId/, resp.error)
98
+ end
99
+
100
+ def test_search_and_convert
101
+ resp = Amazon::Ecs.item_lookup('0974514055')
102
+ title = resp.first_item.get("itemattributes/title")
103
+ authors = resp.first_item.search_and_convert("author")
104
+
105
+ assert_equal "Programming Ruby: The Pragmatic Programmers' Guide, Second Edition", title
106
+ assert authors.is_a?(Array)
107
+ assert 3, authors.size
108
+ assert_equal "Dave Thomas", authors.first.get
109
+ end
110
110
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: amazon-ecs
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.1
7
- date: 2007-02-09 00:00:00 +08:00
6
+ version: 0.5.2
7
+ date: 2007-09-08 00:00:00 +08:00
8
8
  summary: Generic Amazon E-commerce Service (ECS) REST API. Supports ECS 4.0.
9
9
  require_paths:
10
10
  - lib
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
+ post_install_message:
28
29
  authors:
29
30
  - Herryanto Siatono
30
31
  files: