File Coverage

blib/lib/Gravatar/URL.pm
Criterion Covered Total %
statement 53 53 100.0
branch 33 34 97.0
condition 14 16 87.5
subroutine 9 9 100.0
pod 2 2 100.0
total 111 114 97.3


line stmt bran cond sub pod time code
1             package Gravatar::URL;
2              
3 6     6   139842 use strict;
  6         16  
  6         212  
4 6     6   58 use warnings;
  6         13  
  6         178  
5              
6 6     6   5226 use URI::Escape qw(uri_escape);
  6         9924  
  6         506  
7 6     6   46 use Digest::MD5 qw(md5_hex);
  6         19  
  6         419  
8 6     6   35 use Carp;
  6         12  
  6         533  
9              
10             our $VERSION = '1.06';
11              
12 6     6   5569 use parent 'Exporter';
  6         1969  
  6         35  
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 14637 my %args = @_;
141              
142 48 100 100     486 exists $args{id} or exists $args{email} or
143             croak "Cannot generate a Gravatar URI without an email address or gravatar id";
144              
145 47 100 75     434 exists $args{id} xor exists $args{email} or
146             croak "Both an id and an email were given. gravatar_url() only takes one";
147              
148 46         141 _apply_defaults(\%args, \%defaults);
149              
150 46 100       137 if ( exists $args{size} ) {
151 15 100 100     411 $args{size} >= 1 and $args{size} <= 512
152             or croak "Gravatar size must be 1 .. 512";
153             }
154              
155 44 100       124 if ( exists $args{rating} ) {
156 13 100       252 $args{rating} =~ /\A(?:g|pg|r|x)\Z/i
157             or croak "Gravatar rating can only be g, pg, r, or x";
158 12         31 $args{rating} = lc $args{rating};
159             }
160              
161 43 100       102 if ( exists $args{border} ) {
162 1         25 carp "The border key is deprecated";
163 1 50       682 $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     272 $args{gravatar_id} = $args{id} || gravatar_id($args{email});
168              
169 43 100       176 $args{default} = uri_escape($args{default})
170             if $args{default};
171              
172             # Use a fixed order to make testing easier
173 43         584 my @pairs;
174 43         88 for my $arg ( qw( rating size default border ) ) {
175 172 100       445 next unless exists $args{$arg};
176              
177 50         71 my $key = $arg;
178 50 100       151 $key = substr($key, 0, 1) if $args{short_keys};
179 50         155 push @pairs, join("=", $key, $args{$arg});
180             }
181              
182 43         88 my $uri = $args{base};
183 43 100       168 $uri .= "/" unless $uri =~ m{/$};
184 43         107 $uri .= $args{gravatar_id};
185 43 100       139 $uri .= "?".join("&",@pairs) if @pairs;
186              
187 43         402 return $uri;
188             }
189              
190              
191             sub _apply_defaults {
192 67     67   101 my($hash, $defaults) = @_;
193              
194 67         218 for my $key (keys %$defaults) {
195 245 100 100     1343 next if 'base_http' eq $key or 'base_https' eq $key;
196 115 100       300 next if exists $hash->{$key};
197 84         194 $hash->{$key} = $defaults->{$key};
198             }
199              
200 67 100       215 if (not exists $hash->{'base'}) {
201 37 100       129 $hash->{'base'} = $hash->{'https'} ? $defaults->{base_https} : $defaults->{base_http};
202             }
203              
204 67         149 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 1694 my $email = shift;
217 56         510 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;