convert to Dist::Zilla
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Serialize / View.pm
1 package Catalyst::Action::Serialize::View;
2 use Moose;
3 use namespace::autoclean;
4
5 extends 'Catalyst::Action';
6
7 sub execute {
8     my $self = shift;
9     my ( $controller, $c, $view ) = @_;
10
11     # Views don't care / are not going to render an entity for 3XX
12     # responses.
13     return 1 if $c->response->status =~ /^(?:204|3\d\d)$/;
14
15     my $stash_key = (
16             $controller->{'serialize'} ?
17                 $controller->{'serialize'}->{'stash_key'} :
18                 $controller->{'stash_key'} 
19         ) || 'rest';
20
21     if ( !$c->view($view) ) {
22         $c->log->error("Could not load $view, refusing to serialize");
23         return;
24     }
25
26     if ($c->view($view)->process($c, $stash_key)) {
27       return 1;
28     } else {
29       # This is stupid. Please improve it.
30       my $error = join("\n", @{ $c->error }) || "Error in $view";
31       $error .= "\n";
32       $c->clear_errors;
33       die $error;
34     }
35 }
36
37 __PACKAGE__->meta->make_immutable;
38
39 1;