lib/Plack/Middleware/Assets/RailsLike.pm

filter: Statementstmt 97.1% 134 / 138 Branchbran 92.1% 35 / 38 Conditioncond 76.0% 19 / 25 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 96.1% 25 / 26 Podpod 100% 2 / 2 totaltotal 92.1% 224 / 243 CC 31 SCAR 16.1
CC
31
Cov
94%
prepare_app
9.0
_build_content
9.0
call
7.0
CRAP 5.0
« script/assets-railslike-precompiler.pl ↑ Summary lib/Plack/Middleware/Assets/RailsLike/Compiler.pm »
1 package Plack::Middleware::Assets::RailsLike;
2  
3 9 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Cache::NullCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Cache::MemoryCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Digest::SHA1 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use File::Spec::Functions qw(catdir catfile canonpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Plack::Util::Accessor qw(path root search_path expires cache minify);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Plack::Middleware::Assets::RailsLike::Compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 our $VERSION = "0.13";
20  
21 9 our $EXPIRES_NEVER = $Cache::Cache::EXPIRES_NEVER;
22 9 our $EXPIRES_NOW = $Cache::Cache::EXPIRES_NOW;
23  
24 # copy from Cache::BaseCache
25 9 my %_expiration_units = (
26 map( ( $_, 1 ), qw(s second seconds sec) ),
27 map( ( $_, 60 ), qw(m minute minutes min) ),
28 map( ( $_, 60 * 60 ), qw(h hour hours) ),
29 map( ( $_, 60 * 60 * 24 ), qw(d day days) ),
30 map( ( $_, 60 * 60 * 24 * 7 ), qw(w week weeks) ),
31 map( ( $_, 60 * 60 * 24 * 30 ), qw(M month months) ),
32 map( ( $_, 60 * 60 * 24 * 365 ), qw(y year years) )
33 );
34  
35 8 ▶sub prepare_app {
Subroutine prepare_app: called CC=9 SCAR=22.0
Pod documented
36 8 my $self = shift;
37  
38 # Set default values for options
39 8 ▶ $self->{path} ||= qr{^/assets};
Condition50%1/2
$self->{"path"} ||= qr"^/assets"
l!l
08
MC/DC0%0/1
$self->{"path"} ||= qr"^/assets"
$self->{"path"}
Truth table50%1/2
A
A= $self->{"path"}
Aresult
11
00
40 8 ▶ $self->{root} ||= '.';
Condition100%2/2
$self->{"root"} ||= "."
l!l
71
MC/DC100%1/1
$self->{"root"} ||= "."
$self->{"root"}
Truth table100%2/2
A
A= $self->{"root"}
Aresult
11
00
41 8 ▶ $self->{search_path} ||= [ catdir( $self->{root}, 'assets' ) ];
Condition100%2/2
$self->{"search_path"} ||= [catdir($self->{"root"}, "assets")]
l!l
17
MC/DC100%1/1
$self->{"search_path"} ||= [catdir($self->{"root"}, "assets")]
$self->{"search_path"}
Truth table100%2/2
A
A= $self->{"search_path"}
Aresult
11
00
42 8 ▶ $self->{expires} ||= '3 days';
Condition100%2/2
$self->{"expires"} ||= "3 days"
l!l
35
MC/DC100%1/1
$self->{"expires"} ||= "3 days"
$self->{"expires"}
Truth table100%2/2
A
A= $self->{"expires"}
Aresult
11
00
43  
44 8 ▶ if ( $self->{cache} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'cache'}) { } 1 7 8
elsif ($ENV{'PLACK_ENV'} and $ENV{'PLACK_ENV'} eq 'development') { } 1 6 7
45 1 $self->{_max_age} = $self->_max_age;
46 }
47 ▶ elsif ( $ENV{PLACK_ENV} and $ENV{PLACK_ENV} eq 'development' ) {
Condition66%2/3
$ENV{"PLACK_ENV"} and $ENV{"PLACK_ENV"} eq "development"
!ll&&!rl&&r
601
MC/DC50%1/2
$ENV{"PLACK_ENV"} and $ENV{"PLACK_ENV"} eq "development"
$ENV{"PLACK_ENV"} $ENV{"PLACK_ENV"} eq "development"
Truth table66%2/3
A and B
A= $ENV{"PLACK_ENV"}B= $ENV{"PLACK_ENV"} eq "development"
ABresult
0X0
100
111
48  
49 # disable cache
50 1 $self->{cache} = Cache::NullCache->new;
51 1 $self->{_max_age} = 0;
52 }
53 else {
54 $self->{cache} = Cache::MemoryCache->new(
55 {
56 namespace => __PACKAGE__,
57 default_expires_in => $self->{expires},
58 6 auto_purge_interval => '1 day',
59 auto_purge_on_set => 1,
60 auto_purge_on_get => 1
61 }
62 );
63 6 $self->{_max_age} = $self->_max_age;
64 }
65  
66 8 ▶ $self->{minify} //= 1;
Condition100%2/2
$self->{"minify"} //= 1
l!l
53
MC/DC100%1/1
$self->{"minify"} //= 1
$self->{"minify"}
Truth table100%2/2
A
A= $self->{"minify"}
Aresult
11
00
67  
68 $self->{_compiler} = Plack::Middleware::Assets::RailsLike::Compiler->new(
69 minify => $self->{minify},
70 search_path => $self->{search_path},
71 8 );
72 }
73  
74 17 ▶sub call {
Subroutine call: called CC=7 SCAR=19.5
Pod documented
75 17 my ( $self, $env ) = @_;
76  
77 17 my $path_info = $env->{PATH_INFO};
78 17 ▶ if ( $path_info =~ $self->path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_info =~ $self->path) { } 16 1 17
79 16 my $real_path = canonpath( catfile( $self->root, $path_info ) );
80 16 my ( $filename, $dirs, $suffix )
81 = fileparse( $real_path, qr/\.[^.]*/ );
82 16 ▶ my $type = $suffix eq '.js' ? 'js' : 'css';
Branch: 1 branch
BranchTrueFalseTotal
$suffix eq '.js' ? : 12 4 16
83  
84 16 my $content;
85 {
86 16 local $@;
87 16 eval {
88 16 $content
89 = $self->_build_content( $real_path, $filename, $dirs,
90 $suffix, $type );
91 };
92 16 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 15 16
93 1 warn $@;
94 1 return $self->_500;
95 }
96 }
97 15 ▶ return $self->_404 unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 1 14 15
98  
99 14 my $etag = Digest::SHA1::sha1_hex($content);
100 14 ▶ if ( $env->{'HTTP_IF_NONE_MATCH'} || '' eq $etag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{'HTTP_IF_NONE_MATCH'} or '' eq $etag) { } 1 13 14
Condition66%2/3
$env->{"HTTP_IF_NONE_MATCH"} or "" eq $etag
l!l&&r!l&&!r
1013
MC/DC50%1/2
$env->{"HTTP_IF_NONE_MATCH"} or "" eq $etag
$env->{"HTTP_IF_NONE_MATCH"} "" eq $etag
Truth table66%2/3
A or B
A= $env->{"HTTP_IF_NONE_MATCH"}B= "" eq $etag
ABresult
1X1
011
000
101 1 return $self->_304;
102 }
103 else {
104 13 return $self->_build_response( $content, $type, $etag );
105 }
106 }
107 else {
108 1 return $self->app->($env);
109 }
110 }
111  
112 16 ▶sub _build_content {
Subroutine _build_content: called CC=9 SCAR=22.0
113 16 my $self = shift;
114 16 my ( $real_path, $filename, $dirs, $suffix, $type ) = @_;
115 16 my ( $base, $version ) = $filename =~ /^(.+)-([^\-]+)$/;
116  
117 16 my $content = $self->cache->get($real_path);
118 16 ▶ return $content if $content;
Branch: 1 branch
BranchTrueFalseTotal
if $content 1 15 16
119  
120 15 my ( @list, $pre_compiled );
121 15 ▶ if ($version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version) { } 6 9 15
122 6 @list = ( $real_path, catfile( $dirs, "$base$suffix" ) );
123 6 $pre_compiled = 1;
124 }
125 else {
126 9 @list = ($real_path);
127 9 $pre_compiled = 0;
128 }
129  
130 15 for my $file (@list) {
131 19 my $manifest;
132 19 ▶ read_file( $file, buf_ref => \$manifest, err_mode => sub { } );
Subroutine __ANON__: not called CC=1 SCAR=0.0
133 19 ▶ if ( $! && $! == Errno::ENOENT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! and $! == 2) { } 5 14 19
elsif ($!) { } 1 13 14
Condition100%3/3
$! and $! == 2
!ll&&!rl&&r
1315
MC/DC100%2/2
$! and $! == 2
$! $! == 2
Truth table100%3/3
A and B
A= $!B= $! == 2
ABresult
0X0
100
111
134 5 $pre_compiled = 0;
135 5 next;
136 }
137 elsif ($!) {
138 1 die "read_file '$file' failed - $!";
139 }
140  
141 13 ▶ if ($pre_compiled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_compiled) { } 1 12 13
142 1 $content = $manifest;
143 }
144 else {
145 $content = $self->{_compiler}->compile(
146 12 manifest => $manifest,
147 type => $type
148 );
149 }
150  
151 # filename with versioning as a key
152 $self->cache->set( $real_path, $content, $self->{_max_age} )
153 13 ▶ if $self->{_max_age} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_max_age"} > 0 10 3 13
154 13 last;
155 }
156 14 return $content;
157 }
158  
159 13 ▶sub _build_response {
Subroutine _build_response: called CC=3 SCAR=11.0
160 13 my $self = shift;
161 13 my ( $content, $type, $etag ) = @_;
162  
163 # build headers
164 13 ▶ my $content_type = $type eq 'js' ? 'application/javascript' : 'text/css';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'js' ? : 9 4 13
165 13 my $max_age = $self->{_max_age};
166 13 my $expires = time + $max_age;
167  
168 13 ▶ if ( $max_age > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_age > 0) { } 10 3 13
169 return [
170 10 200,
171 [
172 'Content-Type' => $content_type,
173 'Content-Length' => length($content),
174 'Cache-Control' => sprintf( 'max-age=%d', $max_age ),
175 'Expires' => HTTP::Date::time2str($expires),
176 'Etag' => $etag,
177 ],
178 [$content]
179 ];
180 }
181 else {
182 return [
183 3 200,
184 [
185 'Content-Type' => $content_type,
186 'Content-Length' => length($content),
187 'Cache-Control' => 'no-store',
188 ],
189 [$content]
190 ];
191 }
192 }
193  
194 7 ▶sub _max_age {
Subroutine _max_age: called CC=3 SCAR=12.3
195 7 my $self = shift;
196 7 my $max_age = 0;
197 7 ▶ if ( $self->expires eq $EXPIRES_NEVER ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->expires eq $Plack::Middleware::Assets::RailsLike::EXPIRES_NEVER) { } 0 7 7
elsif ($self->expires eq $Plack::Middleware::Assets::RailsLike::EXPIRES_NOW) { } 0 7 7
198  
199 # See http://www.w3.org/Protocols/rfc2616/rfc2616.txt 14.21 Expires
200 0 $max_age = $_expiration_units{'year'};
201 }
202 elsif ( $self->expires eq $EXPIRES_NOW ) {
203 0 $max_age = 0;
204 }
205 else {
206 7 $max_age = $self->_expires_in_seconds;
207 }
208 7 return $max_age;
209 }
210  
211 7 ▶sub _expires_in_seconds {
Subroutine _expires_in_seconds: called CC=5 SCAR=17.8
212 7 my $self = shift;
213 7 my $expires = $self->expires;
214  
215 7 my ( $n, $unit ) = $expires =~ /^\s*(\d+)\s*(\w+)\s*$/;
216 7 ▶ if ( $n && $unit && ( my $secs = $_expiration_units{$unit} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n and $unit and my $secs = $_expiration_units{$unit}) { } 6 1 7
elsif ($expires =~ /^\s*(\d+)\s*$/) { } 1 0 1
Condition50%3/6
$n and $unit and my $secs = $_expiration_units{$unit}
!ll&&!rl&&r
016
$n and $unit
!ll&&!rl&&r
007
MC/DC33%1/3
$n and $unit and my $secs = $_expiration_units{$unit}
$n $unit my $secs = $_expiration_units{$unit}
Truth table50%2/4
A and B and C
A= $nB= $unitC= my $secs = $_expiration_units{$unit}
ABCresult
0XX0
10X0
1100
1111
217 6 return $n * $secs;
218 }
219 elsif ( $expires =~ /^\s*(\d+)\s*$/ ) {
220 1 return $expires;
221 }
222 else {
223 0 Carp::carp "Invalid expiration time '$expires'";
224 0 return 0;
225 }
226 }
227  
228 1 ▶sub _304 {
Subroutine _304: called CC=1 SCAR=0.0
229 1 my $self = shift;
230 1 $self->_response( 304, 'Not Modified' );
231 }
232  
233 1 ▶sub _404 {
Subroutine _404: called CC=1 SCAR=0.0
234 1 my $self = shift;
235 1 $self->_response( 404, 'Not Found' );
236 }
237  
238 1 ▶sub _500 {
Subroutine _500: called CC=1 SCAR=0.0
239 1 my $self = shift;
240 1 $self->_response( 500, 'Internal Server Error' );
241 }
242  
243 3 ▶sub _response {
Subroutine _response: called CC=1 SCAR=0.0
244 3 my $self = shift;
245 3 my ( $code, $content ) = @_;
246 return [
247 3 $code,
248 [ 'Content-Type' => 'text/plain',
249 'Content-Length' => length($content)
250 ],
251 [$content]
252 ];
253 }
254  
255 9 1;
256 __END__
« script/assets-railslike-precompiler.pl ↑ Summary lib/Plack/Middleware/Assets/RailsLike/Compiler.pm »