File Coverage

blib/lib/Catalyst/TraitFor/Request/REST/ForBrowsers/AndPJAX.pm
Criterion Covered Total %
statement 10 10 100.0
branch 2 2 100.0
condition 4 5 80.0
subroutine 3 3 100.0
pod n/a
total 19 20 95.0


line stmt bran cond sub pod time code
1             #
2             # This file is part of Catalyst-TraitFor-Request-REST-ForBrowsers-AndPJAX
3             #
4             # This software is Copyright (c) 2012 by Chris Weyl.
5             #
6             # This is free software, licensed under:
7             #
8             # The GNU Lesser General Public License, Version 2.1, February 1999
9             #
10             package Catalyst::TraitFor::Request::REST::ForBrowsers::AndPJAX;
11             {
12             $Catalyst::TraitFor::Request::REST::ForBrowsers::AndPJAX::VERSION = '0.001';
13             }
14              
15             # ABSTRACT: Acknowledge C<PJAX> requests as browser requests
16              
17 1     1   929919 use Moose::Role;
  1         3  
  1         10  
18 1     1   5592 use namespace::autoclean;
  1         3  
  1         10  
19              
20             with 'Catalyst::TraitFor::Request::REST::ForBrowsers' =>{
21             -excludes => [ '_build_looks_like_browser' ],
22             -alias => {
23             _build_looks_like_browser => '_original_build_looks_like_browser',
24             },
25             };
26              
27              
28             sub _build_looks_like_browser {
29 11     11   202384 my ($self) = @_;
30              
31 11   100     57 my $pjax = $self->header('X-Pjax') || 'false';
32              
33 11 100 66     1046 return 1
34             if $pjax eq 'true' && uc $self->method eq 'GET';
35              
36 9         43 return $self->_original_build_looks_like_browser;
37             }
38              
39              
40             !!42;
41              
42              
43             =pod
44              
45             =encoding utf-8
46              
47             =for :stopwords Chris Weyl PJAX Pjax
48              
49             =head1 NAME
50              
51             Catalyst::TraitFor::Request::REST::ForBrowsers::AndPJAX - Acknowledge C<PJAX> requests as browser requests
52              
53             =head1 VERSION
54              
55             This document describes version 0.001 of Catalyst::TraitFor::Request::REST::ForBrowsers::AndPJAX - released June 06, 2012 as part of Catalyst-TraitFor-Request-REST-ForBrowsers-AndPJAX.
56              
57             =head1 SYNOPSIS
58              
59             # in your app class
60             use CatalystX::RoleApplicator;
61             __PACKAGE__->apply_request_class_roles(qw/
62             Catalyst::TraitFor::Request::REST::ForBrowsers::AndPJAX
63             /);
64              
65             # then, off in an controller somewhere...
66             sub action_GET_html { ... also called for PJAX requests ... }
67              
68             =head1 DESCRIPTION
69              
70             This is a tiny little L<Catalyst::Request> class trait that recognizes that a
71             PJAX request is also a browser request, and thus looks_like_browser() also
72             returns true when the method is GET and the C<X-Pjax> header is present and is
73             'true'.
74              
75             This allows actions using an action class of REST::ForBrowsers to
76             transparently handle PJAX requests, without requiring any more modification to
77             the controller or application than applying this trait to the request class,
78             rather than plain-old L<Catalyst::TraitFor::Request::REST::ForBrowsers>.
79              
80             =head1 METHODS
81              
82             =head2 looks_like_browser
83              
84             This method is wrapped to return true if the method is GET and the C<X-Pjax>
85             header is present and is 'true'.
86              
87             Otherwise we hand things off to the original method, to render its verdict as
88             to the tenor of the request.
89              
90             =head1 SEE ALSO
91              
92             Please see those modules/websites for more information related to this module.
93              
94             =over 4
95              
96             =item *
97              
98             L<Catalyst::TraitFor::Request::REST::ForBrowsers>
99              
100             =item *
101              
102             L<Catalyst::Action::REST::ForBrowsers>
103              
104             =item *
105              
106             L<Plack::Middleware::Pjax>
107              
108             =back
109              
110             =head1 SOURCE
111              
112             The development version is on github at L<http://github.com/RsrchBoy/catalyst-traitfor-request-rest-forbrowsers-andpjax>
113             and may be cloned from L<git://github.com/RsrchBoy/catalyst-traitfor-request-rest-forbrowsers-andpjax.git>
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests on the bugtracker website
118             https://github.com/RsrchBoy/catalyst-traitfor-request-rest-forbrowsers-andp
119             jax/issues
120              
121             When submitting a bug or request, please include a test-file or a
122             patch to an existing test-file that illustrates the bug or desired
123             feature.
124              
125             =head1 AUTHOR
126              
127             Chris Weyl <cweyl@alumni.drew.edu>
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             This software is Copyright (c) 2012 by Chris Weyl.
132              
133             This is free software, licensed under:
134              
135             The GNU Lesser General Public License, Version 2.1, February 1999
136              
137             =cut
138              
139              
140             __END__
141