remove hardcoded version strings
[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 # VERSION
10
11 sub execute {
12     my $self = shift;
13     my ( $controller, $c, $callbacks ) = @_;
14
15     my $rbody;
16
17     # could be a string or a FH
18     if ( my $body = $c->request->body ) {
19         if(openhandle $body) {
20             seek($body, 0, 0); # in case something has already read from it
21             while ( defined( my $line = <$body> ) ) {
22                 $rbody .= $line;
23             }
24         } else {
25             $rbody = $body;
26         }
27     }
28
29     if ( $rbody ) {
30         my $rdata = eval { $callbacks->{deserialize}->( $rbody, $controller, $c ) };
31         if ($@) {
32             return $@;
33         }
34         $c->request->data($rdata);
35     } else {
36         $c->log->debug(
37             'I would have deserialized, but there was nothing in the body!')
38             if $c->debug;
39     }
40     return 1;
41 }
42
43 __PACKAGE__->meta->make_immutable;
44
45 1;
46