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