Added status_created helper method
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Controller / REST.pm
1 package Catalyst::Controller::REST;
2
3 use strict;
4 use warnings;
5 use base 'Catalyst::Controller';
6 use Params::Validate qw(:all);
7
8 __PACKAGE__->mk_accessors(qw(serialize));
9
10 __PACKAGE__->config(
11     serialize => {
12         'default'   => 'YAML',
13         'stash_key' => 'rest',
14         'map'       => {
15             'text/x-yaml'        => 'YAML',
16             'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
17         },
18     }
19 );
20
21 sub begin : ActionClass('Deserialize') {}
22
23 sub end : ActionClass('Serialize') { }
24
25 sub 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     );
34
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;
45 }
46
47 1;