X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FAction%2FREST.pm;h=d04fa0d4bd7860ecb30f530c15524b05101dc112;hb=eccb21379de1fbdc65740e0cb4158d3b4d055cab;hp=a2f3f415ca4a23b5936e98091d8e5f75100aff15;hpb=256c894fcf95e1a0716676afb8f5732854734672;p=catagits%2FCatalyst-Action-REST.git diff --git a/lib/Catalyst/Action/REST.pm b/lib/Catalyst/Action/REST.pm index a2f3f41..d04fa0d 100644 --- a/lib/Catalyst/Action/REST.pm +++ b/lib/Catalyst/Action/REST.pm @@ -11,19 +11,40 @@ use strict; use warnings; use base 'Catalyst::Action'; +use Class::Inspector; sub dispatch { my ( $self, $c ) = @_; my $controller = $self->class; - my $method = $self->name . "_" . uc($c->request->method); - if ($controller->can($method)) { - $c->log->debug("REST ActionClass is calling $method"); + my $method = $self->name . "_" . uc( $c->request->method ); + if ( $controller->can($method) ) { return $controller->$method($c); } else { - $c->log->debug("REST ActionClass is calling " . $self->name); + $self->_return_405($c); return $c->execute( $self->class, $self ); } } +sub _return_405 { + my ( $self, $c ) = @_; + + my $controller = $self->class; + my $methods = Class::Inspector->methods($controller); + my @allowed; + foreach my $method ( @{$methods} ) { + my $name = $self->name; + if ( $method =~ /^$name\_(.+)$/ ) { + push( @allowed, $1 ); + } + } + $c->response->content_type('text/plain'); + $c->response->status(405); + $c->response->header( 'Allow' => \@allowed ); + $c->response->body( "Method " + . $c->request->method + . " not implemented for " + . $c->uri_for( $self->reverse ) ); +} + 1;