add a $VERSION to every module
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize.pm
CommitLineData
7ad87df9 1package Catalyst::Action::Deserialize;
2
930013e6 3use Moose;
4use namespace::autoclean;
7ad87df9 5
930013e6 6extends 'Catalyst::Action::SerializeBase';
7ad87df9 7use Module::Pluggable::Object;
def65dcc 8use MRO::Compat;
7ad87df9 9
f465980c 10our $VERSION = '0.81';
11$VERSION = eval $VERSION;
12
7ad87df9 13__PACKAGE__->mk_accessors(qw(plugins));
14
15sub execute {
16 my $self = shift;
e601adda 17 my ( $controller, $c ) = @_;
7ad87df9 18
7ad87df9 19 my @demethods = qw(POST PUT OPTIONS);
eccb2137 20 my $method = $c->request->method;
21 if ( grep /^$method$/, @demethods ) {
9a76221e 22 my ( $sclass, $sarg, $content_type ) =
23 $self->_load_content_plugins( 'Catalyst::Action::Deserialize',
24 $controller, $c );
25 return 1 unless defined($sclass);
e601adda 26 my $rc;
eccb2137 27 if ( defined($sarg) ) {
e601adda 28 $rc = $sclass->execute( $controller, $c, $sarg );
7ad87df9 29 } else {
e601adda 30 $rc = $sclass->execute( $controller, $c );
31 }
9a76221e 32 if ( $rc eq "0" ) {
33 return $self->_unsupported_media_type( $c, $content_type );
34 } elsif ( $rc ne "1" ) {
35 return $self->_serialize_bad_request( $c, $content_type, $rc );
7ad87df9 36 }
9a76221e 37 }
e601adda 38
def65dcc 39 $self->maybe::next::method(@_);
e601adda 40
41 return 1;
eccb2137 42}
7ad87df9 43
398c5a1b 44=head1 NAME
45
46Catalyst::Action::Deserialize - Deserialize Data in a Request
47
48=head1 SYNOPSIS
49
50 package Foo::Controller::Bar;
51
52 __PACKAGE__->config(
faf5c20b 53 'default' => 'text/x-yaml',
54 'stash_key' => 'rest',
55 'map' => {
56 'text/x-yaml' => 'YAML',
57 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
58 },
398c5a1b 59 );
60
e601adda 61 sub begin :ActionClass('Deserialize') {}
398c5a1b 62
63=head1 DESCRIPTION
64
65This action will deserialize HTTP POST, PUT, and OPTIONS requests.
66It assumes that the body of the HTTP Request is a serialized object.
67The serializer is selected by introspecting the requests content-type
68header.
69
398c5a1b 70The specifics of deserializing each content-type is implemented as
71a plugin to L<Catalyst::Action::Deserialize>. You can see a list
72of currently implemented plugins in L<Catalyst::Controller::REST>.
73
74The results of your Deserializing will wind up in $c->req->data.
75This is done through the magic of L<Catalyst::Request::REST>.
76
e601adda 77While it is common for this Action to be called globally as a
78C<begin> method, there is nothing stopping you from using it on a
79single routine:
398c5a1b 80
e601adda 81 sub foo :Local :Action('Deserialize') {}
398c5a1b 82
e601adda 83Will work just fine.
398c5a1b 84
9a76221e 85When you use this module, the request class will be changed to
86L<Catalyst::Request::REST>.
87
398c5a1b 88=head1 SEE ALSO
89
90You likely want to look at L<Catalyst::Controller::REST>, which implements
91a sensible set of defaults for a controller doing REST.
92
93L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST>
94
5cb5f6bb 95=head1 AUTHORS
398c5a1b 96
5cb5f6bb 97See L<Catalyst::Action::REST> for authors.
398c5a1b 98
99=head1 LICENSE
100
101You may distribute this code under the same terms as Perl itself.
102
103=cut
104
7ad87df9 1051;