Update to 0.62, updated Module::Install
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / REST.pm
CommitLineData
256c894f 1#
2# REST.pm
be3c588a 3# Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
256c894f 4# Created on: 10/12/2006 03:00:32 PM PDT
5#
6# $Id$
7
8package Catalyst::Action::REST;
9
10use strict;
11use warnings;
12
13use base 'Catalyst::Action';
7ad87df9 14use Class::Inspector;
d4611771 15use Catalyst;
9a76221e 16use Catalyst::Request::REST;
7328f0ab 17use 5.8.1;
256c894f 18
d34c067a 19our
df26b361 20 $VERSION = '0.62';
9a76221e 21
22# This is wrong in several ways. First, there's no guarantee that
23# Catalyst.pm has not been subclassed. Two, there's no guarantee that
24# the user isn't already using their request subclass.
25Catalyst->request_class('Catalyst::Request::REST')
2f91bf68 26 unless Catalyst->request_class->isa('Catalyst::Request::REST');
7328f0ab 27
28=head1 NAME
29
30Catalyst::Action::REST - Automated REST Method Dispatching
31
32=head1 SYNOPSIS
33
5e87ec47 34 sub foo :Local :ActionClass('REST') {
35 ... do setup for HTTP method specific handlers ...
36 }
7328f0ab 37
38 sub foo_GET {
39 ... do something for GET requests ...
40 }
41
42 sub foo_PUT {
43 ... do somethign for PUT requests ...
44 }
45
46=head1 DESCRIPTION
47
48This Action handles doing automatic method dispatching for REST requests. It
49takes a normal Catalyst action, and changes the dispatch to append an
50underscore and method name.
51
52For example, in the synopsis above, calling GET on "/foo" would result in
53the foo_GET method being dispatched.
54
55If a method is requested that is not implemented, this action will
56return a status 405 (Method Not Found). It will populate the "Allow" header
5e87ec47 57with the list of implemented request methods. You can override this behavior
58by implementing a custom 405 handler like so:
59
60 sub foo_not_implemented {
61 ... handle not implemented methods ...
62 }
63
64If you do not provide an _OPTIONS subroutine, we will automatically respond
65with a 200 OK. The "Allow" header will be populated with the list of
66implemented request methods.
7328f0ab 67
5e87ec47 68It is likely that you really want to look at L<Catalyst::Controller::REST>,
69which brings this class together with automatic Serialization of requests
70and responses.
398c5a1b 71
9a76221e 72When you use this module, the request class will be changed to
73L<Catalyst::Request::REST>.
74
7328f0ab 75=head1 METHODS
76
77=over 4
78
79=item dispatch
80
81This method overrides the default dispatch mechanism to the re-dispatching
82mechanism described above.
83
84=cut
d34c067a 85
256c894f 86sub dispatch {
bb4130f6 87 my $self = shift;
d34c067a 88 my $c = shift;
256c894f 89
2f91bf68 90 my $controller = $c->component( $self->class );
eccb2137 91 my $method = $self->name . "_" . uc( $c->request->method );
92 if ( $controller->can($method) ) {
d34c067a 93 $c->execute( $self->class, $self, @{ $c->req->args } );
94 return $controller->$method( $c, @{ $c->req->args } );
256c894f 95 } else {
d34c067a 96 if ( $c->request->method eq "OPTIONS" ) {
97 return $self->_return_options($c);
98 } else {
99 my $handle_ni = $self->name . "_not_implemented";
100 if ( $controller->can($handle_ni) ) {
101 return $controller->$handle_ni( $c, @{ $c->req->args } );
102 } else {
103 return $self->_return_not_implemented($c);
104 }
105 }
256c894f 106 }
107}
108
d34c067a 109sub _return_options {
110 my ( $self, $c ) = @_;
111
112 my @allowed = $self->_get_allowed_methods($c);
113 $c->response->content_type('text/plain');
114 $c->response->status(200);
115 $c->response->header( 'Allow' => \@allowed );
116}
117
118sub _get_allowed_methods {
7ad87df9 119 my ( $self, $c ) = @_;
120
121 my $controller = $self->class;
eccb2137 122 my $methods = Class::Inspector->methods($controller);
7ad87df9 123 my @allowed;
eccb2137 124 foreach my $method ( @{$methods} ) {
7ad87df9 125 my $name = $self->name;
eccb2137 126 if ( $method =~ /^$name\_(.+)$/ ) {
127 push( @allowed, $1 );
7ad87df9 128 }
129 }
d34c067a 130 return @allowed;
131}
132
133sub _return_not_implemented {
134 my ( $self, $c ) = @_;
135
136 my @allowed = $self->_get_allowed_methods($c);
7ad87df9 137 $c->response->content_type('text/plain');
138 $c->response->status(405);
eccb2137 139 $c->response->header( 'Allow' => \@allowed );
140 $c->response->body( "Method "
141 . $c->request->method
142 . " not implemented for "
143 . $c->uri_for( $self->reverse ) );
7ad87df9 144}
145
256c894f 1461;
7328f0ab 147
148=back
149
150=head1 SEE ALSO
151
152You likely want to look at L<Catalyst::Controller::REST>, which implements
153a sensible set of defaults for a controller doing REST.
154
155L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize>
156
157=head1 AUTHOR
158
159Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
160
398c5a1b 161Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
162
33e5de96 163=head1 CONTRIBUTERS
164
165Daisuke Maki <daisuke@endeworks.jp>
166
7328f0ab 167=head1 LICENSE
168
169You may distribute this code under the same terms as Perl itself.
170
171=cut
d34c067a 172