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