blib/lib/Kossy.pm

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