File Coverage

blib/lib/Raisin/Routes/Endpoint.pm
Criterion Covered Total %
statement 58 61 95.0
branch 16 22 72.7
condition 7 14 50.0
subroutine 10 10 100.0
pod 0 3 0.0
total 91 110 82.7


line stmt bran cond sub pod time code
1             #!perl
2             #PODNAME: Raisin::Routes::Endpoint
3             #ABSTRACT: Endpoint class for Raisin::Routes.
4              
5 14     14   526 use strict;
  14         30  
  14         439  
6 14     14   73 use warnings;
  14         27  
  14         820  
7              
8             package Raisin::Routes::Endpoint;
9             $Raisin::Routes::Endpoint::VERSION = '0.92';
10 14         114 use Plack::Util::Accessor qw(
11             check
12             code
13             desc
14             entity
15             method
16             named
17             params
18             path
19             regex
20             summary
21             produces
22 14     14   90 );
  14         26  
23              
24 14     14   2006 use Raisin::Util;
  14         37  
  14         9368  
25              
26             sub new {
27 83     83 0 55480 my ($class, %args) = @_;
28              
29 83         192 my $self = bless {}, $class;
30              
31 83         312 $self->check({});
32 83         666 $self->params([]);
33              
34 83         802 @$self{ keys %args } = values %args;
35              
36             # Populate params index
37 83         221 for my $p (@{ $self->params }) {
  83         193  
38 59 50 66     375 if ($p->named && (my $re = $p->regex)) {
39 0         0 $re =~ s/[\$^]//g;
40 0         0 $self->{check}{ $p->name } = $re;
41             }
42             }
43              
44 83         567 $self->regex($self->_build_regex);
45 83         674 $self;
46             }
47              
48             sub _build_regex {
49 83     83   144 my $self = shift;
50 83 50       177 return $self->path if ref($self->path) eq 'Regexp';
51              
52 83         469 my $regex = $self->path;
53              
54 83         749 $regex =~ s/(.?)([:*?])(\w+)/$self->_rep_regex($1, $2, $3)/eg;
  50         140  
55 83         234 $regex =~ s/[{}]//g;
56              
57             # Allows any extensions
58 83         156 $regex .= "(?:\\\.[^.]+?)?";
59              
60 83         1869 qr/^$regex$/;
61             }
62              
63             sub _rep_regex {
64 50     50   237 my ($self, $char, $switch, $token) = @_;
65              
66 50         163 my ($a, $b, $r) = ("(?<$token>", ')', undef);
67              
68 50         139 for ($switch) {
69 50 50 33     154 if ($_ eq ':' || $_ eq '?') {
70 50   50     132 $r = $a . ($self->check->{$token} // '[^/]+?') . $b;
71             }
72 50 50       417 if ($_ eq '*') {
73 0         0 $r = $a . '.+' . $b;
74             }
75             }
76              
77 50 50 33     254 $char = $char . '?' if $char eq '/' && $switch eq '?';
78 50 50       101 $r .= '?' if $switch eq '?';
79              
80 50         207 return $char . $r;
81             }
82              
83             sub tags {
84 20     20 0 222 my $self = shift;
85              
86 20 100       43 unless ($self->{tags}) {
87 17         48 return [Raisin::Util::make_tag_from_path($self->path)];
88             }
89              
90 3         23 $self->{tags};
91             }
92              
93             sub match {
94 463     463 0 22645 my ($self, $method, $path) = @_;
95              
96 463         733 $self->{named} = undef;
97              
98 463 100 66     1205 return if !$method || lc($method) ne lc($self->method);
99 142 100       922 return if $path !~ $self->regex;
100              
101 13     13   6905 my %captured = %+;
  13         5288  
  13         1686  
  72         1248  
102              
103 72         177 foreach my $p (@{ $self->params }) {
  72         181  
104 84 100       447 next unless $p->named;
105 24         138 my $copy = $captured{ $p->name };
106 24 100       157 return unless $p->validate(\$copy, 'quite');
107             }
108              
109 71         389 $self->named(\%captured);
110              
111 71         565 1;
112             }
113              
114             1;
115              
116             __END__