X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FController%2FREST.pm;h=0ce7c44d95775333960fe6e25052fda5cad88a15;hb=832e768d82da10ac48c61848e004fa571ea99bdd;hp=1017cb104ca223c8e77169cccf320b65eab4a670;hpb=e601addaf89882fccbc824c1a53328f0d049b32b;p=catagits%2FCatalyst-Action-REST.git diff --git a/lib/Catalyst/Controller/REST.pm b/lib/Catalyst/Controller/REST.pm index 1017cb1..0ce7c44 100644 --- a/lib/Catalyst/Controller/REST.pm +++ b/lib/Catalyst/Controller/REST.pm @@ -1,5 +1,7 @@ package Catalyst::Controller::REST; +our $VERSION = 0.61; + =head1 NAME Catalyst::Controller::REST - A RESTful controller @@ -158,15 +160,26 @@ you serialize be a HASHREF, we transform outgoing data to be in the form of: { data => $yourdata } +=item L + +Uses a regular Catalyst view. For example, if you wanted to have your +C and C views rendered by TT: + + 'text/html' => [ 'View', 'TT' ], + 'text/xml' => [ 'View', 'XML' ], + +Will do the trick nicely. + =back -By default, L will return a C<415 Unsupported Media Type> response if an attempt to use an unsupported content-type is made. You -can ensure that something is always returned by setting the C config -option: +By default, L will return a C<415 Unsupported Media Type> +response if an attempt to use an unsupported content-type is made. You +can ensure that something is always returned by setting the C +config option: - __PACKAGE__->config->{'serialize'}->{'default'} = 'YAML'; + __PACKAGE__->config->{'default'} = 'text/x-yaml'; -Would make it always fall back to YAML. +Would make it always fall back to the serializer plugin defined for text/x-yaml. Implementing new Serialization formats is easy! Contributions are most welcome! See L and @@ -197,17 +210,17 @@ such require you pass the current context ($c) as the first argument. use strict; use warnings; use base 'Catalyst::Controller'; -use Params::Validate qw(:all); +use Params::Validate qw(SCALAR OBJECT); __PACKAGE__->mk_accessors(qw(serialize)); __PACKAGE__->config( - serialize => { 'stash_key' => 'rest', 'map' => { 'text/html' => 'YAML::HTML', 'text/xml' => 'XML::Simple', 'text/x-yaml' => 'YAML', + 'application/json' => 'JSON', 'text/x-json' => 'JSON', 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], 'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ], @@ -217,7 +230,6 @@ __PACKAGE__->config( 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ], 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ], }, - } ); sub begin : ActionClass('Deserialize') { @@ -244,7 +256,7 @@ Example: sub status_ok { my $self = shift; my $c = shift; - my %p = validate( @_, { entity => 1, }, ); + my %p = Params::Validate::validate( @_, { entity => 1, }, ); $c->response->status(200); $self->_set_entity( $c, $p{'entity'} ); @@ -274,7 +286,7 @@ This is probably what you want for most PUT requests. sub status_created { my $self = shift; my $c = shift; - my %p = validate( + my %p = Params::Validate::validate( @_, { location => { type => SCALAR | OBJECT }, @@ -312,7 +324,7 @@ Example: sub status_accepted { my $self = shift; my $c = shift; - my %p = validate( @_, { entity => 1, }, ); + my %p = Params::Validate::validate( @_, { entity => 1, }, ); $c->response->status(202); $self->_set_entity( $c, $p{'entity'} ); @@ -337,10 +349,10 @@ Example: sub status_bad_request { my $self = shift; my $c = shift; - my %p = validate( @_, { message => { type => SCALAR }, }, ); + my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, ); $c->response->status(400); - $c->log->debug( "Status Bad Request: " . $p{'message'} ); + $c->log->debug( "Status Bad Request: " . $p{'message'} ) if $c->debug; $self->_set_entity( $c, { error => $p{'message'} } ); return 1; } @@ -363,10 +375,10 @@ Example: sub status_not_found { my $self = shift; my $c = shift; - my %p = validate( @_, { message => { type => SCALAR }, }, ); + my %p = Params::Validate::validate( @_, { message => { type => SCALAR }, }, ); $c->response->status(404); - $c->log->debug( "Status Not Found: " . $p{'message'} ); + $c->log->debug( "Status Not Found: " . $p{'message'} ) if $c->debug; $self->_set_entity( $c, { error => $p{'message'} } ); return 1; } @@ -376,7 +388,7 @@ sub _set_entity { my $c = shift; my $entity = shift; if ( defined($entity) ) { - $c->stash->{ $self->config->{'serialize'}->{'stash_key'} } = $entity; + $c->stash->{ $self->{'stash_key'} } = $entity; } return 1; } @@ -406,6 +418,7 @@ This class provides a default configuration for Serialization. It is currently: 'text/html' => 'YAML::HTML', 'text/xml' => 'XML::Simple', 'text/x-yaml' => 'YAML', + 'application/json' => 'JSON', 'text/x-json' => 'JSON', 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ], 'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ], @@ -416,8 +429,7 @@ This class provides a default configuration for Serialization. It is currently: ], 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ] , - 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serializat -ion' ], + 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ], }, } );