File Coverage

blib/lib/Router/Boom/Method.pm
Criterion Covered Total %
statement 55 58 94.8
branch 12 12 100.0
condition 5 9 55.5
subroutine 11 12 91.6
pod 5 5 100.0
total 88 96 91.6


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