blib/lib/Clustericious/RouteBuilder.pm

filter: Statementstmt 74.3% 81 / 109 Branchbran 52.0% 26 / 50 Conditioncond 47.6% 20 / 42 MC/DCmcdc 22.2% 6 / 27 Subroutinesub 65.0% 13 / 20 Podpod n/a totaltotal 58.8% 146 / 248 CC 44 SCAR 32.9
CC
44
Cov
63%
startup_route_builder
70.1
__ANON__
30.0
__ANON__
22.9
CRAP 26.8
« blib/lib/Clustericious/RouteBuilder/CRUD.pm ↑ Summary blib/lib/Clustericious/Plugin/CommonRoutes.pm »
1 package Clustericious::RouteBuilder;
2  
3 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use Clustericious::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use Log::Log4perl qw( :easy );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use Mojo::Util qw( monkey_patch );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Route builder for Clustericious applications
11 32 our $VERSION = '1.29'; # VERSION
12  
13  
14 32 my %routes;
15  
16 # Much of the code below taken directly from Mojolicious::Lite.
17 32 ▶sub import {
Subroutine import: called CC=6 SCAR=19.2
18 32 my($class, $app_class) = @_;
19 32 my $caller = caller;
20
21 32 ▶ unless($app_class)
Branch: 1 branch
BranchTrueFalseTotal
unless ($app_class) 15 17 32
22 {
23 15 ▶ if ($caller->isa("Clustericious::App"))
Branch: 1 branch
BranchTrueFalseTotal
if ($caller->isa('Clustericious::App')) { } 6 9 15
24 {
25 6 $app_class = $caller;
26 }
27 else
28 {
29 9 $app_class = $caller;
30 9 ▶ $app_class =~ s/::Routes$// or die "could not guess app class : ";
Branch: 1 branch
BranchTrueFalseTotal
$app_class =~ s/::Routes$// or die "could not guess app class : " 9 0 9
31 }
32 }
33  
34 32 my @routes;
35 32 $routes{$app_class} = \@routes;
36  
37 # Route generator
38 my $route_sub = sub
39 72 ▶ {
Subroutine __ANON__: called CC=15 SCAR=31.3
40 72 my ($methods, @args) = @_;
41  
42 72 my ($cb, $constraints, $defaults, $name, $pattern);
43 72 my $conditions = [];
44  
45 # Route information
46 72 my $condition;
47 72 ▶ while (my $arg = shift @args)
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift @args) 161 72 233
48 {
49  
50 # Condition can be everything
51 161 ▶ if ($condition)
Branch: 7 branches
BranchTrueFalseTotal
if ($condition) { } 0 161 161
elsif (not ref $arg || $pattern) { } 64 97 161
elsif (not ref $arg and @args) { } 0 97 97
elsif (not ref $arg) { } 35 62 97
elsif (ref $arg eq 'CODE') { } 62 0 62
elsif (ref $arg eq 'ARRAY') { } 0 0 0
elsif (ref $arg eq 'HASH') { } 0 0 0
52 {
53 0 push @$conditions, $condition => $arg;
54 0 $condition = undef;
55 }
56  
57 # First scalar is the pattern
58 ▶ elsif (!ref $arg && !$pattern)
Condition100%3/3
ref $arg || $pattern
l!l&&r!l&&!r
623564
MC/DC100%2/2
ref $arg || $pattern
ref $arg $pattern
Truth table100%3/3
A || B
A= ref $argB= $pattern
ABresult
1X1
011
000
59 {
60 64 $pattern = $arg;
61 }
62  
63 # Scalar
64 ▶ elsif (!ref $arg && @args)
Condition66%2/3
not ref $arg and @args
!ll&&!rl&&r
62350
MC/DC0%0/2
not ref $arg and @args
not ref $arg @args
Truth table66%2/3
not(A) and B
A= not ref $argB= @args
ABresult
0X0
100
111
65 {
66 0 $condition = $arg;
67 }
68  
69 # Last scalar is the route name
70 elsif (!ref $arg)
71 {
72 35 $name = $arg;
73 }
74  
75 # Callback
76 elsif (ref $arg eq 'CODE')
77 {
78 62 $cb = $arg;
79 }
80  
81 # Constraints
82 elsif (ref $arg eq 'ARRAY')
83 {
84 0 $constraints = $arg;
85 }
86  
87 # Defaults
88 elsif (ref $arg eq 'HASH')
89 {
90 0 $defaults = $arg;
91 }
92 }
93  
94 # Defaults
95 0 ▶ $cb ||= sub {1};
Subroutine __ANON__: not called CC=1 SCAR=0.6
Condition66%2/3
$cb ||= sub { 1; }
l!l&&r!l&&!r
62100
MC/DC0%0/2
$cb ||= sub { 1; }
$cb sub { 1; }
Truth table66%2/3
A ||= B
A= $cbB= sub { 1; }
ABresult
1X1
011
000
96 72 ▶ $constraints ||= [];
Condition50%1/2
$constraints ||= []
l!l
072
MC/DC0%0/1
$constraints ||= []
$constraints
Truth table50%1/2
A
A= $constraints
Aresult
11
00
97  
98 # Merge
99 72 ▶ $defaults ||= {};
Condition50%1/2
$defaults ||= {}
l!l
072
MC/DC0%0/1
$defaults ||= {}
$defaults
Truth table50%1/2
A
A= $defaults
Aresult
11
00
100 72 $defaults = {%$defaults, cb => $cb};
101  
102 # Name
103 72 ▶ $name ||= '';
Condition100%2/2
$name ||= ""
l!l
3537
MC/DC100%1/1
$name ||= ""
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
104  
105 72 push @routes, {
106 name => $name,
107 pattern => $pattern,
108 constraints => $constraints,
109 conditions => $conditions,
110 defaults => $defaults,
111 methods => $methods
112 };
113  
114 32 };
115  
116 # Export
117 32 monkey_patch $app_class, startup_route_builder => \&_startup_route_builder;
118  
119 0 ▶ monkey_patch $caller, any => sub { $route_sub->(ref $_[0] ? shift : [], @_) };
Subroutine __ANON__: not called CC=2 SCAR=12.4
Subroutine any: called CC=2 SCAR=12.4
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
ref $_[0] ? : 0 0 0
120 0 ▶ monkey_patch $caller, $_ => sub { unshift @_, 'delete'; goto $route_sub } for qw( Delete del );
Subroutine del: not called CC=1 SCAR=0.6
Subroutine Delete: called CC=1 SCAR=0.6
121  
122 32 foreach my $method (qw( get head ladder post put websocket authenticate authorize ))
123 {
124 256 ▶ monkey_patch $caller, $method => sub { unshift @_, $method; goto $route_sub };
Subroutine websocket: called CC=1 SCAR=0.0
Subroutine ladder: called CC=1 SCAR=0.0
125 }
126 }
127  
128 32 ▶sub _startup_route_builder {
Subroutine startup_route_builder: called CC=19 SCAR=42.5
129 32 my($app, $auth_plugin) = @_;
130  
131 0 ▶ my $stashed = $routes{ ref $app } // do { WARN "no routes stashed for $app"; [] };
Condition33%1/3
$routes{ref $app} // do { WARN("no routes stashed for $app"); [] }
l!l&&r!l&&!r
3200
MC/DC0%0/2
$routes{ref $app} // do { WARN("no routes stashed for $app"); [] }
$routes{ref $app} do { WARN("no routes stashed for $app"); [] }
Truth table33%1/3
A // B
A= $routes{ref $app}B= do { WARN("no routes stashed for $app"); [] }
ABresult
1X1
011
000
132 32 my @stashed = @$stashed;
133 32 my $routes = $app->routes;
134 32 my $head_route = $app->routes;
135 32 my $head_authenticated = $head_route;
136  
137 32 for my $spec (@stashed)
138 {
139 my ($name,$pattern,$constraints,$conditions,$defaults,$methods) =
140 32 @$spec{qw/name pattern constraints conditions defaults methods/};
141  
142 # authenticate, always connects to app->routes
143 32 ▶ if (!ref $methods && $methods eq 'authenticate')
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $methods and $methods eq "authenticate") 1 31 32
Condition66%2/3
not ref $methods and $methods eq "authenticate"
!ll&&!rl&&r
0311
MC/DC50%1/2
not ref $methods and $methods eq "authenticate"
not ref $methods $methods eq "authenticate"
Truth table66%2/3
not(A) and B
A= not ref $methodsB= $methods eq "authenticate"
ABresult
0X0
100
111
144 {
145 1 ▶ my $realm = $pattern || ref $app;
Condition33%1/3
$pattern || ref $app
l!l&&r!l&&!r
010
MC/DC0%0/2
$pattern || ref $app
$pattern ref $app
Truth table33%1/3
A || B
A= $patternB= ref $app
ABresult
1X1
011
000
146 0 ▶ my $cb = defined $auth_plugin ? sub { $auth_plugin->authenticate(shift, $realm) } : sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Subroutine __ANON__: not called CC=1 SCAR=2.0
Branch: 1 branch
BranchTrueFalseTotal
defined $auth_plugin ? : 0 1 1
147 1 $head_route = $head_authenticated = $routes =
148 $app->routes->under->to( { cb => $cb } )->name("authenticated");
149 1 next;
150 }
151  
152 # authorize replaces previous authorize's
153 31 ▶ if (!ref $methods && $methods eq 'authorize')
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $methods and $methods eq "authorize") 1 30 31
Condition66%2/3
not ref $methods and $methods eq "authorize"
!ll&&!rl&&r
0301
MC/DC50%1/2
not ref $methods and $methods eq "authorize"
not ref $methods $methods eq "authorize"
Truth table66%2/3
not(A) and B
A= not ref $methodsB= $methods eq "authorize"
ABresult
0X0
100
111
154 {
155 1 ▶ die "put authenticate before authorize" unless $head_authenticated;
Branch: 1 branch
BranchTrueFalseTotal
unless $head_authenticated 0 1 1
156 1 my $action = $pattern;
157 1 my $resource = $name;
158 1 ▶ if($auth_plugin)
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_plugin) { } 0 1 1
159 {
160 $head_route = $routes = $head_authenticated->under->to( {
161 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
162 0 my($c) = @_;
163 # Dynamically compute resource/action
164 0 my ($d_resource,$d_action) = ($resource, $action);
165 0 ▶ $d_resource =~ s/<path>/$c->req->url->path/e if $d_resource;
Branch: 1 branch
BranchTrueFalseTotal
if $d_resource 0 0 0
166 0 ▶ $d_resource ||= $c->req->url->path;
Condition0%0/3
$d_resource ||= $c->req->url->path
l!l&&r!l&&!r
000
MC/DC0%0/2
$d_resource ||= $c->req->url->path
$d_resource $c->req->url->path
Truth table0%0/3
A ||= B
A= $d_resourceB= $c->req->url->path
ABresult
1X1
011
000
167 0 ▶ $d_action =~ s/<method>/$c->req->method/e if $d_action;
Branch: 1 branch
BranchTrueFalseTotal
if $d_action 0 0 0
168 0 ▶ $d_action ||= $c->req->method;
Condition0%0/3
$d_action ||= $c->req->method
l!l&&r!l&&!r
000
MC/DC0%0/2
$d_action ||= $c->req->method
$d_action $c->req->method
Truth table0%0/3
A ||= B
A= $d_actionB= $c->req->method
ABresult
1X1
011
000
169 0 $auth_plugin->authorize( $c, $d_action, $d_resource );
170 }
171 0 });
172 }
173 else
174 {
175 0 ▶ $head_route = $routes = $head_authenticated->under->to({ cb => sub { 1 } });
Subroutine __ANON__: not called CC=1 SCAR=1.2
176 }
177 1 next;
178 }
179  
180 # ladders don't replace previous ladders
181 30 ▶ if (!ref $methods && $methods eq 'ladder')
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $methods and $methods eq "ladder") 0 30 30
Condition33%1/3
not ref $methods and $methods eq "ladder"
!ll&&!rl&&r
0300
MC/DC0%0/2
not ref $methods and $methods eq "ladder"
not ref $methods $methods eq "ladder"
Truth table33%1/3
not(A) and B
A= not ref $methodsB= $methods eq "ladder"
ABresult
0X0
100
111
182 {
183 0 ▶ die "constraints not handled in ladders" if $constraints && @$constraints;
Branch: 1 branch
BranchTrueFalseTotal
if $constraints and @$constraints 0 0 0
Condition0%0/3
$constraints and @$constraints
!ll&&!rl&&r
000
MC/DC0%0/2
$constraints and @$constraints
$constraints @$constraints
Truth table0%0/3
A and B
A= $constraintsB= @$constraints
ABresult
0X0
100
111
184 0 $routes = $routes
185 ->under( $pattern )
186 ->over($conditions)
187 ->to($defaults)
188 ->name($name);
189 0 next;
190 }
191  
192 # WebSocket?
193 30 ▶ my $websocket = 1 if !ref $methods && $methods eq 'websocket';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $methods and $methods eq "websocket" 1 29 30
Condition66%2/3
not ref $methods and $methods eq "websocket"
!ll&&!rl&&r
0291
MC/DC50%1/2
not ref $methods and $methods eq "websocket"
not ref $methods $methods eq "websocket"
Truth table66%2/3
not(A) and B
A= not ref $methodsB= $methods eq "websocket"
ABresult
0X0
100
111
194 30 ▶ $methods = [] if $websocket;
Branch: 1 branch
BranchTrueFalseTotal
if $websocket 1 29 30
195  
196 # Create route
197 30 my $route = $routes
198 ->route( $pattern, @$constraints )
199 ->over($conditions)
200 ->via($methods)
201 ->to($defaults)
202 ->name($name);
203  
204 # WebSocket
205 0 ▶ $route->websocket if $websocket;
Branch: 1 branch
BranchTrueFalseTotal
if $websocket 0 0 0
206 }
207 }
208  
209 32 1;
210  
211 __END__
« blib/lib/Clustericious/RouteBuilder/CRUD.pm ↑ Summary blib/lib/Clustericious/Plugin/CommonRoutes.pm »