File Coverage

blib/lib/Gravatar/URL.pm
Criterion Covered Total %
statement 53 53 100.0
branch 33 34 97.0
condition 13 16 81.2
subroutine 9 9 100.0
pod 2 2 100.0
total 110 114 96.4


line stmt bran cond sub pod time code
1             package Gravatar::URL;
2              
3 6     6   69589 use strict;
  6         9  
  6         173  
4 6     6   25 use warnings;
  6         7  
  6         159  
5              
6 6     6   2939 use URI::Escape qw(uri_escape);
  6         7235  
  6         394  
7 6     6   35 use Digest::MD5 qw(md5_hex);
  6         9  
  6         245  
8 6     6   24 use Carp;
  6         8  
  6         403  
9              
10             our $VERSION = '1.07';
11              
12 6     6   2390 use parent 'Exporter';
  6         1510  
  6         30  
13             our @EXPORT = qw(
14             gravatar_id
15             gravatar_url
16             );
17              
18             my $Gravatar_Http_Base = "http://www.gravatar.com/avatar/";
19             my $Gravatar_Https_Base = "https://secure.gravatar.com/avatar/";
20              
21             =head1 NAME
22              
23             Gravatar::URL - Make URLs for Gravatars from an email address
24              
25             =head1 SYNOPSIS
26              
27             use Gravatar::URL;
28              
29             my $gravatar_id = gravatar_id($email);
30              
31             my $gravatar_url = gravatar_url(email => $email);
32              
33             =head1 DESCRIPTION
34              
35             A Gravatar is a Globally Recognized Avatar for a given email address.
36             This allows you to have a global picture associated with your email
37             address. You can look up the Gravatar for any email address by
38             constructing a URL to get the image from L. This module
39             does that.
40              
41             Examples of use include the author faces on L.
42              
43             See L for more info.
44              
45             =head1 Functions
46              
47             =head3 B
48              
49             # By email
50             my $url = gravatar_url( email => $email, %options );
51              
52             # By gravatar ID
53             my $url = gravatar_url( id => $id, %options );
54              
55             Constructs a URL to fetch the gravatar for a given C<$email> or C<$id>.
56              
57             C<$id> is a gravatar ID. See L for more information.
58              
59             C<%options> are optional and are...
60              
61             =head4 rating
62              
63             A user can rate how offensive the content of their gravatar is, like a
64             movie. The ratings are g, pg, r and x. If you specify a rating it is
65             the highest rating that will be given.
66              
67             rating => "r" # includes g, pg and r
68              
69             =head4 size
70              
71             Specifies the desired width and height of the gravatar (gravatars are square).
72              
73             Valid values are from 1 to 512 inclusive. Any size other than 80 may
74             cause the original gravatar image to be downsampled using bicubic
75             resampling before output.
76              
77             size => 40, # 40 x 40 image
78              
79             =head4 default
80              
81             The url to use if the user has no gravatar or has none that fits your rating requirements.
82              
83             default => "https://secure.wikimedia.org/wikipedia/en/wiki/File:Mad30.jpg"
84              
85             Relative URLs will be relative to the base (ie. gravatar.com), not your web site.
86              
87             Gravatar defines special values that you may use as a default to
88             produce dynamic default images. These are "identicon", "monsterid",
89             "wavatar" and "retro". "404" will cause the URL to return an HTTP 404 "Not Found"
90             error instead whereas "mm" will display the same "mystery man" image for all
91             missing people. See L for
92             more info.
93              
94             If omitted, Gravatar will serve up their default image, the blue G.
95              
96             =head4 border
97              
98             B This key has been removed from the Gravatar protocol.
99             It will be removed from future versions of Gravatar::URL.
100              
101             Gravatars can be requested to have a 1 pixel colored border. If you'd
102             like that, pass in the color to border as a 3 or 6 digit hex string.
103              
104             border => "000000", # a black border, like my soul
105             border => "000", # black, but in 3 digits
106              
107             =head4 base
108              
109             This is the URL of the location of the Gravatar server you wish to
110             grab Gravatars from. Defaults to
111             L for HTTP and
112             L for HTTPS.
113              
114             =head4 short_keys
115              
116             If true, use short key names when constructing the URL. "s" instead
117             of "size", "r" instead of "ratings" and so on.
118              
119             short_keys defaults to true.
120              
121             =head4 https
122              
123             If true, serve avatars over HTTPS instead of HTTP.
124              
125             You should select this option if your site is served over HTTPS to
126             avoid browser warnings about the presence of insecure content.
127              
128             https defaults to false.
129              
130             =cut
131              
132             my %defaults = (
133             short_keys => 1,
134             base_http => $Gravatar_Http_Base,
135             base_https => $Gravatar_Https_Base,
136             https => 0,
137             );
138              
139             sub gravatar_url {
140 48     48 1 8707 my %args = @_;
141              
142             exists $args{id} or exists $args{email} or
143 48 100 66     283 croak "Cannot generate a Gravatar URI without an email address or gravatar id";
144              
145             exists $args{id} xor exists $args{email} or
146 47 100 75     293 croak "Both an id and an email were given. gravatar_url() only takes one";
147              
148 46         75 _apply_defaults(\%args, \%defaults);
149              
150 46 100       84 if ( exists $args{size} ) {
151 15 100 100     348 $args{size} >= 1 and $args{size} <= 512
152             or croak "Gravatar size must be 1 .. 512";
153             }
154              
155 44 100       65 if ( exists $args{rating} ) {
156 13 100       150 $args{rating} =~ /\A(?:g|pg|r|x)\Z/i
157             or croak "Gravatar rating can only be g, pg, r, or x";
158 12         20 $args{rating} = lc $args{rating};
159             }
160              
161 43 100       65 if ( exists $args{border} ) {
162 1         27 carp "The border key is deprecated";
163 1 50       416 $args{border} =~ /\A[0-9A-F]{3}(?:[0-9A-F]{3})?\Z/
164             or croak "Border must be a 3 or 6 digit hex number in caps";
165             }
166            
167 43   66     130 $args{gravatar_id} = $args{id} || gravatar_id($args{email});
168              
169             $args{default} = uri_escape($args{default})
170 43 100       108 if $args{default};
171              
172             # Use a fixed order to make testing easier
173 43         409 my @pairs;
174 43         53 for my $arg ( qw( rating size default border ) ) {
175 172 100       244 next unless exists $args{$arg};
176              
177 50         41 my $key = $arg;
178 50 100       90 $key = substr($key, 0, 1) if $args{short_keys};
179 50         89 push @pairs, join("=", $key, $args{$arg});
180             }
181              
182 43         35 my $uri = $args{base};
183 43 100       126 $uri .= "/" unless $uri =~ m{/$};
184 43         63 $uri .= $args{gravatar_id};
185 43 100       88 $uri .= "?".join("&",@pairs) if @pairs;
186              
187 43         219 return $uri;
188             }
189              
190              
191             sub _apply_defaults {
192 67     67   71 my($hash, $defaults) = @_;
193              
194 67         150 for my $key (keys %$defaults) {
195 245 100 100     643 next if 'base_http' eq $key or 'base_https' eq $key;
196 115 100       197 next if exists $hash->{$key};
197 84         98 $hash->{$key} = $defaults->{$key};
198             }
199              
200 67 100       119 if (not exists $hash->{'base'}) {
201 37 100       85 $hash->{'base'} = $hash->{'https'} ? $defaults->{base_https} : $defaults->{base_http};
202             }
203              
204 67         70 return;
205             }
206              
207             =head3 B
208              
209             my $id = gravatar_id($email);
210              
211             Converts an C<$email> address into its Gravatar C<$id>.
212              
213             =cut
214              
215             sub gravatar_id {
216 56     56 1 1137 my $email = shift;
217 56         332 return md5_hex(lc $email);
218             }
219              
220              
221             =head1 THANKS
222              
223             Thanks to L for coming up with the whole idea and Ashley
224             Pond V from whose L I took most of the
225             original code.
226              
227              
228             =head1 LICENSE
229              
230             Copyright 2007 - 2009, Michael G Schwern .
231             Copyright 2011, Francois Marier .
232              
233             This program is free software; you can redistribute it and/or
234             modify it under the same terms as Perl itself.
235              
236             See F
237              
238              
239             =head1 SEE ALSO
240              
241             L - a Gravatar plugin for Template Toolkit
242              
243             L - The Gravatar web site
244              
245             L - The Gravatar URL implementor's guide
246              
247             =cut
248              
249              
250             1;