put response methods on the response
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / ActionRole / DeserializeFormat.pm
1 package Catalyst::ActionRole::DeserializeFormat;
2 use Moose::Role;
3 use Moose::Util qw(does_role);
4 use Catalyst::RequestRole::Deserialize;
5 use namespace::clean -except => 'meta';
6
7 requires 'deserialize';
8
9 around 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
41 1;