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; |
7328f0ab |
15 | use 5.8.1; |
256c894f |
16 | |
7328f0ab |
17 | my |
33e5de96 |
18 | $VERSION = '0.2'; |
7328f0ab |
19 | |
20 | =head1 NAME |
21 | |
22 | Catalyst::Action::REST - Automated REST Method Dispatching |
23 | |
24 | =head1 SYNOPSIS |
25 | |
26 | sub foo :Local :ActionClass('REST') {} |
27 | |
28 | sub foo_GET { |
29 | ... do something for GET requests ... |
30 | } |
31 | |
32 | sub foo_PUT { |
33 | ... do somethign for PUT requests ... |
34 | } |
35 | |
36 | =head1 DESCRIPTION |
37 | |
38 | This Action handles doing automatic method dispatching for REST requests. It |
39 | takes a normal Catalyst action, and changes the dispatch to append an |
40 | underscore and method name. |
41 | |
42 | For example, in the synopsis above, calling GET on "/foo" would result in |
43 | the foo_GET method being dispatched. |
44 | |
45 | If a method is requested that is not implemented, this action will |
46 | return a status 405 (Method Not Found). It will populate the "Allow" header |
47 | with the list of implemented request methods. |
48 | |
398c5a1b |
49 | It is likely that you really want to look at L<Catalyst::Controller::REST>. |
50 | |
7328f0ab |
51 | =head1 METHODS |
52 | |
53 | =over 4 |
54 | |
55 | =item dispatch |
56 | |
57 | This method overrides the default dispatch mechanism to the re-dispatching |
58 | mechanism described above. |
59 | |
60 | =cut |
256c894f |
61 | sub dispatch { |
bb4130f6 |
62 | my $self = shift; |
63 | my $c = shift; |
256c894f |
64 | |
65 | my $controller = $self->class; |
eccb2137 |
66 | my $method = $self->name . "_" . uc( $c->request->method ); |
67 | if ( $controller->can($method) ) { |
bb4130f6 |
68 | return $controller->$method($c, @{$c->req->args}); |
256c894f |
69 | } else { |
7ad87df9 |
70 | $self->_return_405($c); |
bb4130f6 |
71 | return $c->execute( $self->class, $self, @{$c->req->args} ); |
256c894f |
72 | } |
73 | } |
74 | |
7ad87df9 |
75 | sub _return_405 { |
76 | my ( $self, $c ) = @_; |
77 | |
78 | my $controller = $self->class; |
eccb2137 |
79 | my $methods = Class::Inspector->methods($controller); |
7ad87df9 |
80 | my @allowed; |
eccb2137 |
81 | foreach my $method ( @{$methods} ) { |
7ad87df9 |
82 | my $name = $self->name; |
eccb2137 |
83 | if ( $method =~ /^$name\_(.+)$/ ) { |
84 | push( @allowed, $1 ); |
7ad87df9 |
85 | } |
86 | } |
87 | $c->response->content_type('text/plain'); |
88 | $c->response->status(405); |
eccb2137 |
89 | $c->response->header( 'Allow' => \@allowed ); |
90 | $c->response->body( "Method " |
91 | . $c->request->method |
92 | . " not implemented for " |
93 | . $c->uri_for( $self->reverse ) ); |
7ad87df9 |
94 | } |
95 | |
256c894f |
96 | 1; |
7328f0ab |
97 | |
98 | =back |
99 | |
100 | =head1 SEE ALSO |
101 | |
102 | You likely want to look at L<Catalyst::Controller::REST>, which implements |
103 | a sensible set of defaults for a controller doing REST. |
104 | |
105 | L<Catalyst::Action::Serialize>, L<Catalyst::Action::Deserialize> |
106 | |
107 | =head1 AUTHOR |
108 | |
109 | Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway |
110 | |
398c5a1b |
111 | Marchex, Inc. paid me while I developed this module. (http://www.marchex.com) |
112 | |
33e5de96 |
113 | =head1 CONTRIBUTERS |
114 | |
115 | Daisuke Maki <daisuke@endeworks.jp> |
116 | |
7328f0ab |
117 | =head1 LICENSE |
118 | |
119 | You may distribute this code under the same terms as Perl itself. |
120 | |
121 | =cut |