| 1 | #!perl | ||||||||||||||||||||||||||||
| 2 | #PODNAME: Raisin::Routes::Endpoint | ||||||||||||||||||||||||||||
| 3 | #ABSTRACT: Endpoint class for Raisin::Routes. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Raisin::Routes::Endpoint; | ||||||||||||||||||||||||||||
| 9 | 18 | $Raisin::Routes::Endpoint::VERSION = '0.94'; | |||||||||||||||||||||||||||
| 10 | 18 | ▶ | use Plack::Util::Accessor qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | check | ||||||||||||||||||||||||||||
| 12 | code | ||||||||||||||||||||||||||||
| 13 | desc | ||||||||||||||||||||||||||||
| 14 | entity | ||||||||||||||||||||||||||||
| 15 | method | ||||||||||||||||||||||||||||
| 16 | named | ||||||||||||||||||||||||||||
| 17 | params | ||||||||||||||||||||||||||||
| 18 | path | ||||||||||||||||||||||||||||
| 19 | regex | ||||||||||||||||||||||||||||
| 20 | summary | ||||||||||||||||||||||||||||
| 21 | produces | ||||||||||||||||||||||||||||
| 22 | 18 | ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 18 | ▶ | use Raisin::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 83 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 83 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 83 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 83 | $self->check({}); | |||||||||||||||||||||||||||
| 32 | 83 | $self->params([]); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 83 | @$self{ keys %args } = values %args; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Populate params index | ||||||||||||||||||||||||||||
| 37 | 83 | for my $p (@{ $self->params }) { | |||||||||||||||||||||||||||
| 38 | 59 | ▶ | if ($p->named && (my $re = $p->regex)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$p->named and my $re = $p->regex
MC/DC0%0/2
$p->named and my $re = $p->regex
$p->named
my $re = $p->regex
Truth table66%2/3
A and B
A= $p->namedB= my $re = $p->regex
| |||||||||||||||||||||||||||||
| 39 | 0 | $self->{check}{ $p->name } = $re; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 83 | $self->regex($self->_build_regex); | |||||||||||||||||||||||||||
| 44 | 83 | $self; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 83 | ▶ | sub _build_regex { | ||||||||||||||||||||||||||
|
Subroutine
_build_regex: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 48 | 83 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | 83 | ▶ | return $self->path if ref($self->path) eq 'Regexp'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 83 | my $regex = $self->path; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 83 | $regex =~ s/(.?)([:*?])(\w+)/$self->_rep_regex($1, $2, $3)/eg; | |||||||||||||||||||||||||||
| 54 | 83 | $regex =~ s/[{}]//g; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Allows any extensions | ||||||||||||||||||||||||||||
| 57 | 83 | $regex .= "(?:\\\.[^.]+?)?"; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 83 | qr/^$regex$/; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 50 | ▶ | sub _rep_regex { | ||||||||||||||||||||||||||
|
Subroutine
_rep_regex: called CC=9 SCAR=26.1
| |||||||||||||||||||||||||||||
| 63 | 50 | my ($self, $char, $switch, $token) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 50 | my ($a, $b, $r) = ("(?<$token>", ')', undef); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 50 | for ($switch) { | |||||||||||||||||||||||||||
| 68 | 50 | ▶ | if ($_ eq ':' || $_ eq '?') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_ eq ":" or $_ eq "?"
MC/DC0%0/2
$_ eq ":" or $_ eq "?"
$_ eq ":"
$_ eq "?"
Truth table33%1/3
A or B
A= $_ eq ":"B= $_ eq "?"
| |||||||||||||||||||||||||||||
| 69 | 50 | ▶ | $r = $a . ($self->check->{$token} // '[^/]+?') . $b; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->check->{$token} // "[^/]+?"
MC/DC0%0/1
$self->check->{$token} // "[^/]+?"
$self->check->{$token}
Truth table50%1/2
A
A= $self->check->{$token}
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 50 | ▶ | if ($_ eq '*') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $r = $a . '.+' . $b; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 50 | ▶ | $char = $char . '?' if $char eq '/' && $switch eq '?'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$char eq "/" and $switch eq "?"
MC/DC0%0/2
$char eq "/" and $switch eq "?"
$char eq "/"
$switch eq "?"
Truth table33%1/3
A and B
A= $char eq "/"B= $switch eq "?"
| |||||||||||||||||||||||||||||
| 77 | 50 | ▶ | $r .= '?' if $switch eq '?'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 50 | return $char . $r; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 20 | ▶ | sub tags { | ||||||||||||||||||||||||||
|
Subroutine
tags: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 20 | ▶ | unless ($self->{tags}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 17 | return [Raisin::Util::make_tag_from_path($self->path)]; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 3 | $self->{tags}; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 467 | ▶ | sub match { | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 467 | my ($self, $method, $path) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 467 | $self->{named} = undef; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 467 | ▶ | return if !$method || lc($method) ne lc($self->method); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $method or lc $method ne lc $self->method
MC/DC50%1/2
not $method or lc $method ne lc $self->method
not $method
lc $method ne lc $self->method
Truth table66%2/3
not(A) or B
A= not $methodB= lc $method ne lc $self->method
| |||||||||||||||||||||||||||||
| 98 | 146 | ▶ | return if $path !~ $self->regex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 76 | my %captured = %+; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 76 | foreach my $p (@{ $self->params }) { | |||||||||||||||||||||||||||
| 103 | 96 | ▶ | next unless $p->named; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 24 | my $copy = $captured{ $p->name }; | |||||||||||||||||||||||||||
| 105 | 24 | ▶ | return unless $p->validate(\$copy, 'quite'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 75 | $self->named(\%captured); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 75 | 1; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 18 | 1; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||