put response methods on the response
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / ActionRole / Deserialize.pm
CommitLineData
b153f31c 1package Catalyst::ActionRole::Deserialize;
2use Moose::Role;
3use Moose::Util qw(does_role);
4use Catalyst::RequestRole::Deserialize;
5use namespace::clean -except => 'meta';
6
7requires 'deserialize';
8
9around execute => sub {
10 my $next = shift;
11 my ($self, $controller, $c, $arg) = @_;
12
13 Catalyst::RequestRole::Deserialize->meta->apply($c->request)
14 unless does_role($c->request, 'Catalyst::RequestRole::Deserialize');
15
16 my $content = "";
17 my $body = $c->request->body;
18 if ($body) {
19 local $_;
20 while (<$body>) { $content .= $_ }
21 }
22
23 if ($content) {
24 my $data = eval {
25 $self->deserialize(
26 $content,
27 $c,
28 $arg,
29 )
30 };
31 return $@ if $@;
32 $c->request->_set_data($data);
33 } else {
34 $c->debug && $c->log->debug(
35 'I would have deserialized, but there was nothing in the body!'
36 );
37 }
38 return 1;
39};
40
411;