File Coverage

blib/lib/Puncheur/Dispatcher/Lite.pm
Criterion Covered Total %
statement 46 62 74.1
branch 9 24 37.5
condition n/a
subroutine 10 11 90.9
pod n/a
total 65 97 67.0


line stmt bran cond sub pod time code
1             package Puncheur::Dispatcher::Lite;
2 1     1   6 use strict;
  1         1  
  1         29  
3 1     1   6 use warnings;
  1         3  
  1         28  
4 1     1   807 use Router::Boom::Method;
  1         18762  
  1         77  
5              
6             sub import {
7 1     1   3 my $class = shift;
8 1         3 my $caller = caller(0);
9              
10              
11 1         11 my $router = Router::Boom::Method->new;
12 1         18 my $base;
13              
14 1     1   16 no strict 'refs';
  1         2  
  1         110  
15 1     0   5 *{"${caller}::base"} = sub { $base = $_[0] };
  1         8  
  0         0  
16              
17             # copied from Amon2::Web::Dispatcher::RouterBoom
18             # functions
19             #
20             # get( '/path', 'Controller#action')
21             # post('/path', 'Controller#action')
22             # delete_('/path', 'Controller#action')
23             # any( '/path', 'Controller#action')
24             # get( '/path', sub { })
25             # post('/path', sub { })
26             # delete_('/path', sub { })
27             # any( '/path', sub { })
28 1         3 for my $method (qw(get post delete_ any)) {
29 4         24 *{"${caller}::${method}"} = sub {
30 1     1   6 use strict 'refs';
  1         2  
  1         522  
31 2     2   5 my ($path, $dest) = @_;
32              
33 2         3 my %dest;
34 2 50       9 if (ref $dest eq 'CODE') {
35 2         5 $dest{code} = $dest;
36             }
37             else {
38 0         0 my ($controller, $method) = split('#', $dest);
39 0 0       0 $dest{class} = $base ? "${base}::${controller}" : $controller;
40 0 0       0 $dest{method} = $method if defined $method;
41             }
42              
43 2         3 my $http_method;
44 2 50       11 if ($method eq 'get') {
    50          
    50          
45 0         0 $http_method = ['GET','HEAD'];
46             } elsif ($method eq 'post') {
47 0         0 $http_method = 'POST';
48             } elsif ($method eq 'delete_') {
49 0         0 $http_method = 'DELETE';
50             }
51              
52 2         11 $router->add($http_method, $path, \%dest);
53 4         33 };
54             }
55              
56             # class methods
57 1     2   4 *{"${caller}::router"} = sub { $router };
  1         6  
  2         19  
58              
59 1         244 *{"${caller}::dispatch"} = sub {
60 2     2   4 my ($class, $c) = @_;
        2      
61 2 50       9 $c = $class unless $c;
62              
63 2         14 my $env = $c->request->env;
64 2 50       20 if (my ($dest, $captured, $method_not_allowed) = $class->router->match($env->{REQUEST_METHOD}, $env->{PATH_INFO})) {
65 2 50       910 if ($method_not_allowed) {
66 0         0 return $c->res_405;
67             }
68              
69 2         5 my $res = eval {
70 2 50       10 if ($dest->{code}) {
71 2         12 return $dest->{code}->($c, $captured);
72             } else {
73 0         0 my $method = $dest->{method};
74 0         0 $c->{args} = $captured;
75 0         0 return $dest->{class}->$method($c, $captured);
76             }
77             };
78 2 50       11 if ($@) {
79 0 0       0 if ($class->can('handle_exception')) {
80 0         0 return $class->handle_exception($c, $@);
81             }
82             else {
83 0         0 print STDERR "$env->{REQUEST_METHOD} $env->{PATH_INFO} [$env->{HTTP_USER_AGENT}]: $@";
84 0         0 return $c->res_500;
85             }
86             }
87 2         13 return $res;
88             }
89             else {
90 0           return $c->res_404;
91             }
92 1         6 };
93             }
94              
95             1;