blib/lib/WebService/Gravatar.pm

filter: Statementstmt 19.5% 17 / 87 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 29.4% 5 / 17 Podpod 100% 11 / 11 totaltotal 22.9% 33 / 144 CC 14 SCAR 21.2
CC
14
Cov
15%
new
20.0
save_data
12.0
save_url
12.0
CRAP 8.3
↑ Summary
1 package WebService::Gravatar;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use RPC::XML::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 WebService::Gravatar - Perl interface to Gravatar API
13  
14 =head1 VERSION
15  
16 Version 0.11
17  
18 =cut
19  
20 2 our $VERSION = '0.11';
21  
22  
23 =head1 SYNOPSIS
24  
25 WebService::Gravatar provides an interface to Gravatar XML-RPC API.
26  
27 use WebService::Gravatar;
28 use MIME::Base64;
29  
30 # Create a new instance of WebService::Gravatar
31 my $grav = WebService::Gravatar->new(email => 'your@email.address',
32 apikey => 'your_API_key');
33  
34 # Get a list of addresses
35 my $addresses = $grav->addresses;
36  
37 if (defined $addresses) {
38 # Print the userimage URL for each e-mail address
39 foreach my $email (keys %$addresses) {
40 print $addresses->{$email}->{'userimage_url'} . "\n";
41 }
42 }
43 else {
44 # We have a problem
45 print STDERR "Error: " . $grav->errstr . "\n";
46 }
47  
48 # Read image file data
49 my $data;
50 {
51 local $/ = undef;
52 open(F, "< my_pretty_face.png");
53 $data = <F>;
54 close(F);
55 }
56
57 # Save the image as a new userimage
58 $grav->save_data(data => encode_base64($data), rating => 0);
59  
60 ...
61  
62 =head1 DESCRIPTION
63  
64 WebService::Gravatar is a Perl interface to Gravatar API. It aims at providing a
65 close representation of the basic XML-RPC API, as documented on Gravatar
66 website: L<http://en.gravatar.com/site/implement/xmlrpc/>. All the method names,
67 parameter names, and data structures are the same as in the API -- the only
68 exception is that in the API the methods are named with camelCase, while the
69 module uses lowercase_with_infix_underscores.
70  
71 =head1 METHODS
72  
73 All the instance methods return C<undef> on failure. More detailed error
74 information can be obtained by calling L<"err"> and L<"errstr">.
75  
76 =head2 new
77  
78 Creates a new instance of WebService::Gravatar.
79  
80 my $grav = WebService::Gravatar->new(email => 'your@email.address',
81 apikey => 'your_API_key');
82  
83 Parameters:
84  
85 =over 4
86  
87 =item * email
88  
89 B<(Required)> E-mail address.
90  
91 =item * apikey
92  
93 B<(Required)> API key. Can be ommitted if C<password> is defined.
94  
95 =item * password
96  
97 B<(Required)> Account password. Can be ommitted if C<apikey> is defined.
98  
99 =back
100  
101 =cut
102  
103 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
104 0 my $class = shift;
105 0 my %args = @_;
106
107 0 my $self = {};
108 0 bless($self, $class);
109
110 0 ▶ if (!defined $args{'email'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"email"}) 0 0 0
111 0 carp "Required parameter 'email' is not defined";
112 }
113
114 0 ▶ if (!defined $args{'apikey'} && !defined $args{'password'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"apikey"} or defined $args{"password"}) 0 0 0
Condition0%0/3
defined $args{"apikey"} or defined $args{"password"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $args{"apikey"} or defined $args{"password"}
defined $args{"apikey"} defined $args{"password"}
Truth table0%0/3
A or B
A= defined $args{"apikey"}B= defined $args{"password"}
ABresult
1X1
011
000
115 0 carp "Either the 'apikey' or 'password' parameter must be defined";
116 }
117
118 0 $self->{'err'} = undef;
119 0 $self->{'errstr'} = undef;
120
121 0 $self->{'apikey'} = $args{'apikey'};
122 0 $self->{'password'} = $args{'password'};
123
124 0 (my $email = $args{'email'}) =~ s/^\s+|\s+$//g;
125
126 0 $self->{'cli'} = RPC::XML::Client->new(
127 'https://secure.gravatar.com/xmlrpc?user=' . md5_hex(lc $email));
128
129 0 return $self;
130 }
131  
132 0 ▶sub _call {
Subroutine _call: not called CC=2 SCAR=17.9
133 0 my $self = shift;
134 0 my $method = shift;
135 my %args = (
136 'apikey' => $self->{'apikey'},
137 0 'password' => $self->{'password'},
138 @_
139 );
140
141 0 $self->{'err'} = undef;
142 0 $self->{'errstr'} = undef;
143
144 0 my $ret = $self->{'cli'}->send_request('grav.' . $method, \%args);
145
146 0 ▶ if ($ret->is_fault) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret->is_fault) { } 0 0 0
147 0 $self->{'err'} = $ret->{'faultCode'}->value;
148 0 $self->{'errstr'} = $ret->{'faultString'}->value;
149 0 return undef;
150 }
151 else {
152 0 return $ret->value;
153 }
154 }
155  
156 =head2 exists
157  
158 Checks whether a hash has a gravatar.
159  
160 $result = $grav->exists(hashes => ['e52beb5a6966554a02a56072cafebabe',
161 '62345cdd79773f62a87fcbc6abadbabe'])
162  
163 Parameters:
164  
165 =over 4
166  
167 =item * hashes
168  
169 B<(Required)> An array of email hashes to check.
170  
171 =back
172  
173 Returns: A reference to a hash that maps email hashes to statuses. Example:
174  
175 $result = {
176 'e52beb5a6966554a02a56072cafebabe' => '1',
177 '62345cdd79773f62a87fcbc6abadbabe' => '0'
178 };
179  
180 =cut
181  
182 0 ▶sub exists {
Subroutine exists: not called CC=2 SCAR=17.9
Pod documented
183 0 my $self = shift;
184 0 my %args = @_;
185
186 0 ▶ if (!defined $args{'hashes'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"hashes"}) 0 0 0
187 0 carp "Required parameter 'hashes' is not defined";
188 }
189
190 0 return $self->_call('exists', %args);
191 }
192  
193 =head2 addresses
194  
195 Gets a list of addresses for this account.
196  
197 $addresses = $grav->addresses;
198  
199 Returns: A reference to a hash that maps addresses to userimage data. Example:
200  
201 $addresses = {
202 'some@email.address' => {
203 'rating' => '0',
204 'userimage' => '8bfc8da2562a53ddd7e630a68badf00d',
205 'userimage_url' => 'http://en.gravatar.com/userimage/123456/8bfc8da2562a53ddd7e630a68badf00d.jpg'
206 },
207 'another@email.address' => {
208 'rating' => '1',
209 'userimage' => '90f269fe7b67d0ce49f96427deadbabe',
210 'userimage_url' => 'http://en.gravatar.com/userimage/123456/90f269fe7b67d0ce49f96427deadbabe.jpg'
211 }
212 };
213  
214 =cut
215  
216 0 ▶sub addresses {
Subroutine addresses: not called CC=1 SCAR=6.9
Pod documented
217 0 my $self = shift;
218  
219 0 return $self->_call('addresses');
220 }
221  
222 =head2 userimages
223  
224 Gets a list of userimages for this account.
225  
226 $userimages = $grav->userimages;
227  
228 Returns: A reference to a hash that maps userimages to data. Example:
229  
230 $userimages = {
231 '8bfc8da2562a53ddd7e630a68badf00d' => [
232 '0',
233 'http://en.gravatar.com/userimage/123456/8bfc8da2562a53ddd7e630a68badf00d.jpg'
234 ],
235 '90f269fe7b67d0ce49f96427deadbabe' => [
236 '1',
237 'http://en.gravatar.com/userimage/123456/90f269fe7b67d0ce49f96427deadbabe.jpg'
238 ]
239 };
240  
241 =cut
242  
243 0 ▶sub userimages {
Subroutine userimages: not called CC=1 SCAR=6.9
Pod documented
244 0 my $self = shift;
245
246 0 return $self->_call('userimages');
247 }
248  
249 =head2 save_data
250  
251 Saves binary image data as a userimage for this account.
252  
253 $grav->save_data(data => $data, rating => 1);
254  
255 Parameters:
256  
257 =over 4
258  
259 =item * data
260  
261 B<(Required)> A base64 encoded image.
262  
263 =item * rating
264  
265 B<(Required)> Rating.
266  
267 =back
268  
269 Returns: Userimage string.
270  
271 =cut
272  
273 0 ▶sub save_data {
Subroutine save_data: not called CC=3 SCAR=24.8
Pod documented
274 0 my $self = shift;
275 0 my %args = @_;
276
277 0 ▶ if (!defined $args{'data'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"data"}) 0 0 0
278 0 carp "Required parameter 'data' is not defined";
279 }
280
281 0 ▶ if (!defined $args{'rating'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"rating"}) 0 0 0
282 0 carp "Required parameter 'rating' is not defined";
283 }
284
285 0 return $self->_call('saveData', %args);
286 }
287  
288 =head2 save_url
289  
290 Reads an image via its URL and saves that as a userimage for this account.
291  
292 $grav->save_url(url => 'http://some.domain.com/image.png', rating => 0);
293  
294 Parameters:
295  
296 =over 4
297  
298 =item * url
299  
300 B<(Required)> A full URL to an image.
301  
302 =item * rating
303  
304 B<(Required)> Rating.
305  
306 =back
307  
308 Returns: Userimage string.
309  
310 =cut
311  
312 0 ▶sub save_url {
Subroutine save_url: not called CC=3 SCAR=24.8
Pod documented
313 0 my $self = shift;
314 0 my %args = @_;
315
316 0 ▶ if (!defined $args{'url'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"url"}) 0 0 0
317 0 carp "Required parameter 'url' is not defined";
318 }
319
320 0 ▶ if (!defined $args{'rating'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"rating"}) 0 0 0
321 0 carp "Required parameter 'rating' is not defined";
322 }
323
324 0 return $self->_call('saveUrl', %args);
325 }
326  
327 =head2 use_userimage
328  
329 Uses the specified userimage as a gravatar for one or more addresses on this
330 account.
331  
332 $grav->use_userimage(userimage => '9116aa83a568563290a681df61c0ffee'.
333 addresses => ['some@email.address', 'another@email.address']);
334  
335 Parameters:
336  
337 =over 4
338  
339 =item * userimage
340  
341 B<(Required)> The userimage to be used.
342  
343 =item * addresses
344  
345 B<(Required)> An array of email addresses for which this userimage will be used.
346  
347 =back
348  
349 Returns: 1 on success, undef on failure.
350  
351 =cut
352  
353 0 ▶sub use_userimage {
Subroutine use_userimage: not called CC=3 SCAR=24.8
Pod documented
354 0 my $self = shift;
355 0 my %args = @_;
356
357 0 ▶ if (!defined $args{'userimage'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"userimage"}) 0 0 0
358 0 carp "Required parameter 'userimage' is not defined";
359 }
360
361 0 ▶ if (!defined $args{'addresses'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"addresses"}) 0 0 0
362 0 carp "Required parameter 'addresses' is not defined";
363 }
364
365 0 return $self->_call('useUserimage', %args);
366 }
367  
368 =head2 remove_image
369  
370 Removes the userimage associated with one or more email addresses.
371  
372 $result = $grav->remove_image(addresses => ['some@email.address',
373 'another@email.address'])
374
375 Parameters:
376  
377 =over 4
378  
379 =item * addresses
380  
381 B<(Required)> An array of email addresses to remove userimages for.
382  
383 =back
384  
385 Returns: A reference to a hash that maps email addresses to statuses. Example:
386  
387 result = {
388 'some@email.address' => 1,
389 'another@email.address' => 0
390 };
391  
392 =cut
393  
394 0 ▶sub remove_image {
Subroutine remove_image: not called CC=2 SCAR=17.9
Pod documented
395 0 my $self = shift;
396 0 my %args = @_;
397
398 0 ▶ if (!defined $args{'addresses'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"addresses"}) 0 0 0
399 0 carp "Required parameter 'addresses' is not defined";
400 }
401
402 0 return $self->_call('removeImage', %args);
403 }
404  
405 =head2 delete_userimage
406  
407 Removes a userimage from the account and any email addresses with which it is
408 associated.
409  
410 $grav->delete_userimage(userimage => '292ed56ce849657d47b04105deadbeef');
411  
412 Parameters:
413  
414 =over 4
415  
416 =item * userimage
417  
418 B<(Required)> The userimage to be removed from the account.
419  
420 =back
421  
422 Returns: 1 on success, undef on failure.
423  
424 =cut
425  
426 0 ▶sub delete_userimage {
Subroutine delete_userimage: not called CC=2 SCAR=17.9
Pod documented
427 0 my $self = shift;
428 0 my %args = @_;
429
430 0 ▶ if (!defined $args{'userimage'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"userimage"}) 0 0 0
431 0 carp "Required parameter 'userimage' is not defined";
432 }
433
434 0 return $self->_call('deleteUserimage', %args);
435 }
436  
437 =head2 test
438  
439 API test method.
440  
441 $result = $grav->test(param => 1);
442  
443 Returns: A reference to a hash which represents the parameters passed to the
444 test method.
445  
446 =head2 err
447  
448 Returns the numeric code of last error.
449  
450 $err_code = $grav->err;
451  
452 =cut
453  
454 0 ▶sub err {
Subroutine err: not called CC=1 SCAR=6.9
Pod documented
455 0 my $self = shift;
456
457 0 return $self->{'err'};
458 }
459  
460 =head2 errstr
461  
462 Returns the human readable text for last error.
463  
464 $err_description = $grav->errstr;
465  
466 =cut
467  
468 0 ▶sub errstr {
Subroutine errstr: not called CC=1 SCAR=6.9
Pod documented
469 0 my $self = shift;
470
471 0 return $self->{'errstr'};
472 }
473  
474 =head1 AUTHOR
475  
476 Michal Wojciechowski, C<< <odyniec at cpan.org> >>
477  
478 =head1 BUGS
479  
480 Please report any bugs or feature requests to C<bug-webservice-gravatar at rt.cpan.org>, or through
481 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebService-Gravatar>. I will be notified, and then you'll
482 automatically be notified of progress on your bug as I make changes.
483  
484  
485 =head1 SUPPORT
486  
487 You can find documentation for this module with the perldoc command.
488  
489 perldoc WebService::Gravatar
490  
491  
492 You can also look for information at:
493  
494 =over 4
495  
496 =item * RT: CPAN's request tracker
497  
498 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WebService-Gravatar>
499  
500 =item * AnnoCPAN: Annotated CPAN documentation
501  
502 L<http://annocpan.org/dist/WebService-Gravatar>
503  
504 =item * CPAN Ratings
505  
506 L<http://cpanratings.perl.org/d/WebService-Gravatar>
507  
508 =item * Search CPAN
509  
510 L<http://search.cpan.org/dist/WebService-Gravatar>
511  
512 =back
513  
514  
515 =head1 COPYRIGHT & LICENSE
516  
517 Copyright 2010 Michal Wojciechowski, all rights reserved.
518  
519 This program is free software; you can redistribute it and/or modify it
520 under the same terms as Perl itself.
521  
522  
523 =head1 SEE ALSO
524  
525 =over 4
526  
527 =item * Gravatar XML-RPC API Documentation
528  
529 L<http://en.gravatar.com/site/implement/xmlrpc/>
530  
531 =back
532  
533 =cut
534  
535 2 1; # End of WebService::Gravatar
↑ Summary