amazon-ecs 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -1
- data/README +13 -28
- data/lib/amazon/ecs.rb +301 -301
- data/test/amazon/ecs_test.rb +109 -109
- metadata +4 -3
data/CHANGELOG
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
0.5.
|
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
|
-
|
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
|
41
|
+
# retrieve string value using XML path
|
46
42
|
item.get('asin')
|
47
43
|
item.get('itemattributes/title')
|
48
44
|
|
49
|
-
# or
|
50
|
-
atts = item.
|
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
|
data/lib/amazon/ecs.rb
CHANGED
@@ -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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
:
|
35
|
-
:
|
36
|
-
:
|
37
|
-
:
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
@@
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
#
|
70
|
-
|
71
|
-
|
72
|
-
opts =
|
73
|
-
opts[:
|
74
|
-
|
75
|
-
|
76
|
-
type
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
opts =
|
89
|
-
opts[:
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
res
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
country =
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
}
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
elements
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
#
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
elements
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
result =
|
258
|
-
result
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
CGI::unescapeHTML(
|
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
|
data/test/amazon/ecs_test.rb
CHANGED
@@ -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.
|
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.
|
7
|
-
date: 2007-
|
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:
|