File Coverage

blib/lib/Mojolicious/Plugin/Gravatar.pm
Criterion Covered Total %
statement 44 47 93.6
branch 5 10 50.0
condition 19 32 59.3
subroutine 8 8 100.0
pod 1 2 50.0
total 77 99 77.7


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Gravatar;
2              
3 1     1   1433 use warnings;
  1         2  
  1         47  
4 1     1   8 use strict;
  1         2  
  1         41  
5 1     1   16 use Mojo::Base 'Mojolicious::Plugin';
  1         3  
  1         8  
6 1     1   237 use Mojo::ByteStream 'b';
  1         2  
  1         879  
7              
8             our $VERSION = '0.04';
9              
10             sub register {
11 1     1 1 52 my ( $self, $app, $conf ) = @_;
12              
13             # Plugin config
14 1   50     5 $conf ||= {};
15              
16 1         6 $self->check_options(%$conf);
17 1   50     4 $conf->{'size'} ||= 80;
18 1   50     3 $conf->{'rating'} ||= 'PG';
19              
20             $app->helper( gravatar_url => sub {
21 4     4   7282 my $c = shift;
22 4         12 my ( $email, %options ) = @_;
23 4         13 $self->check_options(%options);
24              
25 4   66     17 my $default = $options{'default'} || $conf->{'default'};
26 4   66     14 my $size = $options{'size'} || $conf->{'size'};
27 4   66     14 my $rating = $options{'rating'} || $conf->{'rating'};
28 4   50     34 my $scheme = $options{'scheme'} || $conf->{scheme} || $c->req->url->to_abs->scheme || 'http';
29              
30 4         2424 my $url = $scheme . '://www.gravatar.com/avatar/';
31 4         24 $url .= b( lc $email )->md5_sum;
32 4         253 $url .= '?s=' . $size;
33 4 50       14 $url .= '&r=' . $rating if $rating;
34 4 50       14 $url .= '&d=' . b($default)->url_escape if $default;
35 4         168 return $url;
36 1         17 } );
37              
38             $app->helper( gravatar => sub {
39 2     2   5415 my $c = shift;
40 2         7 my ( $email, %options ) = @_;
41 2         9 $self->check_options(%options);
42              
43 2   66     12 my $size = $options{'size'} || $conf->{'size'};
44              
45 2         23 my $url = b($c->gravatar_url(@_))->xml_escape;
46              
47 2         59 return b "Gravatar";
48 1         126 } );
49             }
50              
51             sub check_options {
52 7     7 0 10 my ($self) = shift;
53 7         16 my %options = @_;
54              
55 7 50 33     50 if ( $options{size} && !( $options{size} >= 1 and $options{size} <= 512 ) ) {
      66        
56 0         0 die "Gravatar size must be 1 .. 512\n";
57             }
58              
59 7 50 66     32 if ( $options{rating} && $options{rating} !~ /^(?:g|pg|r|x)$/i ) {
60 0         0 die "Gravatar rating can only be g, pg, r, or x\n";
61             }
62              
63 7 50 66     28 if ( $options{scheme} && $options{scheme} !~ /^https?$/i ) {
64 0         0 die "Http scheme can only be http, https\n";
65             }
66              
67 7         14 return $self;
68             }
69              
70             =head1 NAME
71              
72             Mojolicious::Plugin::Gravatar - Globally Recognized Avatars for Mojolicious
73              
74             =head1 SYNOPSIS
75              
76             # Mojolicious
77             $self->plugin('gravatar');
78            
79             You can pass default size, rating, and default avatar url
80             $self->plugin('gravatar' => {
81             size => 60, #default was 80
82             rating => 'X', #default was PG
83             default => 'http://example.com/default.png' # default was not value
84             scheme => 'https' # if omitted will look in request's url scheme.
85             });
86              
87             # Mojolicious::Lite
88             plugin 'gravatar';
89              
90             # Gravatars in templates
91             <%= gravatar 'user@mail.com' %>
92             will generate
93             Gravatar
94              
95             <%= gravatar_url 'user@mail.com' %> - if you need only url
96            
97             Also you can overwrite any default config variables
98             <%= gravatar 'user@mail.com', size => 40, rating=> 'X' %>
99              
100            
101             If you need some styling for img tag:
102             <%= gravatar $email %>
103             and describe in css - ".gravatar img {border: 1px solid white;}"
104            
105              
106             =head1 DESCRIPTION
107              
108             This plugin adds gravatar ( L ) helpers to your application.
109              
110             =head1 CONFIG
111              
112             =head2 default (optional)
113              
114             The local (any valid absolute image URI) image to use if there is no Gravatar corresponding to the given email.
115              
116             =head2 size (optional)
117              
118             Gravatars are square. Size is 1 through 512 (pixels) and sets the width and the height.
119              
120             =head2 rating (optional)
121              
122             G|PG|R|X. The maximum rating of Gravatar you wish returned. If you have a family friendly forum, for example, you might set it to "G."
123              
124             =head2 scheme (optional)
125              
126             Gravatar URL scheme "http" or "https". If omitted will look in request's url scheme (if empty fill use "http").
127              
128             =head1 HELPERS
129              
130             =head2 gravatar $email [, %options ];
131              
132             generate img tag for getting avatar from gravatar.com
133              
134             $email (required) The key to using Gravatars is a hex hash of the user's email. This is generated automatically and sent to gravatar.com as the gravatar_id.
135              
136             %options (optional) - you can override config parameters . Support all parameters that you have in config
137            
138              
139             example Gravatar
140            
141             =head2 gravatar_url $email [, %options ];
142              
143             generate url for getting avatar from gravatar.com
144              
145             $email (required) The key to using Gravatars is a hex hash of the user's email. This is generated automatically and sent to gravatar.com as the gravatar_id.
146              
147             %options (optional) - you can override config parameters . Support all parameters that you have in config
148            
149             =head1 VERSION
150              
151             Version 0.02
152              
153             =head1 AUTHOR
154            
155             Viktor Turskyi
156              
157             =head1 CONTRIBUTORS
158            
159             Nils Diewald (Akron)
160              
161             =head1 BUGS
162              
163             Please report any bugs or feature requests to C, or through
164             the web interface at L. I will be notified, and then you'll
165             automatically be notified of progress on your bug as I make changes.
166              
167             Also you can report bugs to Github L
168              
169             =head1 SUPPORT
170              
171             You can find documentation for this module with the perldoc command.
172              
173             perldoc Mojolicious::Plugin::Gravatar
174              
175              
176             You can also look for information at:
177              
178             =over 4
179              
180             =item * RT: CPAN's request tracker
181              
182             L
183              
184             =item * AnnoCPAN: Annotated CPAN documentation
185              
186             L
187              
188             =item * CPAN Ratings
189              
190             L
191              
192             =item * Search CPAN
193              
194             L
195              
196             =back
197              
198              
199             =head1 ACKNOWLEDGEMENTS
200              
201              
202             =head1 LICENSE AND COPYRIGHT
203              
204             Copyright 2011 "koorchik".
205              
206             This program is free software; you can redistribute it and/or modify it
207             under the terms of either: the GNU General Public License as published
208             by the Free Software Foundation; or the Artistic License.
209              
210             See http://dev.perl.org/licenses/ for more information.
211              
212              
213             =cut
214              
215             1; # End of Mojolicious::Plugin::Gravatar