ProducerConsumer/Base.pm

filter: Statementstmt 21.7% 25 / 115 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 34.7% 8 / 23 Podpod 0.0% 0 / 15 totaltotal 15.8% 33 / 208 CC 24 SCAR 25.5
CC
24
Cov
15%
sign_and_post
42.0
list_queues
20.0
debug
20.0
CRAP 12.9
« Consumer.pm ↑ Summary
1 package Amazon::SQS::ProducerConsumer::Base;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Digest::HMAC_SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URI::Escape qw(uri_escape_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 =head1 NAME
15  
16 Amazon::SQS::ProducerConsumer::Base - Perl interface to the Amazon Simple Queue Service (SQS) environment
17  
18 =head1 VERSION
19  
20 Version 0.04
21  
22 =cut
23  
24 1 our $VERSION = '0.04';
25  
26 =head1 SYNOPSIS
27  
28 use Amazon::SQS::ProducerConsumer::Base;
29  
30 my $sqs = new Amazon::SQS::ProducerConsumer::Base
31 AWSAccessKeyId => 'PUBLIC_KEY_HERE',
32 SecretAccessKey => 'SECRET_KEY_HERE';
33  
34 # Create a queue
35 my $queueURL = $sqs->create_queue( QueueName => 'TestQueue' );
36  
37 # Send a message to that queue
38 my $messageID = $sqs->send_message( Queue => $queueURL, MessageBody => 'Test message' );
39  
40 # Get a message from that queue
41 my $message = $sqs->receive_message( Queue => $queueURL );
42 print 'Message ID: ', $message->{MessageId}, "\n";
43 print 'Message: ', $message->{MessageBody}, "\n";
44  
45 # Delete the message you got
46 my $message = $sqs->delete_message( Queue => $queueURL, MessageId => $message->{MessageId} );
47  
48 If an error occurs in communicating with SQS, the return value will be undef and $sqs->{error} will be populated with the message.
49  
50 =cut
51  
52 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
53 0 my ($class, %args) = @_;
54  
55 0 my $me = \%args;
56 0 bless $me, $class;
57 0 $me->initialize;
58 0 return $me;
59 }
60  
61 0 ▶sub initialize {
Subroutine initialize: not called CC=2 SCAR=17.9
Pod undocumented
62 0 my $me = shift;
63 0 $me->{signature_version} = 2;
64 0 $me->{version} = '2009-02-01';
65 0 ▶ $me->{host} ||= 'queue.amazonaws.com';
Condition0%0/2
$me->{"host"} ||= "queue.amazonaws.com"
l!l
00
MC/DC0%0/1
$me->{"host"} ||= "queue.amazonaws.com"
$me->{"host"}
Truth table0%0/2
A
A= $me->{"host"}
Aresult
11
00
66 }
67  
68 0 ▶sub create_queue {
Subroutine create_queue: not called CC=2 SCAR=17.9
Pod undocumented
69 0 my ($me, %args) = @_;
70  
71 0 my $xml = $me->sign_and_post( Action => 'CreateQueue', %args );
72 0 ▶ return undef if $me->check_error( $xml );
Branch: 1 branch
BranchTrueFalseTotal
if $me->check_error($xml) 0 0 0
73 0 return $xml->{CreateQueueResult}{QueueUrl};
74 }
75  
76 0 ▶sub list_queues {
Subroutine list_queues: not called CC=4 SCAR=30.0
Pod undocumented
77 0 my ($me, %args) = @_;
78  
79 0 my $xml = $me->sign_and_post( Action => 'ListQueues', %args );
80 0 ▶ return undef if $me->check_error( $xml );
Branch: 1 branch
BranchTrueFalseTotal
if $me->check_error($xml) 0 0 0
81  
82 0 my $result;
83 0 $result = $xml->{ListQueuesResult}{QueueUrl};
84 0 ▶ map { $_ = (split '/', $_)[-1] } @$result if ref $result eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result eq "ARRAY" 0 0 0
85 0 ▶ return ref $result eq 'ARRAY' ? @$result : $result;
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'ARRAY' ? : 0 0 0
86 }
87  
88 0 ▶sub delete_queue {
Subroutine delete_queue: not called CC=2 SCAR=17.9
Pod undocumented
89 0 my ($me, %args) = @_;
90  
91 0 delete $args{ForceDeletion};
92 0 my $xml = $me->sign_and_post( Action => 'DeleteQueue', %args );
93 0 ▶ return undef if $me->check_error( $xml );
Branch: 1 branch
BranchTrueFalseTotal
if $me->check_error($xml) 0 0 0
94  
95 0 return $xml->{ResponseMetadata}{RequestId};
96 }
97  
98 0 ▶sub send_message {
Subroutine send_message: not called CC=2 SCAR=17.9
Pod undocumented
99 0 my ($me, %args) = @_;
100  
101 0 my $xml = $me->sign_and_post( Action => 'SendMessage', %args );
102 0 ▶ return undef if $me->check_error( $xml );
Branch: 1 branch
BranchTrueFalseTotal
if $me->check_error($xml) 0 0 0
103  
104 0 return $xml->{SendMessageResult}{MessageId};
105 }
106  
107 0 ▶sub receive_message {
Subroutine receive_message: not called CC=2 SCAR=17.9
Pod undocumented
108 0 my ($me, %args) = @_;
109  
110 0 delete $args{NumberOfMessages};
111 0 my $xml = $me->sign_and_post( Action => 'ReceiveMessage', %args );
112 0 ▶ return undef if $me->check_error( $xml );
Branch: 1 branch
BranchTrueFalseTotal
if $me->check_error($xml) 0 0 0
113  
114 0 return $xml->{ReceiveMessageResult}{Message};
115 }
116  
117 0 ▶sub receive_messages {
Subroutine receive_messages: not called CC=3 SCAR=24.8
Pod undocumented
118 0 my ($me, %args) = @_;
119  
120 0 my $xml = $me->sign_and_post( Action => 'ReceiveMessage', %args );
121 0 ▶ return undef if $me->check_error( $xml );
Branch: 1 branch
BranchTrueFalseTotal
if $me->check_error($xml) 0 0 0
122 0 my $result = $xml->{ReceiveMessageResult}{Message};
123 0 ▶ return ref $result eq 'ARRAY' ? $result : [ $result ];
Branch: 1 branch
BranchTrueFalseTotal
ref $result eq 'ARRAY' ? : 0 0 0
124 }
125  
126 0 ▶sub delete_message {
Subroutine delete_message: not called CC=2 SCAR=17.9
Pod undocumented
127 0 my ($me, %args) = @_;
128  
129 0 my $xml = $me->sign_and_post( Action => 'DeleteMessage', %args );
130 0 ▶ return undef if $me->check_error( $xml );
Branch: 1 branch
BranchTrueFalseTotal
if $me->check_error($xml) 0 0 0
131 0 return $xml->{ResponseMetadata}{RequestId};
132 }
133  
134 0 ▶sub get_queue_attributes {
Subroutine get_queue_attributes: not called CC=2 SCAR=17.9
Pod undocumented
135 0 my ($me, %args) = @_;
136  
137 0 my $xml = $me->sign_and_post( Action => 'GetQueueAttributes', %args );
138 0 ▶ return undef if $me->check_error( $xml );
Branch: 1 branch
BranchTrueFalseTotal
if $me->check_error($xml) 0 0 0
139 0 return $xml->{GetQueueAttributesResult}{Attribute}[0]{Value};
140 }
141  
142 0 ▶sub set_queue_attributes {
Subroutine set_queue_attributes: not called CC=2 SCAR=17.9
Pod undocumented
143 0 my ($me, %args) = @_;
144  
145 0 my $xml = $me->sign_and_post( Action => 'SetQueueAttributes', %args );
146 0 ▶ return undef if $me->check_error( $xml );
Branch: 1 branch
BranchTrueFalseTotal
if $me->check_error($xml) 0 0 0
147 0 return $xml->{ResponseMetadata}{RequestId};
148 }
149  
150 0 ▶sub sign_and_post {
Subroutine sign_and_post: not called CC=6 SCAR=37.4
Pod undocumented
151 0 my ($me, %args) = @_;
152  
153 0 ▶ $me->{resource_path} = join '/', '', grep $_, $args{AWSAccessKeyId}, delete $args{Queue} if exists $args{Queue};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"Queue"} 0 0 0
154 0 ▶ $me->{resource_path} ||= '/';
Condition0%0/2
$me->{"resource_path"} ||= "/"
l!l
00
MC/DC0%0/1
$me->{"resource_path"} ||= "/"
$me->{"resource_path"}
Truth table0%0/2
A
A= $me->{"resource_path"}
Aresult
11
00
155  
156 0 my @t = gmtime;
157  
158 0 $args{AWSAccessKeyId} = $me->{AWSAccessKeyId};
159 0 $args{SignatureVersion} = $me->{signature_version};
160 0 $args{SignatureMethod} = 'HmacSHA1';
161 0 $args{Version} = $me->{version};
162 0 $args{Timestamp} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ", $t[5]+1900, $t[4]+1, @t[3,2,1,0];
163 0 ▶ $args{MaxNumberOfMessages} = delete $args{NumberOfMessages} if $args{NumberOfMessages};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"NumberOfMessages"} 0 0 0
164  
165 0 my @params;
166 0 for ( sort keys %args ) {
167 0 push @params, join '=', $_, uri_escape_utf8( $args{$_}, "^A-Za-z0-9\-_.~" );
168 }
169  
170 0 $me->{resource_path} =~ s|http://$me->{host}/||;
171 my $string_to_sign = join( "\n",
172 0 'POST', $me->{host}, $me->{resource_path}, join( '&', @params )
173 );
174  
175 0 $me->debug("QUERY TO SIGN: $string_to_sign");
176  
177 0 my $hashed = Digest::HMAC_SHA1->new( $me->{SecretAccessKey} );
178 0 $hashed->add( $string_to_sign );
179 0 my $encoded = encode_base64( $hashed->digest, '' );
180 0 $me->debug("ENCODED SIGNATURE: $encoded");
181 0 $args{Signature} = $encoded;
182  
183 0 my $result = LWP::UserAgent->new->post( "http://$me->{host}$me->{resource_path}", \%args );
184  
185 0 $me->debug("REQUEST RETURNED: " . $result->content);
186  
187 0 ▶ if ( $result->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_success) { } 0 0 0
188 0 my $parser = XML::Simple->new( ForceArray => [ 'item', 'QueueURL','AttributedValue', 'Attribute' ] );
189 0 return $parser->XMLin( $result->content() );
190 } else {
191 0 return { Errors => { Error => { Message => 'HTTP POST failed with error ' . $result->status_line } } };
192 }
193  
194 }
195  
196 0 ▶sub check_error {
Subroutine check_error: not called CC=3 SCAR=24.8
Pod undocumented
197 0 my ($me, $xml) = @_;
198  
199 0 ▶ if ( defined $xml->{Errors} && defined $xml->{Errors}{Error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xml->{"Errors"} and defined $xml->{"Errors"}{"Error"}) 0 0 0
Condition0%0/3
defined $xml->{"Errors"} and defined $xml->{"Errors"}{"Error"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $xml->{"Errors"} and defined $xml->{"Errors"}{"Error"}
defined $xml->{"Errors"} defined $xml->{"Errors"}{"Error"}
Truth table0%0/3
A and B
A= defined $xml->{"Errors"}B= defined $xml->{"Errors"}{"Error"}
ABresult
0X0
100
111
200 0 $me->debug("ERROR: $xml->{Errors}{Error}{Message}");
201 0 $me->{error} = $xml->{Errors}{Error}{Message};
202 0 warn $me->{error};
203 0 return 1;
204 }
205 }
206  
207 0 ▶sub error { $_[0]->{error} }
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
208  
209 0 ▶sub debug {
Subroutine debug: not called CC=4 SCAR=30.0
Pod undocumented
210 0 my ($me, $message) = @_;
211  
212 0 ▶ if ((grep { defined && length } $me->{debug}) && $me->{debug} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {defined $_ and length $_;} $me->{"debug"} and $me->{"debug"} == 1) 0 0 0
Condition0%0/6
grep {defined $_ and length $_;} $me->{"debug"} and $me->{"debug"} == 1
!ll&&!rl&&r
000
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
grep {defined $_ and length $_;} $me->{"debug"} and $me->{"debug"} == 1
grep {defined $_ and length $_;} $me->{"debug"} $me->{"debug"} == 1
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A and B
A= grep {defined $_ and length $_;} $me->{"debug"}B= $me->{"debug"} == 1
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
213 0 warn "$message\n";
214 }
215 }
216  
217  
218 =head1 AUTHOR
219  
220 Nic Wolff, <nic@angel.net>
221  
222 =head1 BUGS
223  
224 Please report any bugs or feature requests to C<bug-amazon-sqs-producerconsumer at rt.cpan.org>, or through
225 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Amazon-SQS-ProducerConsumer>. I will be notified, and then you'll
226 automatically be notified of progress on your bug as I make changes.
227  
228  
229  
230  
231 =head1 SUPPORT
232  
233 You can find documentation for this module with the perldoc command.
234  
235 perldoc Amazon::SQS::ProducerConsumer::Base
236  
237  
238 You can also look for information at:
239  
240 =over 4
241  
242 =item * RT: CPAN's request tracker (report bugs here)
243  
244 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Amazon-SQS-ProducerConsumer>
245  
246 =item * AnnoCPAN: Annotated CPAN documentation
247  
248 L<http://annocpan.org/dist/Amazon-SQS-ProducerConsumer>
249  
250 =item * CPAN Ratings
251  
252 L<http://cpanratings.perl.org/d/Amazon-SQS-ProducerConsumer>
253  
254 =item * Search CPAN
255  
256 L<http://search.cpan.org/dist/Amazon-SQS-ProducerConsumer/>
257  
258 =back
259  
260  
261 =head1 ACKNOWLEDGEMENTS
262  
263  
264 =head1 LICENSE AND COPYRIGHT
265  
266 Copyright 2011 Nic Wolff.
267  
268 This program is free software; you can redistribute it and/or modify it
269 under the terms of either: the GNU General Public License as published
270 by the Free Software Foundation; or the Artistic License.
271  
272 See http://dev.perl.org/licenses/ for more information.
273  
274  
275 =cut
276  
277 1 1;
« Consumer.pm ↑ Summary