blib/lib/Plack/Middleware/JSConcat.pm

filter: Statementstmt 46.5% 20 / 43 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 6 / 10 Podpod 50.0% 2 / 4 totaltotal 44.4% 28 / 63 CC 4 SCAR 15.0
CC
4
Cov
41%
new
12.0
call
6.0
minify_js
2.0
CRAP 4.5
↑ Summary
1 package Plack::Middleware::JSConcat;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.29';
6 1 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 __PACKAGE__->mk_accessors(qw(js_content files key mtime filter));
10  
11  
12 1 ▶use IPC::Run3;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
16 0 my $class = shift;
17 0 my $self = $class->SUPER::new(@_);
18  
19 0 local $/;
20 0 ▶ $self->js_content(join("\n", map { open my $fh, '<', $_ or die "$_: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $_ or die "${_}: $!" 0 0 0
21 0 "/* $_ */\n".<$fh>
22 0 } @{$self->files}));
23  
24 0 ▶ $self->minify_js if $self->filter;
Branch: 1 branch
BranchTrueFalseTotal
if $self->filter 0 0 0
25  
26 0 $self->key( md5_hex( $self->js_content ) );
27 # XXX: use the latest mtime from $self->files instead
28 0 $self->mtime( time() );
29  
30 0 return $self;
31 }
32  
33 0 ▶sub minify_js {
Subroutine minify_js: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my $self = shift;
35 0 my $output = '';
36 0 local $SIG{'CHLD'} = 'DEFAULT';
37 0 run3 [$self->filter], \$self->js_content, \$output, undef;
38  
39 0 $self->js_content($output);
40 }
41  
42 0 ▶sub serve_js {
Subroutine serve_js: not called CC=1 SCAR=6.9
Pod undocumented
43 0 my $self = shift;
44 0 my $content_type =
45 return [ 200, [ 'Content-Type' => 'text/javascript',
46 'Content-Length' => length($self->js_content),
47 'Last-Modified' => HTTP::Date::time2str( $self->mtime ),
48 ],
49 [$self->js_content] ];
50 }
51  
52 0 ▶sub call {
Subroutine call: not called CC=2 SCAR=17.9
Pod documented
53 0 my $self = shift;
54 0 my $env = shift;
55 0 my $url = $env->{'psgix.jsconcat.url'} = '/_js/'.$self->key;
56 0 ▶ return $self->serve_js if $env->{PATH_INFO} eq $url;
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"PATH_INFO"} eq $url 0 0 0
57  
58 0 return $self->app->($env);
59 }
60  
61 1 1;
62  
63 __END__
↑ Summary