~ubuntu-branches/ubuntu/vivid/libamazon-sqs-simple-perl/vivid

« back to all changes in this revision

Viewing changes to lib/Amazon/SQS/Simple/Queue.pm

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2010-02-16 14:29:13 UTC
  • Revision ID: james.westby@ubuntu.com-20100216142913-keyrxsi00ko2v1v0
Tags: upstream-1.03
ImportĀ upstreamĀ versionĀ 1.03

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Amazon::SQS::Simple::Queue;
 
2
 
 
3
use strict;
 
4
use warnings;
 
5
use Amazon::SQS::Simple::Message;
 
6
use Amazon::SQS::Simple::SendResponse;
 
7
 
 
8
use base 'Amazon::SQS::Simple::Base';
 
9
use Amazon::SQS::Simple::Base; # for constants
 
10
 
 
11
use overload '""' => \&_to_string;
 
12
 
 
13
sub Endpoint {
 
14
    my $self = shift;
 
15
    return $self->{Endpoint};
 
16
}
 
17
 
 
18
sub Delete {
 
19
    my $self = shift;
 
20
    my $force = shift; # from API 2007-05-01
 
21
    my $params = { Action => 'DeleteQueue' };
 
22
    $params->{ForceDeletion} = 'true' if $force && $self->_api_version() eq +SQS_VERSION_2007_05_01;
 
23
    
 
24
    my $href = $self->_dispatch($params);    
 
25
}
 
26
 
 
27
sub SendMessage {
 
28
    my ($self, $message, %params) = @_;
 
29
    
 
30
    $params{Action} = 'SendMessage';
 
31
    $params{MessageBody} = $message;
 
32
    
 
33
    my $href = $self->_dispatch(\%params);    
 
34
 
 
35
    if ($self->_api_version() eq +SQS_VERSION_2007_05_01) {
 
36
        return new Amazon::SQS::Simple::SendResponse(
 
37
            $href
 
38
        );
 
39
    }
 
40
    else {
 
41
        # default to most recent version
 
42
        return new Amazon::SQS::Simple::SendResponse(
 
43
            $href->{SendMessageResult}
 
44
        );
 
45
    }
 
46
}
 
47
 
 
48
sub ReceiveMessage {
 
49
    my ($self, %params) = @_;
 
50
    
 
51
    $params{Action} = 'ReceiveMessage';
 
52
    
 
53
    my $href = $self->_dispatch(\%params, [qw(Message)]);
 
54
 
 
55
    my @messages = ();
 
56
 
 
57
    if ($self->_api_version() eq +SQS_VERSION_2007_05_01) {
 
58
        if (defined $href->{Message}) {
 
59
            foreach (@{$href->{Message}}) {
 
60
                push @messages, new Amazon::SQS::Simple::Message(
 
61
                    $_,
 
62
                    $self->_api_version()
 
63
                );
 
64
            }
 
65
        }
 
66
    }
 
67
    else {
 
68
        # default to most recent version
 
69
        if (defined $href->{ReceiveMessageResult}{Message}) {
 
70
            foreach (@{$href->{ReceiveMessageResult}{Message}}) {
 
71
                push @messages, new Amazon::SQS::Simple::Message(
 
72
                    $_,
 
73
                    $self->_api_version()
 
74
                );
 
75
            }
 
76
        }
 
77
    }
 
78
    
 
79
    if (@messages > 1 || wantarray) {
 
80
        return @messages;
 
81
    } elsif (@messages) {
 
82
        return $messages[0];
 
83
    } else {
 
84
        return undef;
 
85
    }
 
86
}
 
87
 
 
88
sub DeleteMessage {
 
89
    my ($self, $receipt_handle, %params) = @_;
 
90
    
 
91
    $params{Action} = 'DeleteMessage';
 
92
    if ($self->_api_version() eq +SQS_VERSION_2007_05_01) {
 
93
        $params{MessageId} = $receipt_handle;
 
94
    }
 
95
    else {
 
96
        # default to the current version
 
97
        $params{ReceiptHandle} = $receipt_handle;
 
98
    }
 
99
    
 
100
    my $href = $self->_dispatch(\%params);
 
101
}
 
102
 
 
103
sub GetAttributes {
 
104
    my ($self, %params) = @_;
 
105
    
 
106
    $params{Action}          = 'GetQueueAttributes';
 
107
 
 
108
    my %result;
 
109
    if ($self->_api_version() eq +SQS_VERSION_2007_05_01) {
 
110
        $params{Attribute} ||= 'All';
 
111
    
 
112
        my $href = $self->_dispatch(\%params, [ 'AttributedValue' ]);
 
113
 
 
114
        if ($href->{'AttributedValue'}) {
 
115
            foreach my $attr (@{$href->{'AttributedValue'}}) {
 
116
                $result{$attr->{Attribute}} = $attr->{Value};
 
117
            }
 
118
        }
 
119
    }
 
120
    else {
 
121
        # default to the current version
 
122
        $params{AttributeName} ||= 'All';
 
123
    
 
124
        my $href = $self->_dispatch(\%params, [ 'Attribute' ]);
 
125
 
 
126
        if ($href->{GetQueueAttributesResult}) {
 
127
            foreach my $attr (@{$href->{GetQueueAttributesResult}{Attribute}}) {
 
128
                $result{$attr->{Name}} = $attr->{Value};
 
129
            }
 
130
        }
 
131
    }
 
132
    
 
133
    return \%result;
 
134
}
 
135
 
 
136
sub SetAttribute {
 
137
    my ($self, $key, $value, %params) = @_;
 
138
    
 
139
    $params{Action}             = 'SetQueueAttributes';
 
140
    if ($self->_api_version() eq +SQS_VERSION_2007_05_01) {
 
141
        $params{Attribute} = $key;
 
142
        $params{Value}     = $value;
 
143
    }
 
144
    else {
 
145
        # default to the current version
 
146
        $params{'Attribute.Name'}   = $key;
 
147
        $params{'Attribute.Value'}  = $value;
 
148
    }
 
149
    
 
150
    my $href = $self->_dispatch(\%params);
 
151
}
 
152
 
 
153
sub _to_string {
 
154
    my $self = shift;
 
155
    return $self->Endpoint();
 
156
}
 
157
 
 
158
1;
 
159
 
 
160
__END__
 
161
 
 
162
=head1 NAME
 
163
 
 
164
Amazon::SQS::Simple::Queue - OO API for representing queues from 
 
165
the Amazon Simple Queue Service.
 
166
 
 
167
=head1 SYNOPSIS
 
168
 
 
169
    use Amazon::SQS::Simple;
 
170
 
 
171
    my $access_key = 'foo'; # Your AWS Access Key ID
 
172
    my $secret_key = 'bar'; # Your AWS Secret Key
 
173
 
 
174
    my $sqs = new Amazon::SQS::Simple($access_key, $secret_key);
 
175
 
 
176
    my $q = $sqs->CreateQueue('queue_name');
 
177
 
 
178
    $q->SendMessage('Hello world!');
 
179
 
 
180
    my $msg = $q->ReceiveMessage();
 
181
 
 
182
    print $msg->MessageBody() # Hello world!
 
183
 
 
184
    $q->DeleteMessage($msg->MessageId());
 
185
 
 
186
=head1 INTRODUCTION
 
187
 
 
188
Don't instantiate this class directly. Objects of this class are returned
 
189
by various methods in C<Amazon::SQS::Simple>. See L<Amazon::SQS::Simple> for
 
190
more details.
 
191
 
 
192
=head1 METHODS
 
193
 
 
194
=over 2
 
195
 
 
196
=item B<Endpoint()>
 
197
 
 
198
Get the endpoint for the queue.
 
199
 
 
200
=item B<Delete([%opts])>
 
201
 
 
202
Deletes the queue. Any messages contained in the queue will be lost.
 
203
 
 
204
=item B<SendMessage($message, [%opts])>
 
205
 
 
206
Sends the message. The message can be up to 8KB in size and should be
 
207
plain text.
 
208
 
 
209
=item B<ReceiveMessage([%opts])>
 
210
 
 
211
Get the next message from the queue.
 
212
 
 
213
Returns an C<Amazon::SQS::Simple::Message> object. See 
 
214
L<Amazon::SQS::Simple::Message> for more details.
 
215
 
 
216
If MaxNumberOfMessages is greater than 1, the method returns
 
217
an array of C<Amazon::SQS::Simple::Message> objects.
 
218
 
 
219
Options for ReceiveMessage:
 
220
 
 
221
=over 4
 
222
 
 
223
=item * MaxNumberOfMessages => NUMBER
 
224
 
 
225
Maximum number of messages to return. Value should be an integer between 1
 
226
and 10 inclusive. Default is 1. 
 
227
 
 
228
=back
 
229
 
 
230
=item B<DeleteMessage($receipt_handle, [%opts])>
 
231
 
 
232
Delete the message with the specified receipt handle from the queue
 
233
 
 
234
=item B<GetAttributes([%opts])>
 
235
 
 
236
Get the attributes for the queue. Returns a reference to a hash
 
237
mapping attribute names to their values. Currently the following
 
238
attribute names are returned:
 
239
 
 
240
=over 4
 
241
 
 
242
=item * VisibilityTimeout
 
243
 
 
244
=item * ApproximateNumberOfMessages
 
245
 
 
246
=back
 
247
 
 
248
=item B<SetAttribute($attribute_name, $attribute_value, [%opts])>
 
249
 
 
250
Sets the value for a queue attribute. Currently the only valid
 
251
attribute name is C<VisibilityTimeout>.
 
252
 
 
253
=back
 
254
 
 
255
=head1 AUTHOR
 
256
 
 
257
Copyright 2007-2008 Simon Whitaker E<lt>swhitaker@cpan.orgE<gt>
 
258
 
 
259
This program is free software; you can redistribute it and/or modify it
 
260
under the same terms as Perl itself.
 
261
 
 
262
=cut