| 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
Condition33%1/3
$method and $path
MC/DC0%0/2
$method and $path
$method
$path
Truth table33%1/3
A and B
A= $methodB= $path
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$code and ref $code eq "CODE"
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"
| |||||||||||||||||||||||||||||
| 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} || []
MC/DC100%1/1
$params{$key} || []
$params{$key}
Truth table100%2/2
A
A= $params{$key}
| |||||||||||||||||||||||||||||
| 53 | 128 | ▶ | while (my ($type, $spec) = $next_param->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 179 | ▶ | last unless $type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $path and ref $path ne "Regexp"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 100 | : $self->routes; | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 69 | ▶ | my @found = grep { $_->match($method, $path) } @$routes or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 68 | ▶ | if (scalar @found > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||