File Coverage

blib/lib/Plack/Middleware/REST.pm
Criterion Covered Total %
statement 39 39 100.0
branch 10 14 71.4
condition 3 13 23.0
subroutine 8 8 100.0
pod 2 2 100.0
total 62 76 81.5


line stmt bran cond sub pod time code
1             package Plack::Middleware::REST;
2             {
3             $Plack::Middleware::REST::VERSION = '0.03';
4             }
5             #ABSTRACT: Route PSGI requests for RESTful web applications
6 2     2   75167 use strict;
  2         5  
  2         75  
7 2     2   12 use warnings;
  2         3  
  2         61  
8              
9 2     2   9 use Carp qw(croak);
  2         9  
  2         127  
10 2     2   12 use Scalar::Util qw(reftype);
  2         3  
  2         212  
11              
12 2     2   710 use parent 'Plack::Middleware';
  2         264  
  2         10  
13 2     2   16912 use Plack::Util::Accessor qw(get create upsert delete list pass_through);
  2         4  
  2         14  
14              
15             our %METHOD = (
16             resource => {
17             GET => 'get',
18             PUT => 'upsert',
19             DELETE => 'delete',
20             },
21             collection => {
22             GET => 'list',
23             POST => 'create',
24             },
25             );
26              
27             sub prepare_app {
28 1     1 1 220 my ($self) = @_;
29              
30 1 50       6 $self->pass_through(0)
31             unless defined $self->pass_through;
32              
33 1         109 my @actions = qw(get create upsert delete list);
34 1         3 foreach my $action (@actions) {
35 5         9 my $app = $self->{$action};
36              
37             # alias
38 5 50 33     25 $self->{$action} = $self->{$app} if $app and !ref $app;
39              
40 5 50 0     17 croak "PSGI application '$action' must be code reference"
      33        
41             if $self->{action} and (reftype($self->{$action}) || '') ne 'CODE';
42             }
43              
44 1         7 while (my ($type,$method) = each %METHOD) {
45 2         7 my @allow = sort grep { $self->{ $method->{$_} } } keys %$method;
  5         18  
46 2         16 $self->{allow}->{$type} = \@allow;
47             }
48             }
49              
50             sub call {
51 11     11 1 36119 my ($self, $env) = @_;
52              
53 11 100 50     46 my $type = ($env->{PATH_INFO} || '/') eq '/'
54             ? 'collection' : 'resource';
55              
56 11         29 my $method = $METHOD{ $type }->{ $env->{REQUEST_METHOD} };
57              
58 11 100       29 my $app = $method ? $self->{ $method } : undef;
59              
60 11 50 0     31 $app ||= $self->{app} if $self->pass_through;
61              
62 11 100       66 if ( $app ) {
63 9         23 $app->($env);
64             } else {
65 2         5 my $allow = join ', ', @{ $self->{allow}->{$type} };
  2         9  
66 2         16 [ 405, [ Allow => $allow ], ['Method Not Allowed'] ];
67             }
68             }
69              
70             1;
71              
72             __END__