File Coverage

blib/lib/Dancer2/Plugin/Syntax/ParamKeywords.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Syntax::ParamKeywords;
2             # ABSTRACT: Parameter keywords for the lazy
3             # VERSION
4             $Dancer2::Plugin::Syntax::ParamKeywords::VERSION = '0.2.0';
5 1     1   866943 use strict;
  1         9  
  1         33  
6 1     1   6 use warnings;
  1         1  
  1         27  
7 1     1   579 use Dancer2::Plugin;
  1         13549  
  1         8  
8              
9             plugin_keywords
10             route_param => sub {
11             my ( $plugin, $param ) = @_;
12             return $plugin->dsl->route_parameters->get( $param );
13             },
14              
15             route_params => sub {
16             my ( $plugin ) = @_;
17             return $plugin->dsl->route_parameters;
18             },
19              
20             query_param => sub {
21             my ( $plugin, $param ) = @_;
22             return $plugin->dsl->query_parameters->get( $param );
23             },
24              
25             query_params => sub {
26             my ( $plugin, $param ) = @_;
27             my $params_hmv = $plugin->dsl->query_parameters;
28             return $params_hmv->get_all( $param ) if defined $param;
29             return $params_hmv;
30             },
31              
32             body_param => sub {
33             my ( $plugin, $param ) = @_;
34             return $plugin->dsl->body_parameters->get( $param );
35             },
36              
37             body_params => sub {
38             my ( $plugin, $param ) = @_;
39             my $params_hmv = $plugin->dsl->body_parameters;
40             return $params_hmv->get_all( $param ) if defined $param;
41             return $params_hmv;
42             };
43              
44             1;
45              
46             __END__
47              
48             =pod
49              
50             =encoding UTF-8
51              
52             =head1 NAME
53              
54             Dancer2::Plugin::Syntax::ParamKeywords - Parameter keywords for the lazy
55              
56             =head1 VERSION
57              
58             version 0.2.0
59              
60             =head1 SYNOPSIS
61              
62             use Dancer2::Plugin::Syntax::ParamKeywords;
63              
64             # Sample URL: http://localhost:3000/my_route/baz/blah
65             get '/my_route/:foo/:bar' => sub {
66             my $params = route_params; # Hash::MultiValue object
67              
68             # This will show in your app log
69             debug "We got us some foo!" if route_param( 'foo' ) eq 'baz';
70             };
71              
72             # Sample URL: http://localhost:3000/my_other_route?foo=baz&bar=blah&bar=bah
73             get '/my_other_route' => sub {
74             my $params = query_params; # Hash::MultiValue object
75              
76             # This will show in your app log
77             debug "We got us some foo!" if query_param( 'foo' ) eq 'baz';
78              
79             return join( ',', sort query_params( 'bar' ) ); # returns 'bah, blah'
80             };
81              
82             # Sample URL: http://localhost:3000/my_post_route
83             # Posted data: foo => 'bar', bar => 'baz', bar => 'quux'
84             post '/my_last_route' => sub {
85             my $params = body_params; # Hash::MultiValue object
86              
87             # This will show in your app log
88             debug "We got us some foo!" if body_param( 'foo' ) eq 'bar';
89              
90             return join( ',', sort body_params( 'bar' ) ); # returns 'baz, quux'
91             };
92              
93             =head1 DESCRIPTION
94              
95             Let's face it: L<Dancer2>'s parameter-fetching keywords take so much typing
96             to use! Why can't they be shorter? Well now, dear reader, they are!
97              
98             This module provides a little syntactic sugar to make getting query, route,
99             and body parameters just a little bit quicker. Instead of writing these:
100              
101             query_parameters->get( ... );
102             query_parameters->get_all( ... );
103             query_parameters
104             body_parameters->get( ... );
105             body_parameters->get_all( ... );
106             body_parameters
107             route_parameters->get( ... );
108             route_parameters
109              
110             You can write just these:
111              
112             query_param( ... );
113             query_params( ... );
114             query_params
115             body_param( ... );
116             body_params( ... );
117             body_params
118             route_param( ... );
119             route_params
120              
121             That's about 25-50% less typing in many cases. You're welcome.
122              
123             =head1 KEYWORDS
124              
125             =head2 query_param
126              
127             This is the same as calling C<query_parameters-E<gt>get>. You must pass a
128             parameter name to get the value of.
129              
130             =head2 query_params
131              
132             If called without a parameter name, this is just like calling
133             C<query_parameters>. It will return a L<Hash::MultiValue> object containing
134             all of the query parameters that were passed.
135              
136             When called with a parameter name, this is the same as calling
137             C<query_parameters-E<gt>get_all>.
138              
139             =head2 body_param
140              
141             This is the equivalent of C<body_parameters-E<gt>get>. You must pass a
142             parameter name to get the value of.
143              
144             =head2 body_params
145              
146             When called without a parameter name, this is the same as calling
147             C<body_parameters>. It will return a L<Hash::MultiValue> object containing
148             all of the body parameters that were posted to your route.
149              
150             When called with a parameter name, this acts the same as calling
151             C<body_parameters-E<gt>get_all>.
152              
153             =head2 route_param
154              
155             This is the equivalent of C<route_parameters-E<gt>get>. You must pass a
156             parameter name to get.
157              
158             =head2 route_params
159              
160             When called without a parameter name, this is the equivalent of calling
161             C<route_parameters>. It will return a L<Hash::MultiValue> object containing
162             all of the route parameters that were passed.
163              
164             Unlike C<query_params> and C<body_params>, you cannot assign multiple
165             values to a single named parameter through route parameters in Dancer2.
166             As such, you cannot pass a parameter to C<route_params> as you can with
167             the other two methods - the underlying C<get_all> functionality for
168             route parameters does not exist.
169              
170             =head1 SEE ALSO
171              
172             =over 4
173              
174             =item * L<Dancer2>
175              
176             =back
177              
178             =head1 AUTHOR
179              
180             Jason A. Crome <cromedome@cpan.org>
181              
182             =head1 COPYRIGHT AND LICENSE
183              
184             This software is copyright (c) 2021 by Jason A. Crome.
185              
186             This is free software; you can redistribute it and/or modify it under
187             the same terms as the Perl 5 programming language system itself.
188              
189             =cut