File Coverage

blib/lib/Amazon/SQS/Simple/Queue.pm
Criterion Covered Total %
statement 24 141 17.0
branch 0 28 0.0
condition 0 14 0.0
subroutine 8 23 34.7
pod 13 14 92.8
total 45 220 20.4


line stmt bran cond sub pod time code
1             package Amazon::SQS::Simple::Queue;
2              
3 2     2   7 use strict;
  2         2  
  2         45  
4 2     2   6 use warnings;
  2         2  
  2         50  
5 2     2   691 use Amazon::SQS::Simple::Message;
  2         2  
  2         41  
6 2     2   628 use Amazon::SQS::Simple::SendResponse;
  2         4  
  2         42  
7 2     2   19 use Carp qw( croak carp );
  2         2  
  2         77  
8              
9 2     2   6 use base 'Amazon::SQS::Simple::Base';
  2         2  
  2         105  
10 2     2   7 use Amazon::SQS::Simple::Base; # for constants
  2         2  
  2         72  
11              
12 2     2   8 use overload '""' => \&_to_string;
  2         2  
  2         14  
13              
14             sub Endpoint {
15 0     0 1   my $self = shift;
16 0           return $self->{Endpoint};
17             }
18              
19             sub Delete {
20 0     0 1   my $self = shift;
21 0           my $params = { Action => 'DeleteQueue' };
22            
23 0           my $href = $self->_dispatch($params);
24             }
25              
26             sub Purge {
27 0     0 0   my $self = shift;
28 0           my $params = { Action => 'PurgeQueue' };
29            
30 0           my $href = $self->_dispatch($params);
31             }
32              
33             sub SendMessage {
34 0     0 1   my ($self, $message, %params) = @_;
35            
36 0           $params{Action} = 'SendMessage';
37 0           $params{MessageBody} = $message;
38            
39 0           my $href = $self->_dispatch(\%params);
40              
41             # default to most recent version
42             return new Amazon::SQS::Simple::SendResponse(
43 0           $href->{SendMessageResult}, $message
44             );
45             }
46              
47             sub SendMessageBatch {
48 0     0 1   my ($self, $messages, %params) = @_;
49            
50 0           $params{Action} = 'SendMessageBatch';
51            
52 0 0         if (ref($messages) eq 'ARRAY'){
53 0           my %messages;
54 0           my @IDs = map { "msg_$_" } (1..scalar(@$messages));
  0            
55 0           @messages{@IDs} = @$messages;
56 0           $messages = \%messages;
57             }
58            
59 0           my $i=0;
60 0           while (my ($id, $msg) = each %$messages){
61 0 0         if ($i==10){
62 0           warn "Batch messaging limited to 10 messages";
63 0           last;
64             }
65 0           $i++;
66 0           $params{"SendMessageBatchRequestEntry.$i.Id"} = $id;
67 0           $params{"SendMessageBatchRequestEntry.$i.MessageBody"} = $msg;
68             }
69            
70 0           my $href = $self->_dispatch(\%params, [qw/SendMessageBatchResultEntry/]);
71 0           my @responses = ();
72            
73             # default to most recent version
74 0           for (@{$href->{SendMessageBatchResult}{SendMessageBatchResultEntry}}) {
  0            
75 0           push @responses, new Amazon::SQS::Simple::SendResponse($_, $messages->{$_->{Id}});
76             }
77            
78 0 0         if (wantarray){
79 0           return @responses;
80             }
81             else {
82 0           return \@responses;
83             }
84             }
85              
86             sub ReceiveMessage {
87 0     0 1   my ($self, %params) = @_;
88            
89 0           $params{Action} = 'ReceiveMessage';
90            
91 0           my $href = $self->_dispatch(\%params, [qw(Message)]);
92              
93 0           my @messages = ();
94              
95             # default to most recent version
96 0 0         if (defined $href->{ReceiveMessageResult}{Message}) {
97 0           foreach (@{$href->{ReceiveMessageResult}{Message}}) {
  0            
98 0           push @messages, new Amazon::SQS::Simple::Message(
99             $_,
100             $self->_api_version()
101             );
102             }
103             }
104            
105 0 0         if (wantarray) {
    0          
106 0           return @messages;
107             }
108             elsif (@messages) {
109 0           return $messages[0];
110             }
111             else {
112 0           return undef;
113             }
114             }
115              
116             sub ReceiveMessageBatch {
117 0     0 1   my ($self, %params) = @_;
118 0           $params{MaxNumberOfMessages} = 10;
119 0           $self->ReceiveMessage(%params);
120             }
121              
122             sub DeleteMessage {
123 0     0 1   my ($self, $message, %params) = @_;
124            
125             # to be consistent with DeleteMessageBatch, this will now accept a message object
126 0           my $receipt_handle;
127 0 0 0       if (ref($message) && $message->isa('Amazon::SQS::Simple::Message')){
128 0           $receipt_handle = $message->ReceiptHandle;
129             }
130             # for backward compatibility, we will still cope with a receipt handle
131             else {
132 0           $receipt_handle = $message;
133             }
134 0           $params{Action} = 'DeleteMessage';
135 0           $params{ReceiptHandle} = $receipt_handle;
136            
137 0           my $href = $self->_dispatch(\%params);
138             }
139              
140             sub DeleteMessageBatch {
141 0     0 1   my ($self, $messages, %params) = @_;
142 0 0         return unless @$messages;
143 0           $params{Action} = 'DeleteMessageBatch';
144            
145 0           my $i=0;
146 0           foreach my $msg (@$messages){
147 0           $i++;
148 0 0         if ($i>10){
149 0           warn "Batch deletion limited to 10 messages";
150 0           last;
151             }
152            
153 0           $params{"DeleteMessageBatchRequestEntry.$i.Id"} = $msg->MessageId;
154 0           $params{"DeleteMessageBatchRequestEntry.$i.ReceiptHandle"} = $msg->ReceiptHandle;
155             }
156            
157 0           my $href = $self->_dispatch(\%params);
158             }
159              
160             sub ChangeMessageVisibility {
161 0     0 1   my ($self, $receipt_handle, $timeout, %params) = @_;
162            
163 0 0 0       if (!defined($timeout) || $timeout =~ /\D/ || $timeout < 0 || $timeout > 43200) {
      0        
      0        
164 0           croak "timeout must be specified and in range 0..43200";
165             }
166            
167 0           $params{Action} = 'ChangeMessageVisibility';
168 0           $params{ReceiptHandle} = $receipt_handle;
169 0           $params{VisibilityTimeout} = $timeout;
170            
171 0           my $href = $self->_dispatch(\%params);
172             }
173              
174             our %valid_permission_actions = map { $_ => 1 } qw(* SendMessage ReceiveMessage DeleteMessage ChangeMessageVisibility GetQueueAttributes);
175              
176             sub AddPermission {
177 0     0 1   my ($self, $label, $account_actions, %params) = @_;
178            
179 0           $params{Action} = 'AddPermission';
180 0           $params{Label} = $label;
181 0           my $i = 1;
182 0           foreach my $account_id (keys %$account_actions) {
183 0 0         $account_id =~ /^\d{12}$/ or croak "Account IDs passed to AddPermission should be 12 digit AWS account numbers, no hyphens";
184 0           my $actions = $account_actions->{$account_id};
185 0           my @actions;
186 0 0         if (UNIVERSAL::isa($actions, 'ARRAY')) {
187 0           @actions = @$actions;
188             } else {
189 0           @actions = ($actions);
190             }
191 0           foreach my $action (@actions) {
192 0 0         exists $valid_permission_actions{$action}
193             or croak "Action passed to AddPermission must be one of "
194             . join(', ', sort keys %valid_permission_actions);
195            
196 0           $params{"AWSAccountId.$i"} = $account_id;
197 0           $params{"ActionName.$i"} = $action;
198 0           $i++;
199             }
200             }
201 0           my $href = $self->_dispatch(\%params);
202             }
203              
204             sub RemovePermission {
205 0     0 1   my ($self, $label, %params) = @_;
206            
207 0           $params{Action} = 'RemovePermission';
208 0           $params{Label} = $label;
209 0           my $href = $self->_dispatch(\%params);
210             }
211              
212             sub GetAttributes {
213 0     0 1   my ($self, %params) = @_;
214            
215 0           $params{Action} = 'GetQueueAttributes';
216              
217 0           my %result;
218             # default to the current version
219 0   0       $params{AttributeName} ||= 'All';
220              
221 0           my $href = $self->_dispatch(\%params, [ 'Attribute' ]);
222              
223 0 0         if ($href->{GetQueueAttributesResult}) {
224 0           foreach my $attr (@{$href->{GetQueueAttributesResult}{Attribute}}) {
  0            
225 0           $result{$attr->{Name}} = $attr->{Value};
226             }
227             }
228 0           return \%result;
229             }
230              
231             sub SetAttribute {
232 0     0 1   my ($self, $key, $value, %params) = @_;
233            
234 0           $params{Action} = 'SetQueueAttributes';
235 0           $params{'Attribute.Name'} = $key;
236 0           $params{'Attribute.Value'} = $value;
237            
238 0           my $href = $self->_dispatch(\%params);
239             }
240              
241             sub _to_string {
242 0     0     my $self = shift;
243 0           return $self->Endpoint();
244             }
245              
246             1;
247              
248             __END__
249              
250             =head1 NAME
251              
252             Amazon::SQS::Simple::Queue - OO API for representing queues from
253             the Amazon Simple Queue Service.
254              
255             =head1 SYNOPSIS
256              
257             use Amazon::SQS::Simple;
258              
259             my $access_key = 'foo'; # Your AWS Access Key ID
260             my $secret_key = 'bar'; # Your AWS Secret Key
261              
262             my $sqs = new Amazon::SQS::Simple($access_key, $secret_key);
263              
264             my $q = $sqs->CreateQueue('queue_name');
265              
266             # Single messages
267            
268             my $response = $q->SendMessage('Hello world!');
269             my $msg = $q->ReceiveMessage;
270             print $msg->MessageBody; # Hello world!
271             $q->DeleteMessage($msg);
272             # or, for backward compatibility
273             $q->DeleteMessage($msg->ReceiptHandle);
274            
275             # Batch messaging of up to 10 messages per operation
276            
277             my @responses = $q->SendMessageBatch( [ 'Hello world!', 'Hello again!' ] );
278             # or with defined message IDs
279             $q->SendMessageBatch( { msg1 => 'Hello world!', msg2 => 'Hello again!' } );
280             my @messages = $q->ReceiveMessageBatch;
281             $q->DeleteMessageBatch( \@messages );
282              
283             =head1 INTRODUCTION
284              
285             Don't instantiate this class directly. Objects of this class are returned
286             by various methods in C<Amazon::SQS::Simple>. See L<Amazon::SQS::Simple> for
287             more details.
288              
289             =head1 METHODS
290              
291             =over 2
292              
293             =item B<Endpoint()>
294              
295             Get the endpoint for the queue.
296              
297             =item B<Delete([%opts])>
298              
299             Deletes the queue. Any messages contained in the queue will be lost.
300              
301             =item B<SendMessage($message, [%opts])>
302              
303             Sends the message. The message can be up to 8KB in size and should be
304             plain text.
305              
306             =item B<SendMessageBatch($messages, [%opts])>
307              
308             Sends a batch of up to 10 messages, passed as an array-ref.
309             Message IDs (of the style 'msg_1', 'msg_2', etc) are auto-generated for each message.
310             Alternatively, if you need to specify the format of the message ID then you can pass a hash-ref {$id1 => $message1, etc}
311              
312             =item B<ReceiveMessage([%opts])>
313              
314             Get the next message from the queue.
315              
316             Returns one or more C<Amazon::SQS::Simple::Message> objects (depending on whether called in list or scalar context),
317             or undef if no messages are retrieved.
318              
319             NOTE: This behaviour has changed slightly since v1.06. It now always returns the first message in scalar
320             context, irrespective of how many there are.
321              
322             See L<Amazon::SQS::Simple::Message> for more details.
323              
324             Options for ReceiveMessage:
325              
326             =over 4
327              
328             =item * MaxNumberOfMessages => INTEGER
329              
330             Maximum number of messages to return (integer from 1 to 20). SQS never returns more messages than this value but might
331             return fewer. Not necessarily all the messages in the queue are returned. Defaults to 1.
332              
333             =item * WaitTimeSeconds => INTEGER
334              
335             Long poll support (integer from 0 to 20). The duration (in seconds) that the I<ReceiveMessage> action call will wait
336             until a message is in the queue to include in the response, as opposed to returning an empty response if a message
337             is not yet available.
338              
339             If you do not specify I<WaitTimeSeconds> in the request, the queue attribute I<ReceiveMessageWaitTimeSeconds>
340             is used to determine how long to wait.
341              
342             =item * VisibilityTimeout => INTEGER
343              
344             The duration in seconds (integer from 0 to 43200) that the received messages are hidden from subsequent retrieve
345             requests after being retrieved by a I<ReceiveMessage> request.
346              
347             If you do not specify I<VisibilityTimeout> in the request, the queue attribute I<VisibilityTimeout> is used to
348             determine how long to wait.
349              
350             =back
351              
352             =item B<ReceiveMessageBatch([%opts])>
353              
354             As ReceiveMessage(MaxNumberOfMessages => 10)
355              
356             =item B<DeleteMessage($message, [%opts])>
357              
358             Pass this method either a message object or receipt handle to delete that message from the queue.
359             For backward compatibility, can pass the message ReceiptHandle rather than the message.
360              
361             =item B<DeleteMessageBatch($messages, [%opts])>
362              
363             Pass this method an array-ref containing up to 10 message objects to delete all of those messages from the queue
364              
365             =item B<ChangeMessageVisibility($receipt_handle, $timeout, [%opts])>
366              
367             NOT SUPPORTED IN APIs EARLIER THAN 2009-01-01
368              
369             Changes the visibility of the message with the specified receipt handle to
370             C<$timeout> seconds. C<$timeout> must be in the range 0..43200.
371              
372             =item B<AddPermission($label, $account_actions, [%opts])>
373              
374             NOT SUPPORTED IN APIs EARLIER THAN 2009-01-01
375              
376             Sets a permissions policy with the specified label. C<$account_actions>
377             is a reference to a hash mapping 12-digit AWS account numbers to the action(s)
378             you want to permit for those account IDs. The hash value for each key can
379             be a string (e.g. "ReceiveMessage") or a reference to an array of strings
380             (e.g. ["ReceiveMessage", "DeleteMessage"])
381              
382             =item B<RemovePermission($label, [%opts])>
383              
384             NOT SUPPORTED IN APIs EARLIER THAN 2009-01-01
385              
386             Removes the permissions policy with the specified label.
387              
388             =item B<GetAttributes([%opts])>
389              
390             Get the attributes for the queue. Returns a reference to a hash
391             mapping attribute names to their values. Currently the following
392             attribute names are returned:
393              
394             =over 4
395              
396             =item * VisibilityTimeout
397              
398             =item * ApproximateNumberOfMessages
399              
400             =back
401              
402             =item B<SetAttribute($attribute_name, $attribute_value, [%opts])>
403              
404             Sets the value for a queue attribute. Currently the only valid
405             attribute name is C<VisibilityTimeout>.
406              
407             =back
408              
409             =head1 ACKNOWLEDGEMENTS
410              
411             Chris Jones provied the batch message code in release 2.0
412              
413             =head1 AUTHOR
414              
415             Copyright 2007-2008 Simon Whitaker E<lt>swhitaker@cpan.orgE<gt>
416             Copyright 2013-2017 Mike (no relation) Whitaker E<lt>penfold@cpan.orgE<gt>
417              
418             This program is free software; you can redistribute it and/or modify it
419             under the same terms as Perl itself.
420              
421             =cut