File Coverage

blib/lib/Catalyst/TraitFor/Request/Methods.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1              
2             # ABSTRACT: Add enumerated methods for HTTP requests
3              
4             use v5.14;
5 1     1   1888520  
  1         10  
6             use Moose::Role;
7 1     1   5  
  1         2  
  1         7  
8             use Data::Enum;
9 1     1   5083  
  1         877  
  1         27  
10             use namespace::autoclean;
11 1     1   7  
  1         2  
  1         8  
12             requires 'method';
13              
14             our $VERSION = 'v0.4.0';
15              
16              
17             my @METHODS = qw/ get head post put delete connect options trace patch unrecognized_method /;
18              
19             has _method_enum => (
20             is => 'ro',
21             lazy => 1,
22             default => sub {
23             state $enum = Data::Enum->new(@METHODS);
24             return eval { $enum->new(lc $_[0]->method =~ s/\W/_/gr) } // $enum->new('unrecognized_method');
25             },
26             handles => [ map { "is_" . $_ } @METHODS ],
27             );
28              
29              
30             1;
31              
32              
33             =pod
34              
35             =encoding UTF-8
36              
37             =head1 NAME
38              
39             Catalyst::TraitFor::Request::Methods - Add enumerated methods for HTTP requests
40              
41             =head1 VERSION
42              
43             version v0.4.0
44              
45             =head1 SYNOPSIS
46              
47             In the L<Catalyst> class
48              
49             __PACKAGE__->config(
50             request_class_traits => [
51             'Methods'
52             ]
53             );
54              
55             In any code that uses a L<Catalyst::Request>, e.g.
56              
57             if ($c->request->is_post) {
58             ...
59             }
60              
61             =head1 DESCRIPTION
62              
63             This trait adds enumerated methods from RFC 7231 and RFC 5789 for
64             checking the HTTP request method.
65              
66             Using these methods is a less error-prone alternative to checking a
67             case-sensitive string with the method name.
68              
69             In other words, you can use
70              
71             $c->request->is_get
72              
73             instead of
74              
75             $c->request->method eq "GET"
76              
77             The methods are implemented as lazy read-only attributes.
78              
79             =head1 METHODS
80              
81             =head2 is_get
82              
83             The request method is C<GET>.
84              
85             =head2 is_head
86              
87             The request method is C<HEAD>.
88              
89             =head2 is_post
90              
91             The request method is C<POST>.
92              
93             =head2 is_put
94              
95             The request method is C<PUT>.
96              
97             =head2 is_delete
98              
99             The request method is C<DELETE>.
100              
101             =head2 is_connect
102              
103             The request method is C<CONNECT>.
104              
105             =head2 is_options
106              
107             The request method is C<OPTIONS>.
108              
109             =head2 is_trace
110              
111             The request method is C<TRACE>.
112              
113             =head2 is_patch
114              
115             The request method is C<PATCH>.
116              
117             =head2 is_unrecognized_method
118              
119             The request method is not recognized.
120              
121             =head1 SEE ALSO
122              
123             L<Catalyst::Request>
124              
125             =head1 SOURCE
126              
127             The development version is on github at L<https://github.com/robrwo/Catalyst-TraitFor-Request-Methods->
128             and may be cloned from L<git://github.com/robrwo/Catalyst-TraitFor-Request-Methods-.git>
129              
130             =head1 BUGS
131              
132             Please report any bugs or feature requests on the bugtracker website
133             L<https://github.com/robrwo/Catalyst-TraitFor-Request-Methods-/issues>
134              
135             When submitting a bug or request, please include a test-file or a
136             patch to an existing test-file that illustrates the bug or desired
137             feature.
138              
139             =head1 AUTHOR
140              
141             Robert Rothenberg <rrwo@cpan.org>
142              
143             =head1 COPYRIGHT AND LICENSE
144              
145             This software is Copyright (c) 2019-2022 by Robert Rothenberg.
146              
147             This is free software, licensed under:
148              
149             The Artistic License 2.0 (GPL Compatible)
150              
151             =cut