blib/lib/Mojolicious/Plugin/Gravatar.pm

filter: Statementstmt 93.8% 46 / 49 Branchbran 50.0% 5 / 10 Conditioncond 63.1% 24 / 38 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 66.4% 85 / 128 CC 20 SCAR 20.5
CC
20
Cov
77%
check_options
12.3
__ANON__
9.7
register
4.1
CRAP 7.8
↑ Summary
1 package Mojolicious::Plugin::Gravatar;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mojo::ByteStream 'b';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.04';
9  
10 1 ▶sub register {
Subroutine register: called CC=4 SCAR=14.2
Pod documented
11 1 my ( $self, $app, $conf ) = @_;
12  
13 # Plugin config
14 1 ▶ $conf ||= {};
Condition50%1/2
$conf ||= {}
l!l
10
MC/DC0%0/1
$conf ||= {}
$conf
Truth table50%1/2
A
A= $conf
Aresult
11
00
15  
16 1 $self->check_options(%$conf);
17 1 ▶ $conf->{'size'} ||= 80;
Condition50%1/2
$conf->{"size"} ||= 80
l!l
10
MC/DC0%0/1
$conf->{"size"} ||= 80
$conf->{"size"}
Truth table50%1/2
A
A= $conf->{"size"}
Aresult
11
00
18 1 ▶ $conf->{'rating'} ||= 'PG';
Condition50%1/2
$conf->{"rating"} ||= "PG"
l!l
10
MC/DC0%0/1
$conf->{"rating"} ||= "PG"
$conf->{"rating"}
Truth table50%1/2
A
A= $conf->{"rating"}
Aresult
11
00
19  
20 4 ▶ $app->helper( gravatar_url => sub {
Subroutine __ANON__: called CC=9 SCAR=22.7
21 4 my $c = shift;
22 4 my ( $email, %options ) = @_;
23 4 $self->check_options(%options);
24  
25 4 ▶ my $default = $options{'default'} || $conf->{'default'};
Condition66%2/3
$options{'default'} || $conf->{'default'}
l!l&&r!l&&!r
130
MC/DC0%0/2
$options{'default'} || $conf->{'default'}
$options{'default'} $conf->{'default'}
Truth table66%2/3
A || B
A= $options{'default'}B= $conf->{'default'}
ABresult
1X1
011
000
26 4 ▶ my $size = $options{'size'} || $conf->{'size'};
Condition66%2/3
$options{'size'} || $conf->{'size'}
l!l&&r!l&&!r
220
MC/DC0%0/2
$options{'size'} || $conf->{'size'}
$options{'size'} $conf->{'size'}
Truth table66%2/3
A || B
A= $options{'size'}B= $conf->{'size'}
ABresult
1X1
011
000
27 4 ▶ my $rating = $options{'rating'} || $conf->{'rating'};
Condition66%2/3
$options{'rating'} || $conf->{'rating'}
l!l&&r!l&&!r
130
MC/DC0%0/2
$options{'rating'} || $conf->{'rating'}
$options{'rating'} $conf->{'rating'}
Truth table66%2/3
A || B
A= $options{'rating'}B= $conf->{'rating'}
ABresult
1X1
011
000
28 4 ▶ my $scheme = $options{'scheme'} || $conf->{scheme} || $c->req->url->to_abs->scheme || 'http';
Condition75%6/8
$options{'scheme'} || $conf->{'scheme'} || $c->req->url->to_abs->scheme || 'http'
l!l
13
$options{"scheme"} or $conf->{"scheme"} or $c->req->url->to_abs->scheme
l!l&&r!l&&!r
103
$options{"scheme"} or $conf->{"scheme"}
l!l&&r!l&&!r
103
MC/DC33%1/3
$options{'scheme'} || $conf->{'scheme'} || $c->req->url->to_abs->scheme || 'http'
$options{"scheme"} $conf->{"scheme"} $c->req->url->to_abs->scheme
Truth table50%2/4
A or B or C
A= $options{"scheme"}B= $conf->{"scheme"}C= $c->req->url->to_abs->scheme
ABCresult
1XX1
01X1
0011
0000
29  
30 4 my $url = $scheme . '://www.gravatar.com/avatar/';
31 4 $url .= b( lc $email )->md5_sum;
32 4 $url .= '?s=' . $size;
33 4 ▶ $url .= '&r=' . $rating if $rating;
Branch: 1 branch
BranchTrueFalseTotal
if $rating 4 0 4
34 4 ▶ $url .= '&d=' . b($default)->url_escape if $default;
Branch: 1 branch
BranchTrueFalseTotal
if $default 4 0 4
35 4 return $url;
36 1 } );
37  
38 2 ▶ $app->helper( gravatar => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
39 2 my $c = shift;
40 2 my ( $email, %options ) = @_;
41 2 $self->check_options(%options);
42  
43 2 ▶ my $size = $options{'size'} || $conf->{'size'};
Condition66%2/3
$options{'size'} || $conf->{'size'}
l!l&&r!l&&!r
110
MC/DC0%0/2
$options{'size'} || $conf->{'size'}
$options{'size'} $conf->{'size'}
Truth table66%2/3
A || B
A= $options{'size'}B= $conf->{'size'}
ABresult
1X1
011
000
44  
45 2 my $url = b($c->gravatar_url(@_))->xml_escape;
46  
47 2 return b "<img src='$url' alt='Gravatar' height='$size' width='$size' />";
48 1 } );
49 }
50  
51 7 ▶sub check_options {
Subroutine check_options: called CC=8 SCAR=25.1
Pod undocumented
52 7 my ($self) = shift;
53 7 my %options = @_;
54  
55 7 ▶ if ( $options{size} && !( $options{size} >= 1 and $options{size} <= 512 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"size"} and not $options{"size"} >= 1 && $options{"size"} <= 512) 0 7 7
Condition50%3/6
$options{"size"} and not $options{"size"} >= 1 && $options{"size"} <= 512
!ll&&!rl&&r
340
$options{'size'} >= 1 && $options{'size'} <= 512
!ll&&!rl&&r
004
MC/DC0%0/3
$options{"size"} and not $options{"size"} >= 1 && $options{"size"} <= 512
$options{"size"} $options{'size'} >= 1 $options{'size'} <= 512
Truth table50%2/4
A and not(B && C)
A= $options{"size"}B= $options{'size'} >= 1C= $options{'size'} <= 512
ABCresult
0XX0
1110
10X1
1101
56 0 die "Gravatar size must be 1 .. 512\n";
57 }
58  
59 7 ▶ if ( $options{rating} && $options{rating} !~ /^(?:g|pg|r|x)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"rating"} and not $options{"rating"} =~ /^(?:g|pg|r|x)$/iu) 0 7 7
Condition66%2/3
$options{"rating"} and not $options{"rating"} =~ /^(?:g|pg|r|x)$/iu
!ll&&!rl&&r
520
MC/DC0%0/2
$options{"rating"} and not $options{"rating"} =~ /^(?:g|pg|r|x)$/iu
$options{"rating"} not $options{"rating"} =~ /^(?:g|pg|r|x)$/iu
Truth table66%2/3
A and not(B)
A= $options{"rating"}B= not $options{"rating"} =~ /^(?:g|pg|r|x)$/iu
ABresult
0X0
100
111
60 0 die "Gravatar rating can only be g, pg, r, or x\n";
61 }
62  
63 7 ▶ if ( $options{scheme} && $options{scheme} !~ /^https?$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"scheme"} and not $options{"scheme"} =~ /^https?$/iu) 0 7 7
Condition66%2/3
$options{"scheme"} and not $options{"scheme"} =~ /^https?$/iu
!ll&&!rl&&r
610
MC/DC0%0/2
$options{"scheme"} and not $options{"scheme"} =~ /^https?$/iu
$options{"scheme"} not $options{"scheme"} =~ /^https?$/iu
Truth table66%2/3
A and not(B)
A= $options{"scheme"}B= not $options{"scheme"} =~ /^https?$/iu
ABresult
0X0
100
111
64 0 die "Http scheme can only be http, https\n";
65 }
66  
67 7 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 <img src="http://www.gravatar.com/avatar/6ad193f57f79ac444c3621370da955e9&amp;s=80&amp;r=PG" alt="Gravatar" height="80" width="80">
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 <span class='gravatar'> <%= gravatar $email %> </span>
103 and describe in css - ".gravatar img {border: 1px solid white;}"
104
105  
106 =head1 DESCRIPTION
107  
108 This plugin adds gravatar ( L<http://gravatar.com> ) 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 <img src="http://www.gravatar.com/avatar/6ad193f57f79ac444c3621370da955e9&amp;s=80&amp;r=PG" alt="Gravatar" height="80" width="80">
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 <koorchik@cpan.org>
156  
157 =head1 CONTRIBUTORS
158
159 Nils Diewald (Akron)
160  
161 =head1 BUGS
162  
163 Please report any bugs or feature requests to C<bug-mojolicious-plugin-gravatar at rt.cpan.org>, or through
164 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Plugin-Gravatar>. 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<https://github.com/koorchik/Mojolicious-Plugin-Gravatar/>
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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Plugin-Gravatar>
183  
184 =item * AnnoCPAN: Annotated CPAN documentation
185  
186 L<http://annocpan.org/dist/Mojolicious-Plugin-Gravatar>
187  
188 =item * CPAN Ratings
189  
190 L<http://cpanratings.perl.org/d/Mojolicious-Plugin-Gravatar>
191  
192 =item * Search CPAN
193  
194 L<http://search.cpan.org/dist/Mojolicious-Plugin-Gravatar/>
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 1; # End of Mojolicious::Plugin::Gravatar
↑ Summary