Ran perltidy
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize.pm
1 #
2 # Catlyst::Action::Deserialize
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
4 #
5 # $Id$
6
7 package Catalyst::Action::Deserialize;
8
9 use strict;
10 use warnings;
11
12 use base 'Catalyst::Action';
13 use Module::Pluggable::Object;
14 use Catalyst::Request::REST;
15
16 __PACKAGE__->mk_accessors(qw(plugins));
17
18 sub execute {
19     my $self = shift;
20     my ( $controller, $c, $test ) = @_;
21
22     my $nreq = bless( $c->request, 'Catalyst::Request::REST' );
23     $c->request($nreq);
24
25     unless ( defined( $self->plugins ) ) {
26         my $mpo = Module::Pluggable::Object->new(
27             'require'     => 1,
28             'search_path' => ['Catalyst::Action::Deserialize'],
29         );
30         my @plugins = $mpo->plugins;
31         $self->plugins( \@plugins );
32     }
33     my $content_type = $c->request->content_type;
34     my $sclass       = 'Catalyst::Action::Deserialize::';
35     my $sarg;
36     my $map = $controller->serialize->{'map'};
37     if ( exists( $map->{$content_type} ) ) {
38         my $mc;
39         if ( ref( $map->{$content_type} ) eq "ARRAY" ) {
40             $mc   = $map->{$content_type}->[0];
41             $sarg = $map->{$content_type}->[1];
42         } else {
43             $mc = $map->{$content_type};
44         }
45         $sclass .= $mc;
46         if ( !grep( /^$sclass$/, @{ $self->plugins } ) ) {
47             die "Cannot find plugin $sclass for $content_type!";
48         }
49     } else {
50         if ( exists( $controller->serialize->{'default'} ) ) {
51             $sclass .= $controller->serialize->{'default'};
52         } else {
53             die "I cannot find a default serializer!";
54         }
55     }
56
57     my @demethods = qw(POST PUT OPTIONS);
58     my $method    = $c->request->method;
59     if ( grep /^$method$/, @demethods ) {
60         if ( defined($sarg) ) {
61             $sclass->execute( $controller, $c, $sarg );
62         } else {
63             $sclass->execute( $controller, $c );
64         }
65         $self->NEXT::execute( @_, );
66     } else {
67         $self->NEXT::execute(@_);
68     }
69 }
70
71 1;