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