X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FController%2FREST.pm;h=12568f7eab8f56b0690d0ac65b66b343c711174a;hb=43e4baa3655de11012897d060a6169dc81b0b4ec;hp=6e9bb8abdbb8090d4552a8d3f72be2223e36dd18;hpb=7b46eb07da55c2b74c5a866f68cf9058f0e61077;p=catagits%2FCatalyst-Action-REST.git diff --git a/lib/Catalyst/Controller/REST.pm b/lib/Catalyst/Controller/REST.pm index 6e9bb8a..12568f7 100644 --- a/lib/Catalyst/Controller/REST.pm +++ b/lib/Catalyst/Controller/REST.pm @@ -1,10 +1,8 @@ package Catalyst::Controller::REST; + use Moose; use namespace::autoclean; -our $VERSION = '1.06'; -$VERSION = eval $VERSION; - =head1 NAME Catalyst::Controller::REST - A RESTful controller @@ -149,11 +147,22 @@ L installed. The C content type is supported but is deprecated and you will receive warnings in your log. You can also add a hash in your controller config to pass options to the json object. +There are two options. C are used when decoding incoming JSON, and C +is used when encoding JSON for output. + For instance, to relax permissions when deserializing input, add: + __PACKAGE__->config( json_options => { relaxed => 1 } ) +To indent the JSON output so it becomes more human readable, add: + + __PACKAGE__->config( + json_options_encode => { indent => 1 } + ) + + =item * C => C If a callback=? parameter is passed, this returns javascript in the form of: $callback($serializedJSON); @@ -291,16 +300,9 @@ __PACKAGE__->mk_accessors(qw(serialize)); __PACKAGE__->config( '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' ], - 'text/x-data-taxi' => [ 'Data::Serializer', 'Data::Taxi' ], - 'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ], - 'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ], }, ); @@ -569,6 +571,79 @@ sub status_gone { return 1; } +=item status_see_other + +Returns a "303 See Other" response. Takes an optional "entity" to serialize, +and a "location" where the client should redirect to. + +Example: + + $self->status_see_other( + $c, + location => $some_other_url, + entity => { + radiohead => "Is a good band!", + } + ); + +=cut + +sub status_see_other { + my $self = shift; + my $c = shift; + my %p = Params::Validate::validate( + @_, + { + location => { type => SCALAR | OBJECT }, + entity => { optional => 1 }, + }, + ); + + $c->response->status(303); + $c->response->header( 'Location' => $p{location} ); + $self->_set_entity( $c, $p{'entity'} ); + return 1; +} + +=item status_moved + +Returns a "301 MOVED" response. Takes an "entity" to serialize, and a +"location" where the created object can be found. + +Example: + + $self->status_moved( + $c, + location => '/somewhere/else', + entity => { + radiohead => "Is a good band!", + }, + ); + +=cut + +sub status_moved { + my $self = shift; + my $c = shift; + my %p = Params::Validate::validate( + @_, + { + location => { type => SCALAR | OBJECT }, + entity => { optional => 1 }, + }, + ); + + my $location = ref $p{location} + ? $p{location}->as_string + : $p{location} + ; + + $c->response->status(301); + $c->response->header( Location => $location ); + $self->_set_entity($c, $p{entity}); + return 1; +} + sub _set_entity { my $self = shift; my $c = shift;