Added Test Suite
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / REST.pm
1 #
2 # REST.pm
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
4 # Created on: 10/12/2006 03:00:32 PM PDT
5 #
6 # $Id$
7
8 package Catalyst::Action::REST;
9
10 use strict;
11 use warnings;
12
13 use base 'Catalyst::Action';
14 use Class::Inspector;
15
16 sub dispatch {
17     my ( $self, $c ) = @_;
18
19     my $controller = $self->class;
20     my $method = $self->name . "_" . uc($c->request->method);
21     if ($controller->can($method)) {
22         return $controller->$method($c);
23     } else {
24         $self->_return_405($c);
25         return $c->execute( $self->class, $self );
26     }
27 }
28
29 sub _return_405 {
30     my ( $self, $c ) = @_;
31
32     my $controller = $self->class;
33     my $methods = Class::Inspector->methods($controller);
34     my @allowed;
35     foreach my $method (@{$methods}) {
36         my $name = $self->name;
37         if ($method =~ /^$name\_(.+)$/) {
38             push(@allowed, $1);
39         }
40     }
41     $c->response->content_type('text/plain');
42     $c->response->status(405);
43     $c->response->header('Allow' => \@allowed);
44     $c->response->body("Method " . $c->request->method . " not implemented for " . $c->uri_for($self->reverse));
45 }
46
47 1;