blib/lib/Kossy.pm

filter: Statementstmt 84.7% 156 / 184 Branchbran 57.6% 30 / 52 Conditioncond 47.3% 9 / 19 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 89.1% 33 / 37 Podpod 85.7% 6 / 7 totaltotal 75.5% 235 / 311 CC 37 SCAR 15.7
CC
37
Cov
76%
_wrap_filter
20.0
filter
12.0
__ANON__
8.1
CRAP 4.8
« blib/lib/Kossy/Response.pm ↑ Summary blib/lib/Kossy/Exception.pm »
1 package Kossy;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Carp qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Cwd qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use File::Basename qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Text::Xslate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use HTML::FillInForm::Lite qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Router::Boom;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 use Class::Accessor::Lite (
16 7 new => 0,
17 rw => [qw/root_dir/]
18 7 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Kossy::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Kossy::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 7 ▶use Kossy::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 7 ▶use Kossy::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use HTTP::Headers::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 our $VERSION = '0.50';
27 7 our @EXPORT = qw/new root_dir psgi build_app _router _connect get post router filter _wrap_filter/;
28  
29 7 our $XSLATE_CACHE = 1;
30 7 our $XSLATE_CACHE_DIR;
31 7 our $SECURITY_HEADER = 1;
32  
33 # cache underscore translation
34 7 HTTP::Headers::Fast::_standardize_field_name('X-Frame-Options');
35  
36 sub new {
37 5 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
38 5 my %args;
39 5 ▶ if ( @_ < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) { } 2 3 5
40 2 my $root_dir = shift;
41 2 my @caller = caller;
42 2 ▶ $root_dir ||= File::Basename::dirname( Cwd::realpath($caller[1]) );
Condition66%2/3
$root_dir ||= File::Basename::dirname(Cwd::realpath($caller[1]))
l!l&&r!l&&!r
110
MC/DC0%0/2
$root_dir ||= File::Basename::dirname(Cwd::realpath($caller[1]))
$root_dir File::Basename::dirname(Cwd::realpath($caller[1]))
Truth table66%2/3
A ||= B
A= $root_dirB= File::Basename::dirname(Cwd::realpath($caller[1]))
ABresult
1X1
011
000
43 2 $args{root_dir} = $root_dir;
44 }
45 else {
46 3 %args = @_;
47 }
48  
49 5 bless \%args, $class;
50 }
51  
52 sub psgi {
53 2 ▶ my $self = shift;
Subroutine psgi: called CC=4 SCAR=14.7
Pod documented
54 2 ▶ if ( ! ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 2 0 2
55 2 my %args;
56 2 ▶ if ( @_ < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) { } 2 0 2
57 2 my $root_dir = shift;
58 2 my @caller = caller;
59 2 ▶ $root_dir ||= File::Basename::dirname( Cwd::realpath($caller[1]) );
Condition33%1/3
$root_dir ||= File::Basename::dirname(Cwd::realpath($caller[1]))
l!l&&r!l&&!r
200
MC/DC0%0/2
$root_dir ||= File::Basename::dirname(Cwd::realpath($caller[1]))
$root_dir File::Basename::dirname(Cwd::realpath($caller[1]))
Truth table33%1/3
A ||= B
A= $root_dirB= File::Basename::dirname(Cwd::realpath($caller[1]))
ABresult
1X1
011
000
60 2 $args{root_dir} = $root_dir;
61 }
62 else {
63 0 %args = @_;
64 }
65 2 $self = $self->new(%args);
66 }
67  
68 2 $self->build_app;
69 }
70  
71 sub build_app {
72 2 ▶ my $self = shift;
Subroutine build_app: called CC=3 SCAR=11.5
Pod undocumented
73  
74 #router
75 2 my $router = Router::Boom->new;
76 2 $router->add($_ => $self->_router->{$_} ) for keys %{$self->_router};
77 2 my $xslate_cache_local = $XSLATE_CACHE;
78 2 my $xslate_cache_dir_local = $XSLATE_CACHE_DIR;
79 2 my $security_header_local = $SECURITY_HEADER;
80 2 my %match_cache;
81  
82 #xslate
83 2 my $fif = HTML::FillInForm::Lite->new();
84 my $tx = Text::Xslate->new(
85 path => [ $self->root_dir . '/views' ],
86 input_layer => ':utf8',
87 module => ['Text::Xslate::Bridge::TT2Like','Number::Format' => [':subs']],
88 function => {
89 fillinform => sub {
90 0 ▶ my $q = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 return sub {
92 0 ▶ my ($html) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 return Text::Xslate::mark_raw( $fif->fill( \$html, $q ) );
94 }
95 0 }
96 },
97 2 ▶ cache => $xslate_cache_local,
Branch: 1 branch
BranchTrueFalseTotal
defined $xslate_cache_dir_local ? : 0 2 2
98 defined $xslate_cache_dir_local ? ( cache_dir => $xslate_cache_dir_local ) : (),
99 );
100  
101 sub {
102 18 ▶ my $env = shift;
Subroutine __ANON__: called CC=1 SCAR=0.2
103 18 $Kossy::Response::SECURITY_HEADER = $security_header_local;
104 try {
105 18 ▶ my $header = bless {
Subroutine __ANON__: called CC=6 SCAR=18.7
Branch: 1 branch
BranchTrueFalseTotal
$security_header_local ? : 17 1 18
106 'content-type' => 'text/html; charset=UTF-8',
107 $security_header_local ? ('x-frame-options' => 'DENY') : (),
108 }, 'HTTP::Headers::Fast';
109 18 my $c = Kossy::Connection->new({
110 tx => $tx,
111 req => Kossy::Request->new($env),
112 res => Kossy::Response->new(200, $header),
113 stash => {},
114 });
115 18 my $method = uc($env->{REQUEST_METHOD});
116 18 my $cache_key = $method . '-' . $env->{PATH_INFO};
117 my ($match,$args) = try {
118 18 ▶ if ( exists $match_cache{$cache_key} ) {
Subroutine __ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if (exists $match_cache{$cache_key}) 2 16 18
119 2 return @{$match_cache{$cache_key}};
120 }
121 16 my $path_info = Encode::decode_utf8( $env->{PATH_INFO}, Encode::FB_CROAK | Encode::LEAVE_SRC );
122 16 my @match = $router->match($path_info);
123 16 ▶ if ( !@match ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@match) 2 14 16
124 2 $c->halt(404);
125 }
126
127 14 ▶ if ( !exists $match[0]->{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $match[0]{$method}) 2 12 14
128 2 $c->halt(405);
129 }
130 $match_cache{$cache_key} = [$match[0]->{$method},$match[1]]
131 12 ▶ if ! scalar keys %{$match[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$match[1];} 10 2 12
132 12 return ($match[0]->{$method},$match[1]);
133 } catch {
134 4 ▶ if ( ref $_ && ref $_ eq 'Kossy::Exception' ) {
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and ref $_ eq "Kossy::Exception") 4 0 4
Condition33%1/3
ref $_ and ref $_ eq "Kossy::Exception"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $_ and ref $_ eq "Kossy::Exception"
ref $_ ref $_ eq "Kossy::Exception"
Truth table33%1/3
A and B
A= ref $_B= ref $_ eq "Kossy::Exception"
ABresult
0X0
100
111
135 4 die $_; #rethrow
136 }
137 0 $c->halt(400,'unexpected character in request');
138 18 };
139
140 14 my $code = $match->{__action__};
141 14 ▶ my $filters = $match->{__filter__} || [];
Condition50%1/2
$match->{'__filter__'} || []
l!l
140
MC/DC0%0/1
$match->{'__filter__'} || []
$match->{'__filter__'}
Truth table50%1/2
A
A= $match->{'__filter__'}
Aresult
11
00
142 14 ▶ if ( $] == 5.020000 || $] == 5.020100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] == 5.02 or $] == 5.0201) { } 0 14 14
Condition33%1/3
$] == "5.02" or $] == "5.0201"
l!l&&r!l&&!r
0014
MC/DC0%0/2
$] == "5.02" or $] == "5.0201"
$] == "5.02" $] == "5.0201"
Truth table33%1/3
A or B
A= $] == "5.02"B= $] == "5.0201"
ABresult
1X1
011
000
143 # workaround for 5.20.0 or 5.20.1 https://github.com/kazeburo/Kossy/pull/10
144 0 my %args = map { $_ => Encode::decode_utf8($args->{$_}) } keys %$args;
145 0 $c->args(\%args);
146 } else {
147 14 $c->args({%$args});
148 }
149 my $app = sub {
150 14 ▶ my ($self, $c) = @_;
Subroutine __ANON__: called CC=7 SCAR=20.9
151 14 my $response;
152 14 my $res = $code->($self, $c);
153 14 ▶ Carp::croak "Undefined Response" if ! defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 0 14 14
154 14 ▶ my $res_t = ref($res) || '';
Condition100%2/2
ref $res || ''
l!l
113
MC/DC100%1/1
ref $res || ''
ref $res
Truth table100%2/2
A
A= ref $res
Aresult
11
00
155 14 ▶ if ( $res_t eq 'Kossy::Response' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($res_t eq 'Kossy::Response') { } 1 13 14
elsif ($res_t eq 'Plack::Response') { } 0 13 13
elsif ($res_t eq 'ARRAY') { } 0 13 13
elsif (not $res_t) { } 13 0 13
156 1 $response = $res;
157 }
158 elsif ( $res_t eq 'Plack::Response' ) {
159 0 $response = bless $res, 'Kossy::Response';
160 }
161 elsif ( $res_t eq 'ARRAY' ) {
162 0 $response = Kossy::Response->new(@$res);
163 }
164 elsif ( !$res_t ) {
165 13 $c->res->body($res);
166 13 $response = $c->res;
167 }
168 else {
169 0 Carp::croak sprintf "Unknown Response: %s", $res_t;
170 }
171 14 $response;
172 14 };
173
174 14 for my $filter ( reverse @$filters ) {
175 0 $app = $self->_wrap_filter($filter,$app);
176 }
177 # do all
178 14 local $Kossy::Response::DIRECT;
179 14 $app->($self, $c)->finalize;
180 } catch {
181 4 ▶ if ( ref $_ && ref $_ eq 'Kossy::Exception' ) {
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and ref $_ eq "Kossy::Exception") 4 0 4
Condition33%1/3
ref $_ and ref $_ eq "Kossy::Exception"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $_ and ref $_ eq "Kossy::Exception"
ref $_ ref $_ eq "Kossy::Exception"
Truth table33%1/3
A and B
A= ref $_B= ref $_ eq "Kossy::Exception"
ABresult
0X0
100
111
182 4 return $_->response;
183 }
184 0 die $_;
185 18 };
186 2 };
187 }
188  
189  
190  
191 7 my $_ROUTER={};
192 sub _router {
193 71 ▶ my $klass = shift;
Subroutine _router: called CC=3 SCAR=11.0
194 71 ▶ my $class = ref $klass ? ref $klass : $klass;
Branch: 1 branch
BranchTrueFalseTotal
ref $klass ? : 20 51 71
195 71 ▶ if ( !$_ROUTER->{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ROUTER->{$class}) 3 68 71
196 3 $_ROUTER->{$class} = {};
197 }
198 71 $_ROUTER->{$class};
199 }
200  
201 sub _connect {
202 27 ▶ my $class = shift;
Subroutine _connect: called CC=4 SCAR=14.0
203 27 my ( $methods, $pattern, $filter, $code ) = @_;
204 27 ▶ $methods = ref($methods) ? $methods : [$methods];
Branch: 1 branch
BranchTrueFalseTotal
ref $methods ? : 27 0 27
205 27 ▶ if (!$code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 27 0 27
206 27 $code = $filter;
207 27 $filter = [];
208 }
209 27 for my $method ( @$methods ) {
210 51 $class->_router->{$pattern}->{$method} = {
211 __action__ => $code,
212 __filter__ => $filter
213 };
214 }
215 }
216  
217 sub get {
218 21 ▶ my $class = caller;
Subroutine get: called CC=1 SCAR=0.0
Pod documented
219 21 $class->_connect( ['GET','HEAD'], @_ );
220 }
221  
222 sub post {
223 3 ▶ my $class = caller;
Subroutine post: called CC=1 SCAR=0.0
Pod documented
224 3 $class->_connect( ['POST'], @_ );
225 }
226  
227 sub router {
228 3 ▶ my $class = caller;
Subroutine router: called CC=1 SCAR=0.0
Pod documented
229 3 $class->_connect( @_ );
230 }
231  
232 7 my $_FILTER={};
233 sub filter {
234 0 ▶ my $class = caller;
Subroutine filter: not called CC=3 SCAR=24.8
Pod documented
235 0 ▶ if ( !$_FILTER->{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_FILTER->{$class}) 0 0 0
236 0 $_FILTER->{$class} = {};
237 }
238 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
239 0 $_FILTER->{$class}->{$_[0]} = $_[1];
240 }
241 0 $_FILTER->{$class};
242 }
243  
244 sub _wrap_filter {
245 0 ▶ my $klass = shift;
Subroutine _wrap_filter: not called CC=4 SCAR=30.0
246 0 ▶ my $class = ref $klass ? ref $klass : $klass;
Branch: 1 branch
BranchTrueFalseTotal
ref $klass ? : 0 0 0
247 0 ▶ if ( !$_FILTER->{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_FILTER->{$class}) 0 0 0
248 0 $_FILTER->{$class} = {};
249 }
250 0 my ($filter,$app) = @_;
251 0 my $filter_subref = $_FILTER->{$class}->{$filter};
252 0 ▶ Carp::croak sprintf("Filter:%s is not exists", $filter) unless $filter_subref;
Branch: 1 branch
BranchTrueFalseTotal
unless $filter_subref 0 0 0
253 0 return $filter_subref->($app);
254 }
255  
256 7 1;
257  
258 __END__
« blib/lib/Kossy/Response.pm ↑ Summary blib/lib/Kossy/Exception.pm »