blib/lib/Mojolicious/Plugin/GoogleFontProxy.pm

filter: Statementstmt 100% 40 / 40 Branchbran 66.6% 4 / 6 Conditioncond 80.0% 8 / 10 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 91.4% 64 / 70 CC 8 SCAR 11.0
CC
8
Cov
93%
__ANON__
5.0
__ANON__
3.0
register
2.0
CRAP 3.0
↑ Summary
1 package Mojolicious::Plugin::GoogleFontProxy;
2  
3 # ABSTRACT: a small proxy that can be useful when you use Google fonts in your website
4  
5 3 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.03';
8  
9 3 our $CSS_URL_FORMAT = 'https://fonts.googleapis.com/css%s?family=%s';
10 3 our $FONT_URL_FORMAT = 'https://fonts.gstatic.com/s/%s';
11 3 our $USER_AGENT_STRING = '';
12  
13 3 ▶sub register {
Subroutine register: called CC=2 SCAR=6.9
Pod documented
14 3 my ($self, $app, $config) = @_;
15  
16 3 ▶ if ( !$config->{no_types} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"no_types"}) 3 0 3
17 3 $app->types->type( 'woff2' => 'font/woff2' );
18 3 $app->types->type( 'woff' => 'font/woff' );
19 3 $app->types->type( 'ttf' => 'font/ttf' );
20 3 $app->types->type( 'otf' => 'font/opentype' );
21 }
22  
23 $app->hook(
24 13 ▶ after_render => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
25 13 my ($c, $content, $format) = @_;
26  
27 13 ▶ return if !$format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 0 13 13
28 13 ▶ return if $format ne 'html' && $format ne 'css';
Branch: 1 branch
BranchTrueFalseTotal
if $format ne "html" and $format ne "css" 5 8 13
Condition100%3/3
$format ne "html" and $format ne "css"
!ll&&!rl&&r
535
MC/DC100%2/2
$format ne "html" and $format ne "css"
$format ne "html" $format ne "css"
Truth table100%3/3
A and B
A= $format ne "html"B= $format ne "css"
ABresult
0X0
100
111
29  
30 8 $$content =~ s{
31 https://fonts.googleapis.com/css
32 (?<version>[0-9]?)
33 \?family=(?<file>.*?)
34 (?<suffix>['"])
35 }{$c->url_for(
36 'google-proxy-css',
37 version => $+{version} || 0,
38 file => $+{file},
39 3 ▶ ) . $+{suffix};
Condition100%2/2
$+{'version'} || 0
l!l
12
MC/DC100%1/1
$+{'version'} || 0
$+{'version'}
Truth table100%2/2
A
A= $+{'version'}
Aresult
11
00
40 }xge;
41 }
42 3 );
43  
44 3 ▶ $app->routes->get( '/google/css/:version/*file' )->to( cb => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
45 3 my $c = shift;
46  
47 3 $c->render_later;
48  
49 3 ▶ my $version = $c->param('version') || '';
Condition100%2/2
$c->param('version') || ''
l!l
12
MC/DC100%1/1
$c->param('version') || ''
$c->param('version')
Truth table100%2/2
A
A= $c->param('version')
Aresult
11
00
50 3 my $url = sprintf $CSS_URL_FORMAT, $version, $c->param('file');
51 3 ▶ my $ua_string = $USER_AGENT_STRING || $c->tx->req->headers->user_agent;
Condition33%1/3
$Mojolicious::Plugin::GoogleFontProxy::USER_AGENT_STRING || $c->tx->req->headers->user_agent
l!l&&r!l&&!r
300
MC/DC0%0/2
$Mojolicious::Plugin::GoogleFontProxy::USER_AGENT_STRING || $c->tx->req->headers->user_agent
$Mojolicious::Plugin::GoogleFontProxy::USER_AGENT_STRING $c->tx->req->headers->user_agent
Truth table33%1/3
A || B
A= $Mojolicious::Plugin::GoogleFontProxy::USER_AGENT_STRINGB= $c->tx->req->headers->user_agent
ABresult
1X1
011
000
52  
53 3 ▶ $c->ua->get( $url => { 'User-Agent' => $ua_string } => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 3 my ($ua, $tx) = @_;
55  
56 3 my $body = $tx->res->body;
57 3 $body =~ s{
58 https?://fonts\.gstatic\.com/s/(.*?)\) \s* format\('(.*?)'\)
59 16 }{$c->url_for('google-proxy-font', file => $1, fformat => $2) . ") format('$2')"}xmseg;
60  
61 3 return $c->render( data => $body, format => 'css' );
62 3 });
63 3 })->name( 'google-proxy-css' );
64  
65 2 ▶ $app->routes->get( '/google/font/:fformat/*file' )->to( cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 2 my $c = shift;
67  
68 2 $c->render_later;
69  
70 2 my $url = sprintf $FONT_URL_FORMAT, $c->param('file');
71  
72 2 ▶ $c->ua->get( $url => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 2 my ($ua, $tx) = @_;
74  
75 2 my $body = $tx->res->body;
76  
77 2 return $c->render( data => $body, format => $c->param('fformat') );
78 2 });
79 3 })->name( 'google-proxy-font' );
80 }
81  
82 3 1;
83  
84 __END__
↑ Summary