File Coverage

blib/lib/Router/Boom/Method.pm
Criterion Covered Total %
statement 57 60 95.0
branch 12 12 100.0
condition 5 9 55.5
subroutine 11 12 91.6
pod 5 5 100.0
total 90 98 91.8


line stmt bran cond sub pod time code
1             package Router::Boom::Method;
2 1     1   20855 use strict;
  1         1  
  1         22  
3 1     1   3 use warnings;
  1         1  
  1         16  
4 1     1   3 use utf8;
  1         0  
  1         4  
5 1     1   25 use 5.008_005;
  1         3  
6              
7 1     1   283 use Router::Boom;
  1         1  
  1         368  
8              
9             our $VERSION = "1.03";
10              
11             sub new {
12 1     1 1 6 my $class = shift;
13 1         1 my $self = bless {}, $class;
14 1         2 return $self;
15             }
16              
17             sub add {
18 5     5 1 12 my ($self, $method, $path, $opaque) = @_;
19              
20 5         7 delete $self->{router}; # clear cache
21              
22 5 100 100     15 if (defined($method) && !ref($method)) {
23 3         4 $method = [$method];
24             }
25              
26 5 100       10 unless ($self->{path_seen}->{$path}++) {
27 4         3 push @{$self->{path}}, $path;
  4         5  
28             }
29 5         2 push @{$self->{data}->{$path}}, [$method, $opaque];
  5         11  
30             }
31              
32             sub routes {
33 1     1 1 1572 my $self = shift;
34              
35 1         1 my @routes;
36 1         2 for my $path (@{$self->{path}}) {
  1         3  
37 4         3 for my $route (@{$self->{data}->{$path}}) {
  4         6  
38 5         5 my ($method, $opaque) = @$route;
39 5         9 push @routes, [ $method, $path, $opaque ];
40             }
41             }
42 1         7 return @routes;
43             }
44              
45             sub _method_match {
46 11     11   9 my ($request_method, $matcher) = @_;
47 11 100       16 return 1 if not defined($matcher);
48              
49 10         9 for my $m (@$matcher) {
50 12 100       28 return 1 if $m eq $request_method;
51             }
52 5         9 return 0;
53             }
54              
55             sub match {
56 10     10 1 21510 my ($self, $request_method, $path) = @_;
57              
58 10   66     27 $self->{router} ||= $self->_build_router();
59              
60 10 100       22 if (my ($patterns, $captured) = $self->{router}->match($path)) {
61 9         4 my @allowed_methods;
62 9         14 for my $pattern (@$patterns) {
63 11 100       14 if (_method_match($request_method, $pattern->[0])) {
64 6         15 return ($pattern->[1], $captured, 0, []);
65             }
66 5         5 push @allowed_methods, @{$pattern->[0]};
  5         8  
67             }
68 3         7 return (undef, undef, 1, \@allowed_methods);
69             } else {
70 1         3 return;
71             }
72             }
73              
74             sub regexp {
75 0     0 1 0 my $self = shift;
76 0   0     0 $self->{router} ||= $self->_build_router();
77 0         0 $self->{router}->regexp;
78             }
79              
80             sub _build_router {
81 1     1   2 my ($self) = @_;
82 1         3 my $router = Router::Boom->new();
83 1         1 for my $path (@{$self->{path}}) {
  1         2  
84 4         8 $router->add($path, $self->{data}->{$path});
85             }
86 1         3 $router;
87             }
88              
89             1;
90             __END__