[Catalyst] Re: Catalyst::Action::REST

Garrett Goebel ggoebel at goebel.ws
Mon Nov 27 12:25:18 GMT 2006


On Nov 21, 2006, at 9:44 PM, Garrett Goebel wrote:
> On Nov 21, 2006, at 9:16 AM, Matt S Trout wrote:
>> Garrett Goebel wrote:
>>> I tunneled PUT and DELETE inside a POST via a "_method" body  
>>> parameter, which was automatically unravelled before dispatch  
>>> within an overridden Catalyst::prepare_body_parameters method.
>>
>> Clever. Would you be willing to separate that out as a plugin with  
>> a configurable parameter name? I think that might be well-received  
>> (and actually something that *should* be a plugin for once :)
>
> I'll see what I can do. I'll be off the grid over the Thanksgiving  
> holidays. But I'll take my laptop with me. There's a good chance  
> I'll have the time, but with 4 kids under the age of 10, I can't  
> make any promises ;)

On the ride home from the holidays, I managed to throw together my  
first catalyst plugin module. That was, the easy part. Now I need to  
write the tests and package it up for CPAN consumption.


I'd like to invite criticism and comments now, before changes get  
more painful.

For starters:

Is there a better package name?

Should I be inheriting from Catalyst::Base or Catalyst::Component?  
The documentation says to use the former, but from scanning the code,  
it looks like I only need the latter. On this one, I've stuck with  
the documentation.

Should I be using Class::C3 for ->next::method instead of - 
 >NEXT::method? There are quite a few caveats in the Class::C3  
documentation about not playing well with SUPER, but no mention of  
how well it plays with NEXT.

Should I be using $c->config->{'Catalyst::Plugin::Request::Method'}?  
The documentation seems to indicate this, but the existing plugin  
modules seem to prefer shorter keys. Here I went with the preference  
for shorter keys that seemed evident in the plugin modules I scanned.  
Easy enough to reverse.


package Catalyst::Plugin::Request::Method;
use 5.008;
use strict;
use warnings;
use version; our $VERSION = qv('0.0.1');
use base qw(Catalyst::Base);
use NEXT;

sub setup {
     my $c = shift @_;

     $c->NEXT::setup(@_);

     my $cfg = ( $c->config->{'request_method'} ||= {} );
     $cfg->{tunnel_param} ||= '_method';

     return $cfg;
}

sub prepare_body_parameters {
     my $c   = shift @_;
     my $req = $c->request;

     $c->NEXT::prepare_body_parameters(@_);

     my $tunnel_param = $c->config->{'request_method'}-> 
{'tunnel_param'};

     # Allow POST to masquerade as other request method via  
body_parameter
     if ($req->method eq 'POST' && exists $req->parameters-> 
{$tunnel_param}) {
         my $request_method = $req->parameters->{$tunnel_param};
         $req->method($request_method);
         delete $req->parameters->{$tunnel_param};
     }

     1;
}

1;

__END__





More information about the Catalyst mailing list