Version 1.02
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize / Callback.pm
1 package Catalyst::Action::Deserialize::Callback;
2
3 use Moose;
4 use namespace::autoclean;
5 use Scalar::Util qw(openhandle);
6
7 extends 'Catalyst::Action';
8
9 our $VERSION = '1.02';
10 $VERSION = eval $VERSION;
11
12 sub execute {
13     my $self = shift;
14     my ( $controller, $c, $callbacks ) = @_;
15
16     my $rbody;
17
18     # could be a string or a FH
19     if ( my $body = $c->request->body ) {
20         if(openhandle $body) {
21             seek($body, 0, 0); # in case something has already read from it
22             while ( defined( my $line = <$body> ) ) {
23                 $rbody .= $line;
24             }
25         } else {
26             $rbody = $body;
27         }
28     }
29
30     if ( $rbody ) {
31         my $rdata = eval { $callbacks->{deserialize}->( $rbody, $controller, $c ) };
32         if ($@) {
33             return $@;
34         }
35         $c->request->data($rdata);
36     } else {
37         $c->log->debug(
38             'I would have deserialized, but there was nothing in the body!')
39             if $c->debug;
40     }
41     return 1;
42 }
43
44 __PACKAGE__->meta->make_immutable;
45
46 1;
47