Commit | Line | Data |
256c894f |
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'; |
7ad87df9 |
14 | use Class::Inspector; |
256c894f |
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)) { |
256c894f |
22 | return $controller->$method($c); |
23 | } else { |
7ad87df9 |
24 | $self->_return_405($c); |
256c894f |
25 | return $c->execute( $self->class, $self ); |
26 | } |
27 | } |
28 | |
7ad87df9 |
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 | |
256c894f |
47 | 1; |