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