Added status_created helper method
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Controller / REST.pm
CommitLineData
256c894f 1package Catalyst::Controller::REST;
2
3use strict;
4use warnings;
5use base 'Catalyst::Controller';
5511d1ff 6use Params::Validate qw(:all);
256c894f 7
8__PACKAGE__->mk_accessors(qw(serialize));
9
10__PACKAGE__->config(
11 serialize => {
eccb2137 12 'default' => 'YAML',
256c894f 13 'stash_key' => 'rest',
eccb2137 14 'map' => {
15 'text/x-yaml' => 'YAML',
7ad87df9 16 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
17 },
256c894f 18 }
19);
20
5511d1ff 21sub begin : ActionClass('Deserialize') {}
22
23sub end : ActionClass('Serialize') { }
24
25sub status_created {
26 my $self = shift;
27 my $c = shift;
28 my %p = validate(@_,
29 {
30 location => { type => SCALAR | OBJECT },
31 entity => { optional => 1 },
32 },
33 );
256c894f 34
5511d1ff 35 my $location;
36 if (ref($p{'location'})) {
37 $location = $p{'location'}->as_string;
38 }
39 $c->response->status(201);
40 $c->response->header('Location' => $location);
41 if (exists($p{'entity'})) {
42 $c->stash->{$self->config->{'serialize'}->{'stash_key'}} = $p{'entity'};
43 }
44 return 1;
eccb2137 45}
256c894f 46
471;