blib/lib/Raisin/Routes.pm

filter: Statementstmt 89.2% 58 / 65 Branchbran 70.0% 14 / 20 Conditioncond 45.4% 5 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 10 / 10 Podpod 66.6% 2 / 3 totaltotal 77.5% 90 / 116 CC 16 SCAR 23.8
CC
16
Cov
80%
add
18.9
find
4.0
BEGIN
1.0
CRAP 10.8
« blib/lib/Raisin/Param.pm ↑ Summary blib/lib/Raisin/Routes/Endpoint.pm »
1 #!perl
2 #PODNAME: Raisin::Routes
3 #ABSTRACT: A routing class for Raisin.
4  
5 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Raisin::Routes;
9 16 $Raisin::Routes::VERSION = '0.94';
10 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Plack::Util::Accessor qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 cache
13 list
14 routes
15 16 );
16  
17 16 ▶use Raisin::Param;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use Raisin::Routes::Endpoint;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use Raisin::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 33 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
22 33 my $class = shift;
23 33 my $self = bless { id => rand() }, $class;
24  
25 33 $self->cache({});
26 33 $self->list({});
27 33 $self->routes([]);
28  
29 33 $self;
30 }
31  
32 64 ▶sub add {
Subroutine add: called CC=13 SCAR=29.4
Pod documented
33 64 my ($self, %params) = @_;
34  
35 64 my $method = uc $params{method};
36 64 my $path = $params{path};
37  
38 64 ▶ if (!$method || !$path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method and $path) 0 64 64
Condition33%1/3
$method and $path
!ll&&!rl&&r
0064
MC/DC0%0/2
$method and $path
$method $path
Truth table33%1/3
A and B
A= $methodB= $path
ABresult
0X0
100
111
39 0 carp "Method and path are required";
40 0 return;
41 }
42  
43 64 my $code = $params{code};
44 # Supports only CODE as route destination
45 64 ▶ if (!$code || !(ref($code) eq 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code and ref $code eq "CODE") 0 64 64
Condition33%1/3
$code and ref $code eq "CODE"
!ll&&!rl&&r
0064
MC/DC0%0/2
$code and ref $code eq "CODE"
$code ref $code eq "CODE"
Truth table33%1/3
A and B
A= $codeB= ref $code eq "CODE"
ABresult
0X0
100
111
46 0 carp "Invalid route params for $method $path";
47 0 return;
48 }
49  
50 64 my @pp;
51 64 for my $key (qw(params named)) {
52 128 ▶ my $next_param = Raisin::Util::iterate_params($params{$key} || []);
Condition100%2/2
$params{$key} || []
l!l
3494
MC/DC100%1/1
$params{$key} || []
$params{$key}
Truth table100%2/2
A
A= $params{$key}
Aresult
11
00
53 128 ▶ while (my ($type, $spec) = $next_param->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$next_param()) 179 0 179
54 179 ▶ last unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 128 51 179
55  
56 51 push @pp, Raisin::Param->new(
57 named => $key eq 'named',
58 type => $type, # -> requires/optional
59 spec => $spec, # -> { name => ..., type => ... }
60 );
61 }
62 }
63  
64 64 ▶ if (ref($path) && ref($path) ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path and ref $path ne "Regexp") 0 64 64
Condition33%1/3
ref $path and ref $path ne "Regexp"
!ll&&!rl&&r
6400
MC/DC0%0/2
ref $path and ref $path ne "Regexp"
ref $path ref $path ne "Regexp"
Truth table33%1/3
A and B
A= ref $pathB= ref $path ne "Regexp"
ABresult
0X0
100
111
65 0 print STDERR "Route `$path` should be SCALAR or Regexp\n";
66 0 return;
67 }
68  
69 # Cut off the last slash from a path
70 64 ▶ $path =~ s#(.+)/$#$1# if !ref($path);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 64 0 64
71  
72 my $ep = Raisin::Routes::Endpoint->new(
73 code => $code,
74 method => $method,
75 params => \@pp,
76 path => $path,
77  
78 desc => $params{desc},
79 entity => $params{entity},
80 summary => $params{summary},
81 tags => $params{tags},
82 produces => $params{produces},
83 64 );
84 64 push @{ $self->{routes} }, $ep;
85  
86 64 ▶ if ($self->list->{$path}{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->list->{$path}{$method}) 2 62 64
87 2 Raisin::log(warn => "route has been redefined: $method $path");
88 }
89  
90 64 $self->list->{$path}{$method} = scalar @{ $self->{routes} };
91 }
92  
93 69 ▶sub find {
Subroutine find: called CC=4 SCAR=14.0
Pod documented
94 69 my ($self, $method, $path) = @_;
95  
96 69 my $cache_key = lc "$method:$path";
97 my $routes
98 = exists $self->cache->{$cache_key}
99 69 ▶ ? $self->cache->{$cache_key}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->cache->{$cache_key} ? : 40 29 69
100 : $self->routes;
101  
102 69 ▶ my @found = grep { $_->match($method, $path) } @$routes or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@found) = grep({$_->match($method, $path);} @$routes) or return 68 1 69
103  
104 68 ▶ if (scalar @found > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @found > 1) 0 68 68
105 0 Raisin::log(warn => "more then one route has been found: $method $path");
106 }
107  
108 68 $self->cache->{$cache_key} = \@found;
109 68 $found[0];
110 }
111  
112 16 1;
113  
114 __END__
« blib/lib/Raisin/Param.pm ↑ Summary blib/lib/Raisin/Routes/Endpoint.pm »