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; |
eccb2137 |
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; |
eccb2137 |
33 | my $methods = Class::Inspector->methods($controller); |
7ad87df9 |
34 | my @allowed; |
eccb2137 |
35 | foreach my $method ( @{$methods} ) { |
7ad87df9 |
36 | my $name = $self->name; |
eccb2137 |
37 | if ( $method =~ /^$name\_(.+)$/ ) { |
38 | push( @allowed, $1 ); |
7ad87df9 |
39 | } |
40 | } |
41 | $c->response->content_type('text/plain'); |
42 | $c->response->status(405); |
eccb2137 |
43 | $c->response->header( 'Allow' => \@allowed ); |
44 | $c->response->body( "Method " |
45 | . $c->request->method |
46 | . " not implemented for " |
47 | . $c->uri_for( $self->reverse ) ); |
7ad87df9 |
48 | } |
49 | |
256c894f |
50 | 1; |