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