Initial commit of Catalyst-Action-REST
adam [Sat, 14 Oct 2006 01:01:57 +0000 (01:01 +0000)]
TODO [new file with mode: 0644]
lib/Catalyst/Action/Deserialize/YAML.pm [new file with mode: 0644]
lib/Catalyst/Action/REST.pm [new file with mode: 0644]
lib/Catalyst/Action/Serialize/YAML.pm [new file with mode: 0644]
lib/Catalyst/Controller/REST.pm [new file with mode: 0644]
lib/Catalyst/Request/REST.pm [new file with mode: 0644]

diff --git a/TODO b/TODO
new file mode 100644 (file)
index 0000000..572dd3d
--- /dev/null
+++ b/TODO
@@ -0,0 +1,10 @@
+* Override setup_classes from Catalyst::Base, so things that use 
+  C::Controller:REST don't need to have ActionClass('REST') on them
+
+* Create generalized serializer/deserializer, that uses content_type to
+  choose
+
+* Document everything
+
+* More tests
+
diff --git a/lib/Catalyst/Action/Deserialize/YAML.pm b/lib/Catalyst/Action/Deserialize/YAML.pm
new file mode 100644 (file)
index 0000000..56ecb52
--- /dev/null
@@ -0,0 +1,32 @@
+#
+# Catlyst::Action::Deserialize::YAML.pm
+# Created by: Adam Jacob, Marchex, <adam@marchex.com>
+# Created on: 10/12/2006 03:00:32 PM PDT
+#
+# $Id$
+
+package Catalyst::Action::Deserialize::YAML;
+
+use strict;
+use warnings;
+
+use base 'Catalyst::Action';
+use YAML::Syck;
+use Catalyst::Request::REST;
+
+sub execute {
+    my $self = shift;
+    my ( $controller, $c, $test ) = @_;
+   
+    my $nreq = bless($c->request, 'Catalyst::Request::REST');
+    $c->request($nreq);
+    if ($c->request->method eq "POST" || $c->request->method eq "PUT") {
+        my $rdata = LoadFile($c->request->body);
+        $c->request->data($rdata);
+        $self->NEXT::execute( @_, );
+    } else {
+        $self->NEXT::execute( @_ );
+    }
+};
+
+1;
diff --git a/lib/Catalyst/Action/REST.pm b/lib/Catalyst/Action/REST.pm
new file mode 100644 (file)
index 0000000..a2f3f41
--- /dev/null
@@ -0,0 +1,29 @@
+#
+# REST.pm
+# Created by: Adam Jacob, Marchex, <adam@marchex.com>
+# Created on: 10/12/2006 03:00:32 PM PDT
+#
+# $Id$
+
+package Catalyst::Action::REST;
+
+use strict;
+use warnings;
+
+use base 'Catalyst::Action';
+
+sub dispatch {
+    my ( $self, $c ) = @_;
+
+    my $controller = $self->class;
+    my $method = $self->name . "_" . uc($c->request->method);
+    if ($controller->can($method)) {
+        $c->log->debug("REST ActionClass is calling $method");
+        return $controller->$method($c);
+    } else {
+        $c->log->debug("REST ActionClass is calling " . $self->name);
+        return $c->execute( $self->class, $self );
+    }
+}
+
+1;
diff --git a/lib/Catalyst/Action/Serialize/YAML.pm b/lib/Catalyst/Action/Serialize/YAML.pm
new file mode 100644 (file)
index 0000000..fbcb4f8
--- /dev/null
@@ -0,0 +1,34 @@
+#
+# Catlyst::Action::Serialize::YAML.pm
+# Created by: Adam Jacob, Marchex, <adam@marchex.com>
+# Created on: 10/12/2006 03:00:32 PM PDT
+#
+# $Id$
+
+package Catalyst::Action::Serialize::YAML;
+
+use strict;
+use warnings;
+
+use base 'Catalyst::Action';
+use YAML::Syck;
+
+sub execute {
+    my $self = shift;
+    my ( $controller, $c, $test ) = @_;
+
+    my $stash_key = $controller->serialize->{'stash_key'} || 'rest';
+  
+    if (! $c->response->content_type ) {
+        $c->response->content_type($c->req->content_type);
+    }
+    return 1 if $c->req->method eq 'HEAD';
+    return 1 if length( $c->response->body );
+    return 1 if scalar @{ $c->error };
+    return 1 if $c->response->status =~ /^(?:204|3\d\d)$/;
+
+    $c->response->output( Dump( $c->stash->{$stash_key} ) );
+    return 1;
+};
+
+1;
diff --git a/lib/Catalyst/Controller/REST.pm b/lib/Catalyst/Controller/REST.pm
new file mode 100644 (file)
index 0000000..22535e4
--- /dev/null
@@ -0,0 +1,19 @@
+package Catalyst::Controller::REST;
+
+use strict;
+use warnings;
+use base 'Catalyst::Controller';
+
+__PACKAGE__->mk_accessors(qw(serialize));
+
+__PACKAGE__->config(
+    serialize => {
+        'stash_key' => 'rest',
+    }
+);
+
+sub begin :ActionClass('Deserialize::YAML') {}
+
+sub end :ActionClass('Serialize::YAML') {}
+
+1;
diff --git a/lib/Catalyst/Request/REST.pm b/lib/Catalyst/Request/REST.pm
new file mode 100644 (file)
index 0000000..9b81c62
--- /dev/null
@@ -0,0 +1,18 @@
+#
+# REST.pm
+# Created by: Adam Jacob, Marchex, <adam@marchex.com>
+# Created on: 10/13/2006 03:54:33 PM PDT
+#
+# $Id: $
+
+package Catalyst::Request::REST;
+
+use strict;
+use warnings;
+
+use base 'Catalyst::Request';
+
+__PACKAGE__->mk_accessors(qw(data));
+
+1;
+