Version 1.02
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize / Callback.pm
CommitLineData
178f8470 1package Catalyst::Action::Deserialize::Callback;
2
3use Moose;
4use namespace::autoclean;
5use Scalar::Util qw(openhandle);
6
7extends 'Catalyst::Action';
8
d0822465 9our $VERSION = '1.02';
178f8470 10$VERSION = eval $VERSION;
11
12sub 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
461;
47