1f647dddf2ef27b3c665cfe2940407cceddcf0f7
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / REST / ForBrowsers.pm
1 package Catalyst::Action::REST::ForBrowsers;
2
3 use Moose;
4 use namespace::autoclean;
5
6 our $VERSION = '0.88';
7 $VERSION = eval $VERSION;
8
9 extends 'Catalyst::Action::REST';
10 use Catalyst::Request::REST::ForBrowsers;
11
12 sub BUILDARGS {
13     my $class  = shift;
14     my $config = shift;
15     Catalyst::Request::REST::ForBrowsers->_insert_self_into( $config->{class} );
16     return $class->SUPER::BUILDARGS( $config, @_ );
17 }
18
19 override dispatch => sub {
20     my $self = shift;
21     my $c    = shift;
22
23     my $req = $c->request();
24
25     return super()
26         unless $req->can('looks_like_browser')
27             && $req->looks_like_browser()
28             && uc $c->request()->method() eq 'GET';
29
30     my $controller  = $c->component( $self->class );
31     my $rest_method = $self->name() . '_GET_html';
32
33     if (   $controller->action_for($rest_method)
34         || $controller->can($rest_method) ) {
35
36         return $self->_dispatch_rest_method( $c, $rest_method );
37     }
38
39     return super();
40 };
41
42 __PACKAGE__->meta->make_immutable;
43
44 1;