lib/Mojolicious/Plugin/ShareHelpers.pm

filter: Statementstmt 98.2% 113 / 115 Branchbran 90.3% 56 / 62 Conditioncond 66.6% 12 / 18 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 16 / 16 Podpod 20.0% 1 / 5 totaltotal 89.3% 201 / 225 CC 42 SCAR 21.8
CC
42
Cov
93%
share_button
15.0
share_meta
10.1
share_url
9.3
CRAP 8.8
↑ Summary
1 package Mojolicious::Plugin::ShareHelpers;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Mojo::ByteStream 'b';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.7';
10  
11 1 our $APP; # for app instance
12  
13 1 ▶has url => sub { +{
Subroutine __ANON__: called CC=1 SCAR=0.0
14 1 'twitter' => 'http://twitter.com/share',
15 'facebook' => 'http://facebook.com/sharer.php',
16 'vkontakte' => 'http://vk.com/share.php',
17 'mymailru' => 'http://connect.mail.ru/share',
18 'google+' => 'http://plus.google.com',
19 1 } };
20  
21 1 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
22 1 my($self, $app) = @_;
23  
24 1 $APP = $app;
25  
26 1 ▶ $app->helper( share_url => sub { $self->share_url ( @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
27 1 ▶ $app->helper( share_button => sub { $self->share_button ( @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 ▶ $app->helper( share_meta => sub { $self->share_meta ( @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
29 1 ▶ $app->helper( is_share_agent => sub { $self->is_share_agent( @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
30 }
31  
32 16 ▶sub share_url {
Subroutine share_url: called CC=9 SCAR=22.3
Pod undocumented
33 16 my($self, $c) = (shift, shift);
34  
35 16 my $type = shift;
36 16 ▶ return '' unless $self->_check_type( $type );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_type($type) 2 14 16
37  
38 14 my %args = @_;
39  
40 14 my $param;
41 14 ▶ if ($type eq 'twitter') {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'twitter') { } 4 10 14
elsif ($type eq 'facebook') { } 2 8 10
elsif ($type eq 'vkontakte') { } 2 6 8
elsif ($type eq 'mymailru') { } 6 0 6
elsif ($type eq 'google+') { } 0 0 0
42 4 $param->{$_} = $args{$_} for qw(url via text related count lang counturl);
43 }
44 elsif ($type eq 'facebook') {
45 2 $param->{u} = $args{url };
46 2 $param->{t} = $args{text};
47 }
48 elsif ($type eq 'vkontakte') {
49 2 $param->{url} = $args{url};
50 }
51 elsif ($type eq 'mymailru') {
52 6 $param->{share_url} = $args{url};
53 }
54 elsif ($type eq 'google+') {
55 0 $APP->log->error("Google Plus doen't have share URL, use share_button");
56 0 return '';
57 }
58  
59 14 my @p = grep { $param->{$_} } sort keys %$param;
60 return join '?', $self->url->{ $type },
61 14 ▶ @p ? join '&', map { $_ . '=' . b( $param->{$_} )->url_escape } @p : ()
Branch: 1 branch
BranchTrueFalseTotal
@p ? : 12 2 14
62 ;
63 }
64  
65 27 ▶sub share_button {
Subroutine share_button: called CC=15 SCAR=27.1
Pod undocumented
66 27 my($self, $c) = (shift, shift);
67  
68 27 my $type = shift;
69 27 ▶ return '' unless $self->_check_type( $type );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_type($type) 2 25 27
70  
71 25 my %args = @_;
72  
73 25 my $button;
74 25 ▶ if ($type eq 'twitter') {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'twitter') { } 4 21 25
elsif ($type eq 'facebook') { } 4 17 21
elsif ($type eq 'vkontakte') { } 4 13 17
elsif ($type eq 'mymailru') { } 4 9 13
elsif ($type eq 'google+') { } 9 0 9
75 4 ▶ if ($args{iframe}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'iframe'}) { } 2 2 4
76 2 my $url = $c->share_url( $type, @_ );
77 2 my($param) = $url =~ /.*\?(.*)/;
78  
79 2 $button =
80 qq(<iframe allowtransparency="true" frameborder="0" scrolling="no" style="width:130px; height:50px;" ) .
81 qq(src="http://platform.twitter.com/widgets/tweet_button.html?$param"></iframe>)
82 ;
83 }
84 else {
85 2 my $attr; push @$attr, qq(data-$_="$args{$_}")
86 2 for grep { $args{$_} } qw(url via text related count lang counturl);
87 2 my $param = join ' ', @$attr;
88  
89 2 $button =
90 qq(<a href="http://twitter.com/share" class="twitter-share-button" $param>Tweet</a>) .
91 qq(<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>)
92 ;
93 }
94 }
95 elsif ($type eq 'facebook') {
96 4 ▶ if ($args{fb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'fb'}) { } 2 2 4
97 2 my $attr = { type => $args{type}, href => $args{url}, class => $args{class} };
98 2 my $param = join ' ', map { qq($_="$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr;
99  
100 2 $button =
101 qq(<fb:share-button $param></fb:share-button>)
102 ;
103 }
104 else {
105 2 my $attr = { type => $args{type}, share_url => $args{url} };
106 2 my $param = join ' ', map { qq($_="$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr;
107  
108 2 $button =
109 qq(<a name="fb_share" $param>$args{title}</a>) .
110 qq(<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>)
111 ;
112 }
113 }
114 elsif ($type eq 'vkontakte') {
115 4 ▶ my $url = $args{url} ? qq({url: "$args{url}"}) : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$args{'url'} ? : 2 2 4
116 4 my $attr = { type => $args{type}, text => $args{title} };
117 4 my $param = join ', ', map { qq($_: "$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr;
118  
119 4 $button =
120 qq(<script type="text/javascript" src="http://vk.com/js/api/share.js?146" charset="windows-1251"></script>) .
121 qq(<script type="text/javascript">document.write(VK.Share.button($url, {$param}));</script>)
122 ;
123 }
124 elsif ($type eq 'mymailru') {
125 1 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 4 my $url = $c->share_url( $type, @_ );
127  
128 4 ▶ $args{type } ||= '';
Condition50%1/2
$args{"type"} ||= ""
l!l
40
MC/DC0%0/1
$args{"type"} ||= ""
$args{"type"}
Truth table50%1/2
A
A= $args{"type"}
Aresult
11
00
129 4 ▶ $args{title} ||= 'В Мой Мир';
Condition50%1/2
$args{"title"} ||= "\x{412} \x{41c}\x{43e}\x{439} \x{41c}\x{438}\x{440}"
l!l
40
MC/DC0%0/1
$args{"title"} ||= "\x{412} \x{41c}\x{43e}\x{439} \x{41c}\x{438}\x{440}"
$args{"title"}
Truth table50%1/2
A
A= $args{"title"}
Aresult
11
00
130  
131 4 $button =
132 qq(<script src="http://cdn.connect.mail.ru/js/share/2/share.js" type="text/javascript"></script>) .
133 qq(<a class="mrc__share" type="$args{type}" href="$url">$args{title}</a>)
134 ;
135 }
136 elsif ($type eq 'google+') {
137 9 my $attr = { size => $args{size}, href => $args{url}, count => $args{count}, callback => $args{callback} };
138 9 my $param = join ' ', 'class="g-plusone"', map { qq(data-$_="$attr->{$_}") } grep { $attr->{$_} } sort keys %$attr;
139  
140 9 my $script = join ', ', map { qq($_: "$args{$_}") } grep { $args{$_} } qw(lang parsetags);
141  
142 $button =
143 (
144 $args{noscript}
145 9 ▶ ? ''
Branch: 2 branches
BranchTrueFalseTotal
$args{'noscript'} ? : 5 4 9
$script ? : 3 1 4
146 : qq(<script type="text/javascript" src="https://apis.google.com/js/plusone.js">) . ( $script ? "{$script}" : '' ) . qq(</script>\n)
147 ) .
148 qq(<div $param></div>)
149 ;
150 }
151  
152 25 return $button;
153 }
154  
155 6 ▶sub share_meta {
Subroutine share_meta: called CC=10 SCAR=23.1
Pod undocumented
156 6 my($self, $c) = (shift, shift);
157 6 my %args = @_;
158  
159 6 $_ = b($_)->xml_escape->to_string for grep {$_} @args{qw(title description)};
160  
161 return join "\n",
162 @_ ? qq(<meta name="medium" content="mult"/>) : '',
163  
164 $args{og} ? (
165 $args{fb_app_id} ? qq(<meta property="fb:app_id" content="$args{fb_app_id}"/>) : (),
166 qq(<meta property="og:site_name" content="$args{site_name}" />),
167 qq(<meta property="og:type" content="website" />),
168 6 ▶ map { $args{$_} ? qq(<meta property="og:$_" content="$args{$_}"/>) : () }
Branch: 5 branches
BranchTrueFalseTotal
$args{$_} ? : 6 0 6
$args{'title'} ? : 4 2 6
$args{'description'} ? : 4 2 6
$args{'image'} ? : 4 2 6
$args{'url'} ? : 4 2 6
169 qw(image title description)
170 ) : (),
171  
172 $args{title} ? qq(<meta name="title" content="$args{title}"/>) : (),
173 $args{description} ? qq(<meta name="description" content="$args{description}"/>) : (),
174 $args{image} ? qq(<link rel="image_src" href="$args{image}" />) : (),
175 $args{url} ? (
176 6 ▶ qq(<link rel="target_url" href="$args{url}"/>),
Branch: 3 branches
BranchTrueFalseTotal
@_ ? : 4 2 6
$args{'og'} ? : 2 4 6
$args{'fb_app_id'} ? : 2 0 2
177 qq(<link rel="canonical" href="$args{url}"/>),
178 ) : (),
179 ;
180 }
181  
182 3 ▶sub is_share_agent {
Subroutine is_share_agent: called CC=8 SCAR=21.1
Pod undocumented
183 3 my($self, $c) = (shift, shift);
184  
185 3 my $ua = $c->req->headers->user_agent;
186 3 my $range = $c->req->headers->header('Range');
187 3 my $enc = $c->req->headers->header('Accept-Encoding');
188  
189 3 ▶ my $agent =
Branch: 2 branches
BranchTrueFalseTotal
$ua =~ /facebookexternalhit/u && $range && $enc eq 'gzip' ? : 1 2 3
$ua =~ /Mozilla/u && $range && $enc =~ /gzip/u ? : 1 1 2
Condition66%8/12
$ua =~ /facebookexternalhit/u and $range and $enc eq "gzip"
!ll&&!rl&&r
201
$ua =~ /facebookexternalhit/u and $range
!ll&&!rl&&r
201
$ua =~ /Mozilla/u and $range and $enc =~ /gzip/u
!ll&&!rl&&r
101
$ua =~ /Mozilla/u and $range
!ll&&!rl&&r
101
MC/DC33%1/3
$ua =~ /facebookexternalhit/u and $range and $enc eq "gzip"
$ua =~ /facebookexternalhit/u $range $enc eq "gzip"
MC/DC33%1/3
$ua =~ /Mozilla/u and $range and $enc =~ /gzip/u
$ua =~ /Mozilla/u $range $enc =~ /gzip/u
Truth table50%2/4
A and B and C
A= $ua =~ /facebookexternalhit/uB= $rangeC= $enc eq "gzip"
ABCresult
0XX0
10X0
1100
1111
Truth table50%2/4
A and B and C
A= $ua =~ /Mozilla/uB= $rangeC= $enc =~ /gzip/u
ABCresult
0XX0
10X0
1100
1111
190 $ua =~ /facebookexternalhit/ && $range && $enc eq 'gzip' ? 'facebook' :
191 $ua =~ /Mozilla/ && $range && $enc =~ /gzip/ ? 'vkontakte' : # XXX: add cp1251
192 ''
193 ;
194  
195 3 ▶ $APP->log->debug(qq(Found the share agent "$agent")) if $agent;
Branch: 1 branch
BranchTrueFalseTotal
if $agent 2 1 3
196  
197 3 return $agent;
198 }
199  
200 43 ▶sub _check_type {
Subroutine _check_type: called CC=4 SCAR=13.9
201 43 my $self = shift;
202 43 ▶ my $type = shift || '';
Condition100%2/2
shift() || ''
l!l
412
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
203  
204 43 ▶ if (!$type) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 2 41 43
elsif (not exists $self->url->{$type}) { } 2 39 41
205 2 $APP->log->debug('Missed the share type');
206 2 return;
207 }
208 elsif (! exists $self->url->{ $type }) {
209 2 my $types = join ', ', sort keys %{$self->url};
210 2 $APP->log->debug(qq(Bad share type "$type", support types of share: $types));
211 2 return;
212 }
213 else {
214 39 return $type;
215 }
216 }
217  
218 1 1;
219  
220 __END__
↑ Summary