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