File Coverage

blib/lib/CGI/Application/Plugin/DeclareREST.pm
Criterion Covered Total %
statement 51 67 76.1
branch 3 6 50.0
condition 6 24 25.0
subroutine 12 16 75.0
pod 9 9 100.0
total 81 122 66.3


line stmt bran cond sub pod time code
1 2     2   46947 use strict;
  2         6  
  2         68  
2 2     2   10 use warnings;
  2         3  
  2         104  
3             package CGI::Application::Plugin::DeclareREST;
4             {
5             $CGI::Application::Plugin::DeclareREST::VERSION = '0.01';
6             }
7             # ABSTRACT: Declare RESTful API for CGI::Application
8              
9 2     2   9 use Exporter;
  2         7  
  2         87  
10 2     2   1890 use REST::Utils qw( request_method );
  2         3994  
  2         122  
11 2     2   3761 use Routes::Tiny 0.11;
  2         9007  
  2         2158  
12              
13             our @ISA = qw( Exporter );
14             our @EXPORT = qw(
15             get post del put patch any
16             match captures
17             add_route
18             );
19              
20             our %EXPORT_TAGS = (
21             http_methods => [qw( get post del put patch )],
22             );
23              
24             our %routes;
25              
26              
27             sub add_route {
28 0     0 1 0 my $self = shift;
29 0         0 my $class = ref $self;
30            
31 0   0     0 my $router = $routes{ $class } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
32 0         0 return $router->add_route( @_ );
33             }
34              
35              
36             sub get {
37 6     6 1 854 my $sub = pop;
38 6         15 my ($path, %args) = @_;
39            
40 6         14 my $caller = caller();
41 6   66     31 my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
42 6         49 return $router->add_route($path, method => 'get', name => $sub, %args);
43             }
44              
45              
46             sub post {
47 1     1 1 249 my $sub = pop;
48 1         3 my ($path, %args) = @_;
49            
50 1         3 my $caller = caller();
51 1   33     6 my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
52 1         6 return $router->add_route($path, method => 'post', name => $sub, %args );
53             }
54              
55              
56             sub del {
57 1     1 1 119 my $sub = pop;
58 1         3 my ($path, %args) = @_;
59            
60 1         3 my $caller = caller();
61 1   33     8 my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
62 1         6 return $router->add_route($path, method => 'delete', name => $sub, %args );
63             }
64              
65              
66             sub put {
67 0     0 1 0 my $sub = pop;
68 0         0 my ($path, %args) = @_;
69            
70 0         0 my $caller = caller();
71 0   0     0 my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
72 0         0 return $router->add_route($path, method => 'put', name => $sub, %args );
73             }
74              
75              
76             sub patch {
77 0     0 1 0 my $sub = pop;
78 0         0 my ($path, %args) = @_;
79            
80 0         0 my $caller = caller();
81 0   0     0 my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
82 0         0 return $router->add_route($path, method => 'patch', name => $sub, %args );
83             }
84              
85              
86             sub any {
87 1     1 1 207 my $sub = pop;
88 1         5 my ($methods, $path, %args) = @_;
89            
90 1         2 my $caller = caller();
91 1   33     18 my $router = $routes{ $caller } ||= Routes::Tiny->new( strict_trailing_slash => 0 );
92 1         6 return $router->add_route($path, method => $methods, name => $sub, %args);
93             }
94              
95             sub import {
96 2     2   27 my $caller = caller;
97            
98 2         25 $caller->add_callback('prerun', \&_routes_prerun);
99 2         642 goto &Exporter::import;
100             }
101              
102             sub _routes_prerun {
103 10     10   145362 my $self = shift;
104 10   33     65 my $class = ref $self || $self;
105              
106 10 50       41 if(defined $self->query->param( $self->mode_param )) {
107             # OK, we got the query param, to select the right runmode:
108             # We should passthrough to allow normal behaviour
109 0         0 return;
110             }
111              
112 10         362 my $method = request_method($self->query);
113              
114 10         7390 my $r = $routes{$class};
115 10 50       41 if($r) {
116 10         45 my $match = $r->match($self->query->path_info, method => $method );
117 10 50       5828 if($match) {
118 10         45 $self->run_modes( $match->name => $match->name );
119 10         405 $self->prerun_mode( $match->name );
120 10         254 $self->{__MATCH} = $match;
121 10         33 return;
122             }
123             }
124             }
125              
126              
127             sub match {
128 0     0 1 0 (shift)->{__MATCH};
129             }
130              
131              
132             sub captures {
133 8     8 1 641 (shift)->{__MATCH}->captures;
134             }
135              
136             1;
137              
138             __END__