hollownest-ruby-aws 0.0.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.
@@ -0,0 +1,141 @@
1
+ # $Id: cache.rb,v 1.8 2008/06/10 06:33:46 ianmacd Exp $
2
+ #
3
+
4
+ module Amazon
5
+
6
+ module AWS
7
+
8
+ # This class provides a simple results caching system for operations
9
+ # performed by AWS.
10
+ #
11
+ # To use it, set _cache_ to *true* in either <tt>/etc/amazonrc</tt> or
12
+ # <tt>~/.amazonrc</tt>.
13
+ #
14
+ # By default, the cache directory used is <tt>/tmp/amazon</tt>, but this
15
+ # can be changed by defining _cache_dir_ in either <tt>/etc/amazonrc</tt>
16
+ # or <tt>~/.amazonrc</tt>.
17
+ #
18
+ # When a cache is used, Ruby/AWS will check the cache directory for a
19
+ # recent copy of a response to the exact operation that you are
20
+ # performing. If found, the cached response will be returned instead of
21
+ # the request being forwarded to the AWS servers for processing. If no
22
+ # (recent) copy is found, the request will be forwarded to the AWS servers
23
+ # as usual. Recency is defined here as less than 24 hours old.
24
+ #
25
+ class Cache
26
+
27
+ require 'fileutils'
28
+
29
+ begin
30
+ require 'md5'
31
+ rescue LoadError
32
+ # Ruby 1.9 has moved MD5.
33
+ #
34
+ require 'digest/md5'
35
+ end
36
+
37
+ # Exception class for bad cache paths.
38
+ #
39
+ class PathError < StandardError; end
40
+
41
+ # Length of one day in seconds
42
+ #
43
+ ONE_DAY = 86400 # :nodoc:
44
+
45
+ # Age in days below which to consider cache files valid.
46
+ #
47
+ MAX_AGE = 1.0
48
+
49
+ # Default cache location.
50
+ #
51
+ DEFAULT_CACHE_DIR = '/tmp/amazon'
52
+
53
+ attr_reader :path
54
+
55
+ def initialize(path=DEFAULT_CACHE_DIR)
56
+ path ||= DEFAULT_CACHE_DIR
57
+
58
+ ::FileUtils::mkdir_p( path ) unless File.exists? path
59
+
60
+ unless File.directory? path
61
+ raise PathError, "cache path #{path} is not a directory"
62
+ end
63
+
64
+ unless File.readable? path
65
+ raise PathError, "cache path #{path} is not readable"
66
+ end
67
+
68
+ unless File.writable? path
69
+ raise PathError, "cache path #{path} is not writable"
70
+ end
71
+
72
+ @path = path
73
+ end
74
+
75
+
76
+ # Determine whether or not the the response to a given URL is cached.
77
+ # Returns *true* or *false*.
78
+ #
79
+ def cached?(url)
80
+ digest = Digest::MD5.hexdigest( url )
81
+
82
+ cache_files = Dir.glob( File.join( @path, '*' ) ).map do |d|
83
+ File.basename( d )
84
+ end
85
+
86
+ return cache_files.include?( digest ) &&
87
+ ( Time.now - File.mtime( File.join( @path, digest ) ) ) /
88
+ ONE_DAY <= MAX_AGE
89
+ end
90
+
91
+
92
+ # Retrieve the cached response associated with _url_.
93
+ #
94
+ def fetch(url)
95
+ digest = Digest::MD5.hexdigest( url )
96
+ cache_file = File.join( @path, digest )
97
+
98
+ return nil unless File.exist? cache_file
99
+
100
+ Amazon.dprintf( 'Fetching %s from cache...', digest )
101
+ File.open( File.join( cache_file ) ).readlines.to_s
102
+ end
103
+
104
+
105
+ # Cache the data from _contents_ and associate it with _url_.
106
+ #
107
+ def store(url, contents)
108
+ digest = Digest::MD5.hexdigest( url )
109
+ cache_file = File.join( @path, digest )
110
+
111
+ Amazon.dprintf( 'Caching %s...', digest )
112
+ File.open( cache_file, 'w' ) { |f| f.puts contents }
113
+ end
114
+
115
+
116
+ # This method flushes all files from the cache directory specified
117
+ # in the object's <i>@path</i> variable.
118
+ #
119
+ def flush_all
120
+ FileUtils.rm Dir.glob( File.join( @path, '*' ) )
121
+ end
122
+
123
+
124
+ # This method flushes expired files from the cache directory specified
125
+ # in the object's <i>@path</i> variable.
126
+ #
127
+ def flush_expired
128
+ now = Time.now
129
+
130
+ expired_files = Dir.glob( File.join( @path, '*' ) ).find_all do |f|
131
+ ( now - File.mtime( f ) ) / ONE_DAY > MAX_AGE
132
+ end
133
+
134
+ FileUtils.rm expired_files
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+
141
+ end
@@ -0,0 +1,356 @@
1
+ # $Id: search.rb,v 1.30 2009/02/19 16:19:47 ianmacd Exp $
2
+ #
3
+
4
+ module Amazon
5
+
6
+ module AWS
7
+
8
+ require 'amazon/aws'
9
+ require 'net/http'
10
+ require 'rexml/document'
11
+
12
+ # Load this library with:
13
+ #
14
+ # require 'amazon/aws/search'
15
+ #
16
+ module Search
17
+
18
+ class Request
19
+
20
+ include REXML
21
+
22
+ # Exception class for bad access key ID.
23
+ #
24
+ class AccessKeyIdError < Amazon::AWS::Error::AWSError; end
25
+
26
+ # Exception class for bad locales.
27
+ #
28
+ class LocaleError < Amazon::AWS::Error::AWSError; end
29
+
30
+ attr_reader :conn, :locale, :user_agent
31
+ attr_writer :cache
32
+
33
+ # This method is used to generate an AWS search request object.
34
+ #
35
+ # _key_id_ is your AWS {access key
36
+ # ID}[https://aws-portal.amazon.com/gp/aws/developer/registration/index.html],
37
+ # _associate_ is your
38
+ # Associates[http://docs.amazonwebservices.com/AWSECommerceService/2009-01-06/GSG/BecominganAssociate.html]
39
+ # tag (if any), _locale_ is the locale in which you which to work
40
+ # (*us* for amazon.com[http://www.amazon.com/], *uk* for
41
+ # amazon.co.uk[http://www.amazon.co.uk], etc.), _cache_ is whether or
42
+ # not you wish to utilise a response cache, and _user_agent_ is the
43
+ # client name to pass when performing calls to AWS. By default,
44
+ # _user_agent_ will be set to a string identifying the Ruby/AWS
45
+ # library and its version number.
46
+ #
47
+ # _locale_ and _cache_ can also be set later, if you wish to change
48
+ # the current behaviour.
49
+ #
50
+ # Example:
51
+ #
52
+ # req = Request.new( '0Y44V8FAFNM119CX4TR2', 'calibanorg-20' )
53
+ #
54
+ def initialize(key_id=nil, associate=nil, locale=nil, cache=nil,
55
+ user_agent=USER_AGENT)
56
+
57
+ @config ||= Amazon::Config.new
58
+
59
+ def_locale = locale
60
+ locale = 'us' unless locale
61
+ locale.downcase!
62
+
63
+ key_id ||= @config['key_id']
64
+ cache = @config['cache'] if cache.nil?
65
+
66
+ # Take locale from config file if no locale was passed to method.
67
+ #
68
+ if @config.key?( 'locale' ) && ! def_locale
69
+ locale = @config['locale']
70
+ end
71
+ validate_locale( locale )
72
+
73
+ if key_id.nil?
74
+ raise AccessKeyIdError, 'key_id may not be nil'
75
+ end
76
+
77
+ @key_id = key_id
78
+ @tag = associate || @config['associate'] || DEF_ASSOC[locale]
79
+ @user_agent = user_agent
80
+ @cache = unless cache == 'false' || cache == false
81
+ Amazon::AWS::Cache.new( @config['cache_dir'] )
82
+ else
83
+ nil
84
+ end
85
+ @api = @config['api'] || nil
86
+ self.locale = locale
87
+ end
88
+
89
+
90
+ # Assign a new locale. If the locale we're coming from is using the
91
+ # default Associate ID for that locale, then we use the new locale's
92
+ # default ID, too.
93
+ #
94
+ def locale=(l) # :nodoc:
95
+ old_locale = @locale ||= nil
96
+ @locale = validate_locale( l )
97
+
98
+ # Use the new locale's default ID if the ID currently in use is the
99
+ # current locale's default ID.
100
+ #
101
+ if @tag == Amazon::AWS::DEF_ASSOC[old_locale]
102
+ @tag = Amazon::AWS::DEF_ASSOC[@locale]
103
+ end
104
+
105
+ if @config.key?( @locale ) && @config[@locale].key?( 'associate' )
106
+ @tag = @config[@locale]['associate']
107
+ end
108
+
109
+ # We must now set up a new HTTP connection to the correct server for
110
+ # this locale, unless the same server is used for both.
111
+ #
112
+ unless Amazon::AWS::ENDPOINT[@locale] ==
113
+ Amazon::AWS::ENDPOINT[old_locale]
114
+ #connect( @locale )
115
+ @conn = nil
116
+ end
117
+ end
118
+
119
+
120
+ # If @cache has simply been assigned *true* at some point in time,
121
+ # assign a proper cache object to it when it is referenced. Otherwise,
122
+ # just return its value.
123
+ #
124
+ def cache # :nodoc:
125
+ if @cache == true
126
+ @cache = Amazon::AWS::Cache.new( @config['cache_dir'] )
127
+ else
128
+ @cache
129
+ end
130
+ end
131
+
132
+
133
+ # Verify the validity of a locale string. _l_ is the locale string.
134
+ #
135
+ def validate_locale(l)
136
+ unless Amazon::AWS::ENDPOINT.has_key? l
137
+ raise LocaleError, "invalid locale: #{l}"
138
+ end
139
+ l
140
+ end
141
+ private :validate_locale
142
+
143
+
144
+ # Return an HTTP connection for the current _locale_.
145
+ #
146
+ def connect(locale)
147
+ if ENV.key? 'http_proxy'
148
+ uri = URI.parse( ENV['http_proxy'] )
149
+ proxy_user = proxy_pass = nil
150
+ proxy_user, proxy_pass = uri.userinfo.split( /:/ ) if uri.userinfo
151
+ @conn = Net::HTTP::Proxy( uri.host, uri.port, proxy_user,
152
+ proxy_pass ).start(
153
+ Amazon::AWS::ENDPOINT[locale].host )
154
+ else
155
+ @conn = Net::HTTP::start( Amazon::AWS::ENDPOINT[locale].host )
156
+ end
157
+ end
158
+ private :connect
159
+
160
+
161
+ # Reconnect to the server if our connection has been lost (due to a
162
+ # time-out, etc.).
163
+ #
164
+ def reconnect # :nodoc:
165
+ connect( self.locale )
166
+ self
167
+ end
168
+
169
+
170
+ # This method checks for errors in an XML response returned by AWS.
171
+ # _xml_ is the XML node below which to search.
172
+ #
173
+ def error_check(xml)
174
+ if xml = xml.elements['Errors/Error']
175
+ raise Amazon::AWS::Error.exception( xml )
176
+ end
177
+ end
178
+ private :error_check
179
+
180
+
181
+ # Perform a search of the AWS database. _operation_ is one of the
182
+ # objects subclassed from _Operation_, such as _ItemSearch_,
183
+ # _ItemLookup_, etc. It may also be a _MultipleOperation_ object.
184
+ #
185
+ # _response_group_ will apply to all both operations contained in
186
+ # _operation_, if _operation_ is a _MultipleOperation_ object.
187
+ #
188
+ # _nr_pages_ is the number of results pages to return. It defaults to
189
+ # <b>1</b>. If a higher number is given, pages 1 to _nr_pages_ will be
190
+ # returned. If the special value <b>:ALL_PAGES</b> is given, all
191
+ # results pages will be returned.
192
+ #
193
+ # The maximum page number that can be returned for each type of
194
+ # operation is documented in the AWS Developer's Guide:
195
+ #
196
+ # http://docs.amazonwebservices.com/AWSECommerceService/2009-01-06/DG/index.html?MaximumNumberofPages.html
197
+ #
198
+ # Note that _ItemLookup_ operations can use three separate pagination
199
+ # parameters. Ruby/AWS, however, uses _OfferPage_ for the purposes of
200
+ # returning multiple pages.
201
+ #
202
+ # If operation is of class _MultipleOperation_, the operations
203
+ # combined within will return only the first page, regardless of
204
+ # whether a higher number of pages is requested.
205
+ #
206
+ def search(operation, response_group, nr_pages=1)
207
+ q_params = Amazon::AWS::SERVICE.
208
+ merge( { 'AWSAccessKeyId' => @key_id,
209
+ 'AssociateTag' => @tag } ).
210
+ merge( operation.params ).
211
+ merge( response_group.params )
212
+
213
+ # Check to see whether a particular version of the API has been
214
+ # requested. If so, overwrite Version with the new value.
215
+ #
216
+ q_params.merge!( { 'Version' => @api } ) if @api
217
+
218
+ query = Amazon::AWS.assemble_query( q_params )
219
+ page = Amazon::AWS.get_page( self, query )
220
+ doc = Document.new( page )
221
+
222
+ # Some errors occur at the very top level of the XML. For example,
223
+ # when no Operation parameter is given. This should not be possible
224
+ # with user code, but occurred during debugging of this library.
225
+ #
226
+ error_check( doc )
227
+
228
+ # Another possible error results in a document containing nothing
229
+ # but <Result>Internal Error</Result>. This occurs when a specific
230
+ # version of the AWS API is requested, in combination with an
231
+ # operation that did not yet exist in that version of the API.
232
+ #
233
+ # For example:
234
+ #
235
+ # http://ecs.amazonaws.com/onca/xml?AWSAccessKeyId=foo&Operation=VehicleSearch&Year=2008&ResponseGroup=VehicleMakes&Service=AWSECommerceService&Version=2008-03-03
236
+ #
237
+ if xml = doc.elements['Result']
238
+ raise Amazon::AWS::Error::AWSError, xml.text
239
+ end
240
+
241
+ # Fundamental errors happen at the OperationRequest level. For
242
+ # example, if an invalid AWSAccessKeyId is used.
243
+ #
244
+ error_check( doc.elements['*/OperationRequest'] )
245
+
246
+ # Check for parameter and value errors deeper down, inside Request.
247
+ #
248
+ if operation.kind == 'MultipleOperation'
249
+
250
+ # Everything is a level deeper, because of the
251
+ # <MultiOperationResponse> container.
252
+ #
253
+ # Check for errors in the first operation.
254
+ #
255
+ error_check( doc.elements['*/*/*/Request'] )
256
+
257
+ # Check for errors in the second operation.
258
+ #
259
+ error_check( doc.elements['*/*[3]/*/Request'] )
260
+
261
+ # If second operation is batched, check for errors in its 2nd set
262
+ # of results.
263
+ #
264
+ if batched = doc.elements['*/*[3]/*[2]/Request']
265
+ error_check( batched )
266
+ end
267
+ else
268
+ error_check( doc.elements['*/*/Request'] )
269
+
270
+ # If operation is batched, check for errors in its 2nd set of
271
+ # results.
272
+ #
273
+ if batched = doc.elements['*/*[3]/Request']
274
+ error_check( batched )
275
+ end
276
+ end
277
+
278
+ # FIXME: This doesn't work if a MultipleOperation was used, because
279
+ # <TotalPages> will be nested one level deeper. It's therefore
280
+ # currently only possible to return the first page of results
281
+ # for operations combined in a MultipleOperation.
282
+ #
283
+ if doc.elements['*/*[2]/TotalPages']
284
+ total_pages = doc.elements['*/*[2]/TotalPages'].text.to_i
285
+ else
286
+ total_pages = 1
287
+ end
288
+
289
+ # Create a root AWS object and walk the XML response tree.
290
+ #
291
+ aws = AWS::AWSObject.new( operation )
292
+ aws.walk( doc )
293
+ result = aws
294
+
295
+ # If only one page has been requested or only one page is available,
296
+ # we can stop here. First yield to the block, if given.
297
+ #
298
+ if nr_pages == 1 || ( tp = total_pages ) == 1
299
+ yield result if block_given?
300
+ return result
301
+ end
302
+
303
+ # Limit the number of pages to the maximum number available.
304
+ #
305
+ nr_pages = tp.to_i if nr_pages == :ALL_PAGES || nr_pages > tp.to_i
306
+
307
+ if PAGINATION.key? operation.kind
308
+ page_parameter = PAGINATION[operation.kind]['parameter']
309
+ max_pages = PAGINATION[operation.kind]['max_page']
310
+ else
311
+ page_parameter = 'ItemPage'
312
+ max_pages = 400
313
+ end
314
+
315
+ # Iterate over pages 2 and higher, but go no higher than MAX_PAGES.
316
+ #
317
+ 2.upto( nr_pages < max_pages ? nr_pages : max_pages ) do |page_nr|
318
+ query = Amazon::AWS.assemble_query(
319
+ q_params.merge( { page_parameter => page_nr } ) )
320
+ page = Amazon::AWS.get_page( self, query )
321
+ doc = Document.new( page )
322
+
323
+ # Check for errors.
324
+ #
325
+ error_check( doc.elements['*/OperationRequest'] )
326
+ error_check( doc.elements['*/*/Request'] )
327
+
328
+ # Create a new AWS object and walk the XML response tree.
329
+ #
330
+ aws = AWS::AWSObject.new
331
+ aws.walk( doc )
332
+
333
+ # When dealing with multiple pages, we return not just an
334
+ # AWSObject, but an array of them.
335
+ #
336
+ result = [ result ] unless result.is_a? Array
337
+
338
+ # Append the new object to the array.
339
+ #
340
+ result << aws
341
+ end
342
+
343
+ # Yield each object to the block, if given.
344
+ #
345
+ result.each { |r| yield r } if block_given?
346
+
347
+ result
348
+ end
349
+
350
+ end
351
+
352
+ end
353
+
354
+ end
355
+
356
+ end