kerryb-right_aws 1.7.3

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,380 @@
1
+ #
2
+ # Copyright (c) 2007-2008 RightScale Inc
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
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+
24
+ module RightAws
25
+
26
+ #
27
+ # = RightAws::Sqs -- RightScale's Amazon SQS interface
28
+ # The RightAws::Sqs class provides a complete interface to Amazon's Simple
29
+ # Queue Service.
30
+ # For explanations of the semantics
31
+ # of each call, please refer to Amazon's documentation at
32
+ # http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=31
33
+ #
34
+ # Error handling: all operations raise an RightAws::AwsError in case
35
+ # of problems. Note that transient errors are automatically retried.
36
+ #
37
+ # sqs = RightAws::Sqs.new(aws_access_key_id, aws_secret_access_key)
38
+ # queue1 = sqs.queue('my_awesome_queue')
39
+ # ...
40
+ # queue2 = RightAws::Sqs::Queue.create(sqs, 'my_cool_queue', true)
41
+ # puts queue2.size
42
+ # ...
43
+ # message1 = queue2.receive
44
+ # message1.visibility = 0
45
+ # puts message1
46
+ # ...
47
+ # queue2.clear(true)
48
+ # queue2.send_message('Ola-la!')
49
+ # message2 = queue2.pop
50
+ # ...
51
+ # grantee1 = RightAws::Sqs::Grantee.create(queue2,'one_cool_guy@email.address')
52
+ # grantee1.grant('FULLCONTROL')
53
+ # grantee1.drop
54
+ # ...
55
+ # grantee2 = queue.grantees('another_cool_guy@email.address')
56
+ # grantee2.revoke('SENDMESSAGE')
57
+ #
58
+ class Sqs
59
+ attr_reader :interface
60
+
61
+ def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
62
+ @interface = SqsInterface.new(aws_access_key_id, aws_secret_access_key, params)
63
+ end
64
+
65
+ # Retrieves a list of queues.
66
+ # Returns an +array+ of +Queue+ instances.
67
+ #
68
+ # RightAws::Sqs.queues #=> array of queues
69
+ #
70
+ def queues(prefix=nil)
71
+ @interface.list_queues(prefix).map do |url|
72
+ Queue.new(self, url)
73
+ end
74
+ end
75
+
76
+ # Returns Queue instance by queue name.
77
+ # If the queue does not exist at Amazon SQS and +create+ is true, the method creates it.
78
+ #
79
+ # RightAws::Sqs.queue('my_awesome_queue') #=> #<RightAws::Sqs::Queue:0xb7b626e4 ... >
80
+ #
81
+ def queue(queue_name, create=true, visibility=nil)
82
+ url = @interface.queue_url_by_name(queue_name)
83
+ url = (create ? @interface.create_queue(queue_name, visibility) : nil) unless url
84
+ url ? Queue.new(self, url) : nil
85
+ end
86
+
87
+
88
+ class Queue
89
+ attr_reader :name, :url, :sqs
90
+
91
+ # Returns Queue instance by queue name.
92
+ # If the queue does not exist at Amazon SQS and +create+ is true, the method creates it.
93
+ #
94
+ # RightAws::Sqs::Queue.create(sqs, 'my_awesome_queue') #=> #<RightAws::Sqs::Queue:0xb7b626e4 ... >
95
+ #
96
+ def self.create(sqs, url_or_name, create=true, visibility=nil)
97
+ sqs.queue(url_or_name, create, visibility)
98
+ end
99
+
100
+ # Creates new Queue instance.
101
+ # Does not create a queue at Amazon.
102
+ #
103
+ # queue = RightAws::Sqs::Queue.new(sqs, 'my_awesome_queue')
104
+ #
105
+ def initialize(sqs, url_or_name)
106
+ @sqs = sqs
107
+ @url = @sqs.interface.queue_url_by_name(url_or_name)
108
+ @name = @sqs.interface.queue_name_by_url(@url)
109
+ end
110
+
111
+ # Retrieves queue size.
112
+ #
113
+ # queue.size #=> 1
114
+ #
115
+ def size
116
+ @sqs.interface.get_queue_length(@url)
117
+ end
118
+
119
+ # Clears queue.
120
+ # Deletes only the visible messages unless +force+ is +true+.
121
+ #
122
+ # queue.clear(true) #=> true
123
+ #
124
+ # P.S. when <tt>force==true</tt> the queue deletes then creates again. This is
125
+ # the quickest method to clear a big queue or a queue with 'locked' messages. All queue
126
+ # attributes are restored. But there is no way to restore grantees' permissions to
127
+ # this queue. If you have no grantees except 'root' then you have no problems.
128
+ # Otherwise, it's better to use <tt>queue.clear(false)</tt>.
129
+ #
130
+ # PS This function is no longer supported. Amazon has changed the SQS semantics to require at least 60 seconds between
131
+ # queue deletion and creation. Hence this method will fail with an exception.
132
+ #
133
+ def clear(force=false)
134
+ ## if force
135
+ ## @sqs.interface.force_clear_queue(@url)
136
+ ## else
137
+ @sqs.interface.clear_queue(@url)
138
+ ## end
139
+ end
140
+
141
+ # Deletes queue.
142
+ # Queue must be empty or +force+ must be set to +true+.
143
+ # Returns +true+.
144
+ #
145
+ # queue.delete(true) #=> true
146
+ #
147
+ def delete(force=false)
148
+ @sqs.interface.delete_queue(@url, force)
149
+ end
150
+
151
+ # Sends new message to queue.
152
+ # Returns new Message instance that has been sent to queue.
153
+ def send_message(message)
154
+ message = message.to_s
155
+ msg = Message.new(self, @sqs.interface.send_message(@url, message), message)
156
+ msg.sent_at = Time.now
157
+ msg
158
+ end
159
+ alias_method :push, :send_message
160
+
161
+ # Retrieves several messages from queue.
162
+ # Returns an array of Message instances.
163
+ #
164
+ # queue.receive_messages(2,10) #=> array of messages
165
+ #
166
+ def receive_messages(number_of_messages=1, visibility=nil)
167
+ list = @sqs.interface.receive_messages(@url, number_of_messages, visibility)
168
+ list.map! do |entry|
169
+ msg = Message.new(self, entry[:id], entry[:body], visibility)
170
+ msg.received_at = Time.now
171
+ msg
172
+ end
173
+ end
174
+
175
+ # Retrieves first accessible message from queue.
176
+ # Returns Message instance or +nil+ it the queue is empty.
177
+ #
178
+ # queue.receive #=> #<RightAws::Sqs::Message:0xb7bf0884 ... >
179
+ #
180
+ def receive(visibility=nil)
181
+ list = receive_messages(1, visibility)
182
+ list.empty? ? nil : list[0]
183
+ end
184
+
185
+ # Peeks message body.
186
+ #
187
+ # queue.peek #=> #<RightAws::Sqs::Message:0xb7bf0884 ... >
188
+ #
189
+ def peek(message_id)
190
+ entry = @sqs.interface.peek_message(@url, message_id)
191
+ msg = Message.new(self, entry[:id], entry[:body])
192
+ msg.received_at = Time.now
193
+ msg
194
+ end
195
+
196
+ # Pops (and deletes) first accessible message from queue.
197
+ # Returns Message instance or +nil+ it the queue is empty.
198
+ #
199
+ # queue.pop #=> #<RightAws::Sqs::Message:0xb7bf0884 ... >
200
+ #
201
+ def pop
202
+ msg = receive
203
+ msg.delete if msg
204
+ msg
205
+ end
206
+
207
+ # Retrieves +VisibilityTimeout+ value for the queue.
208
+ # Returns new timeout value.
209
+ #
210
+ # queue.visibility #=> 30
211
+ #
212
+ def visibility
213
+ @sqs.interface.get_visibility_timeout(@url)
214
+ end
215
+
216
+ # Sets new +VisibilityTimeout+ for the queue.
217
+ # Returns new timeout value.
218
+ #
219
+ # queue.visibility #=> 30
220
+ # queue.visibility = 33
221
+ # queue.visibility #=> 33
222
+ #
223
+ def visibility=(visibility_timeout)
224
+ @sqs.interface.set_visibility_timeout(@url, visibility_timeout)
225
+ visibility_timeout
226
+ end
227
+
228
+ # Sets new queue attribute value.
229
+ # Not all attributes may be changed: +ApproximateNumberOfMessages+ (for example) is a read only attribute.
230
+ # Returns a value to be assigned to attribute.
231
+ #
232
+ # queue.set_attribute('VisibilityTimeout', '100') #=> '100'
233
+ # queue.get_attribute('VisibilityTimeout') #=> '100'
234
+ #
235
+ def set_attribute(attribute, value)
236
+ @sqs.interface.set_queue_attributes(@url, attribute, value)
237
+ value
238
+ end
239
+
240
+ # Retrieves queue attributes.
241
+ # At this moment Amazon supports +VisibilityTimeout+ and +ApproximateNumberOfMessages+ only.
242
+ # If the name of attribute is set, returns its value. Otherwise, returns a hash of attributes.
243
+ #
244
+ # queue.get_attribute('VisibilityTimeout') #=> '100'
245
+ #
246
+ def get_attribute(attribute='All')
247
+ attributes = @sqs.interface.get_queue_attributes(@url, attribute)
248
+ attribute=='All' ? attributes : attributes[attribute]
249
+ end
250
+
251
+ # Retrieves a list of grantees.
252
+ # Returns an +array+ of Grantee instances if the +grantee_email_address+ is unset.
253
+ # Otherwise returns a Grantee instance that points to +grantee_email_address+ or +nil+.
254
+ #
255
+ # grantees = queue.grantees #=> [#<RightAws::Sqs::Grantee:0xb7bf0888 ... >, ...]
256
+ # ...
257
+ # grantee = queue.grantees('cool_guy@email.address') #=> nil | #<RightAws::Sqs::Grantee:0xb7bf0888 ... >
258
+ #
259
+ def grantees(grantee_email_address=nil, permission = nil)
260
+ hash = @sqs.interface.list_grants(@url, grantee_email_address, permission)
261
+ grantees = []
262
+ hash.each do |key, value|
263
+ grantees << Grantee.new(self, grantee_email_address, key, value[:name], value[:perms])
264
+ end
265
+ if grantee_email_address
266
+ grantees.blank? ? nil : grantees.shift
267
+ else
268
+ grantees
269
+ end
270
+ end
271
+ end
272
+
273
+
274
+ class Message
275
+ attr_reader :queue, :id, :body, :visibility
276
+ attr_accessor :sent_at, :received_at
277
+
278
+ def initialize(queue, id=nil, body=nil, visibility=nil)
279
+ @queue = queue
280
+ @id = id
281
+ @body = body
282
+ @visibility = visibility
283
+ @sent_at = nil
284
+ @received_at = nil
285
+ end
286
+
287
+ # Returns +Message+ instance body.
288
+ def to_s
289
+ @body
290
+ end
291
+
292
+ # Changes +VisibilityTimeout+ for current message.
293
+ # Returns new +VisibilityTimeout+ value.
294
+ def visibility=(visibility_timeout)
295
+ @queue.sqs.interface.change_message_visibility(@queue.url, @id, visibility_timeout)
296
+ @visibility = visibility_timeout
297
+ end
298
+
299
+ # Removes message from queue.
300
+ # Returns +true+.
301
+ def delete
302
+ @queue.sqs.interface.delete_message(@queue.url, @id)
303
+ end
304
+ end
305
+
306
+
307
+ class Grantee
308
+ attr_accessor :queue, :id, :name, :perms, :email
309
+
310
+ # Creates new Grantee instance.
311
+ # To create new grantee for queue use:
312
+ #
313
+ # grantee = Grantee.new(queue, grantee@email.address)
314
+ # grantee.grant('FULLCONTROL')
315
+ #
316
+ def initialize(queue, email=nil, id=nil, name=nil, perms=[])
317
+ @queue = queue
318
+ @id = id
319
+ @name = name
320
+ @perms = perms
321
+ @email = email
322
+ retrieve unless id
323
+ end
324
+
325
+ # Retrieves security information for grantee identified by email.
326
+ # Returns +nil+ if the named user has no privileges on this queue, or
327
+ # +true+ if perms updated successfully.
328
+ def retrieve # :nodoc:
329
+ @id = nil
330
+ @name = nil
331
+ @perms = []
332
+
333
+ hash = @queue.sqs.interface.list_grants(@queue.url, @email)
334
+ return nil if hash.empty?
335
+
336
+ grantee = hash.shift
337
+ @id = grantee[0]
338
+ @name = grantee[1][:name]
339
+ @perms = grantee[1][:perms]
340
+ true
341
+ end
342
+
343
+ # Adds permissions for grantee.
344
+ # Permission: 'FULLCONTROL' | 'RECEIVEMESSAGE' | 'SENDMESSAGE'.
345
+ # The caller must have set the email instance variable.
346
+ def grant(permission=nil)
347
+ raise "You can't grant permission without defining a grantee email address!" unless @email
348
+ @queue.sqs.interface.add_grant(@queue.url, @email, permission)
349
+ retrieve
350
+ end
351
+
352
+ # Revokes permissions for grantee.
353
+ # Permission: 'FULLCONTROL' | 'RECEIVEMESSAGE' | 'SENDMESSAGE'.
354
+ # Default value is 'FULLCONTROL'.
355
+ # User must have +@email+ or +@id+ set.
356
+ # Returns +true+.
357
+ def revoke(permission='FULLCONTROL')
358
+ @queue.sqs.interface.remove_grant(@queue.url, @email || @id, permission)
359
+ unless @email # if email is unknown - just remove permission from local perms list...
360
+ @perms.delete(permission)
361
+ else # ... else retrieve updated information from Amazon
362
+ retrieve
363
+ end
364
+ true
365
+ end
366
+
367
+ # Revokes all permissions for this grantee.
368
+ # Returns +true+
369
+ def drop
370
+ @perms.each do |permission|
371
+ @queue.sqs.interface.remove_grant(@queue.url, @email || @id, permission)
372
+ end
373
+ retrieve
374
+ true
375
+ end
376
+
377
+ end
378
+
379
+ end
380
+ end
@@ -0,0 +1,278 @@
1
+ #
2
+ # Copyright (c) 2008 RightScale Inc
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
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+
24
+ module RightAws
25
+
26
+ #
27
+ # RightAws::SqsGen2 -- RightScale's Amazon SQS interface, API version
28
+ # 2008-01-01 and later.
29
+ # The RightAws::SqsGen2 class provides a complete interface to the second generation of Amazon's Simple
30
+ # Queue Service.
31
+ # For explanations of the semantics
32
+ # of each call, please refer to Amazon's documentation at
33
+ # http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=31
34
+ #
35
+ #
36
+ # RightAws::SqsGen2 is built atop RightAws::SqsGen2Interface, a lower-level
37
+ # procedural API that may be appropriate for certain programs.
38
+ #
39
+ # Error handling: all operations raise an RightAws::AwsError in case
40
+ # of problems. Note that transient errors are automatically retried.
41
+ #
42
+ # sqs = RightAws::SqsGen2.new(aws_access_key_id, aws_secret_access_key)
43
+ # queue1 = sqs.queue('my_awesome_queue')
44
+ # ...
45
+ # queue2 = RightAws::SqsGen2::Queue.create(sqs, 'my_cool_queue', true)
46
+ # puts queue2.size
47
+ # ...
48
+ # message1 = queue2.receive
49
+ # message1.visibility = 0
50
+ # puts message1
51
+ # ...
52
+ # queue2.clear(true)
53
+ # queue2.send_message('Ola-la!')
54
+ # message2 = queue2.pop
55
+ # ...
56
+ #
57
+ # NB: Second-generation SQS has eliminated the entire access grant mechanism present in Gen 1.
58
+ class SqsGen2
59
+ attr_reader :interface
60
+
61
+ def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
62
+ @interface = SqsGen2Interface.new(aws_access_key_id, aws_secret_access_key, params)
63
+ end
64
+
65
+ # Retrieves a list of queues.
66
+ # Returns an +array+ of +Queue+ instances.
67
+ #
68
+ # RightAws::Sqs.queues #=> array of queues
69
+ #
70
+ def queues(prefix=nil)
71
+ @interface.list_queues(prefix).map do |url|
72
+ Queue.new(self, url)
73
+ end
74
+ end
75
+
76
+ # Returns Queue instance by queue name.
77
+ # If the queue does not exist at Amazon SQS and +create+ is true, the method creates it.
78
+ #
79
+ # RightAws::SqsGen2.queue('my_awesome_queue') #=> #<RightAws::SqsGen2::Queue:0xb7b626e4 ... >
80
+ #
81
+ def queue(queue_name, create=true, visibility=nil)
82
+ url = @interface.queue_url_by_name(queue_name)
83
+ url = (create ? @interface.create_queue(queue_name, visibility) : nil) unless url
84
+ url ? Queue.new(self, url) : nil
85
+ end
86
+
87
+
88
+ class Queue
89
+ attr_reader :name, :url, :sqs
90
+
91
+ # Returns Queue instance by queue name.
92
+ # If the queue does not exist at Amazon SQS and +create+ is true, the method creates it.
93
+ #
94
+ # RightAws::SqsGen2::Queue.create(sqs, 'my_awesome_queue') #=> #<RightAws::SqsGen2::Queue:0xb7b626e4 ... >
95
+ #
96
+ def self.create(sqs, url_or_name, create=true, visibility=nil)
97
+ sqs.queue(url_or_name, create, visibility)
98
+ end
99
+
100
+ # Creates new Queue instance.
101
+ # Does not create a queue at Amazon.
102
+ #
103
+ # queue = RightAws::SqsGen2::Queue.new(sqs, 'my_awesome_queue')
104
+ #
105
+ def initialize(sqs, url_or_name)
106
+ @sqs = sqs
107
+ @url = @sqs.interface.queue_url_by_name(url_or_name)
108
+ @name = @sqs.interface.queue_name_by_url(@url)
109
+ end
110
+
111
+ # Retrieves queue size.
112
+ #
113
+ # queue.size #=> 1
114
+ #
115
+ def size
116
+ @sqs.interface.get_queue_length(@url)
117
+ end
118
+
119
+ # Clears queue, deleting only the visible messages. Any message within its visibility
120
+ # timeout will not be deleted, and will re-appear in the queue in the
121
+ # future when the timeout expires.
122
+ #
123
+ # To delete all messages in a queue and eliminate the chance of any
124
+ # messages re-appearing in the future, it's best to delete the queue and
125
+ # re-create it as a new queue. Note that doing this will take at least 60
126
+ # s since SQS does not allow re-creation of a queue within this interval.
127
+ #
128
+ # queue.clear() #=> true
129
+ #
130
+ def clear()
131
+ @sqs.interface.clear_queue(@url)
132
+ end
133
+
134
+ # Deletes queue. Any messages in the queue will be permanently lost.
135
+ # Returns +true+.
136
+ #
137
+ # NB: Use with caution; severe data loss is possible!
138
+ #
139
+ # queue.delete(true) #=> true
140
+ #
141
+ def delete(force=false)
142
+ @sqs.interface.delete_queue(@url)
143
+ end
144
+
145
+ # Sends new message to queue.
146
+ # Returns new Message instance that has been sent to queue.
147
+ def send_message(message)
148
+ message = message.to_s
149
+ res = @sqs.interface.send_message(@url, message)
150
+ msg = Message.new(self, res['MessageId'], nil, message)
151
+ msg.send_checksum = res['MD5OfMessageBody']
152
+ msg.sent_at = Time.now
153
+ msg
154
+ end
155
+ alias_method :push, :send_message
156
+
157
+ # Retrieves several messages from queue.
158
+ # Returns an array of Message instances.
159
+ #
160
+ # queue.receive_messages(2,10) #=> array of messages
161
+ #
162
+ def receive_messages(number_of_messages=1, visibility=nil)
163
+ list = @sqs.interface.receive_message(@url, number_of_messages, visibility)
164
+ list.map! do |entry|
165
+ msg = Message.new(self, entry['MessageId'], entry['ReceiptHandle'],
166
+ entry['Body'], visibility)
167
+ msg.received_at = Time.now
168
+ msg.receive_checksum = entry['MD5OfBody']
169
+ msg
170
+ end
171
+ end
172
+
173
+ # Retrieves first accessible message from queue.
174
+ # Returns Message instance or +nil+ it the queue is empty.
175
+ #
176
+ # queue.receive #=> #<RightAws::SqsGen2::Message:0xb7bf0884 ... >
177
+ #
178
+ def receive(visibility=nil)
179
+ list = receive_messages(1, visibility)
180
+ list.empty? ? nil : list[0]
181
+ end
182
+
183
+ # Pops (and deletes) first accessible message from queue.
184
+ # Returns Message instance or +nil+ if the queue is empty.
185
+ #
186
+ # queue.pop #=> #<RightAws::SqsGen2::Message:0xb7bf0884 ... >
187
+ #
188
+ def pop
189
+ list = @sqs.interface.pop_messages(@url, 1)
190
+ return nil if list.empty?
191
+ entry = list[0]
192
+ msg = Message.new(self, entry['MessageId'], entry['ReceiptHandle'],
193
+ entry['Body'], visibility)
194
+ msg.received_at = Time.now
195
+ msg.receive_checksum = entry['MD5OfBody']
196
+ msg
197
+ end
198
+
199
+ # Retrieves +VisibilityTimeout+ value for the queue.
200
+ # Returns new timeout value.
201
+ #
202
+ # queue.visibility #=> 30
203
+ #
204
+ def visibility
205
+ @sqs.interface.get_queue_attributes(@url, 'VisibilityTimeout')['VisibilityTimeout']
206
+ end
207
+
208
+ # Sets new +VisibilityTimeout+ for the queue.
209
+ # Returns new timeout value.
210
+ #
211
+ # queue.visibility #=> 30
212
+ # queue.visibility = 33
213
+ # queue.visibility #=> 33
214
+ #
215
+ def visibility=(visibility_timeout)
216
+ @sqs.interface.set_queue_attributes(@url, 'VisibilityTimeout', visibility_timeout)
217
+ visibility_timeout
218
+ end
219
+
220
+ # Sets new queue attribute value.
221
+ # Not all attributes may be changed: +ApproximateNumberOfMessages+ (for example) is a read only attribute.
222
+ # Returns a value to be assigned to attribute.
223
+ # Currently, 'VisibilityTimeout' is the only settable queue attribute.
224
+ # Attempting to set non-existent attributes generates an indignant
225
+ # exception.
226
+ #
227
+ # queue.set_attribute('VisibilityTimeout', '100') #=> '100'
228
+ # queue.get_attribute('VisibilityTimeout') #=> '100'
229
+ #
230
+ def set_attribute(attribute, value)
231
+ @sqs.interface.set_queue_attributes(@url, attribute, value)
232
+ value
233
+ end
234
+
235
+ # Retrieves queue attributes.
236
+ # At this moment Amazon supports +VisibilityTimeout+ and +ApproximateNumberOfMessages+ only.
237
+ # If the name of attribute is set, returns its value. Otherwise, returns a hash of attributes.
238
+ #
239
+ # queue.get_attribute('VisibilityTimeout') #=> {"VisibilityTimeout"=>"45"}
240
+ #
241
+ def get_attribute(attribute='All')
242
+ attributes = @sqs.interface.get_queue_attributes(@url, attribute)
243
+ attribute=='All' ? attributes : attributes[attribute]
244
+ end
245
+ end
246
+
247
+ class Message
248
+ attr_reader :queue, :id, :body, :visibility, :receipt_handle
249
+ attr_accessor :sent_at, :received_at, :send_checksum, :receive_checksum
250
+
251
+ def initialize(queue, id=nil, rh = nil, body=nil, visibility=nil)
252
+ @queue = queue
253
+ @id = id
254
+ @receipt_handle = rh
255
+ @body = body
256
+ @visibility = visibility
257
+ @sent_at = nil
258
+ @received_at = nil
259
+ @send_checksum = nil
260
+ @receive_checksum = nil
261
+ end
262
+
263
+ # Returns +Message+ instance body.
264
+ def to_s
265
+ @body
266
+ end
267
+
268
+ # Removes message from queue.
269
+ # Returns +true+.
270
+ def delete
271
+ @queue.sqs.interface.delete_message(@queue.url, @receipt_handle) if @receipt_handle
272
+ end
273
+
274
+ end
275
+
276
+
277
+ end
278
+ end