File Coverage

blib/lib/Dancer2/Plugin/HTTP/Bundle.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 18 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::HTTP::Bundle;
2              
3 1     1   21679 use 5.006;
  1         4  
4 1     1   5 use strict;
  1         2  
  1         22  
5 1     1   5 use warnings;
  1         5  
  1         34  
6              
7 1     1   753 use Import::Into;
  1         3007  
  1         146  
8              
9             my @sub_modules = qw(
10             Dancer2::Plugin::HTTP::Caching
11             Dancer2::Plugin::HTTP::ContentNegotiation
12             Dancer2::Plugin::HTTP::ConditionalRequest
13             );
14             # Dancer2::Plugin::HTTP::Auth::Extensible
15             # Dancer2::Plugin::HTTP::Auth::Handler;
16             # Dancer2::Plugin::HTTP::Cache 'CHI';
17              
18             sub import {
19 1     1   10 my $caller = caller;
20             $_->import::into( $caller )
21 1         8 for @sub_modules;
22             }
23              
24              
25             =head1 NAME
26              
27             Dancer2::Plugin::HTTP::Bundle - The missing HTTP bits of Dancer2 Bundled
28              
29             =head1 VERSION
30              
31             Version 0.02
32              
33             =cut
34              
35             our $VERSION = '0.02';
36              
37              
38             =head1 SYNOPSIS
39              
40             There are a few Dancer2 Plugins to help building REST api's. This wrapper helps
41             loading them all at once, in the right order and will demonstrate the combined
42             use of them.
43              
44             use Dancer2::Plugin::HTTP::Bundle
45            
46             get '/secrets/:id' => http_auth_handler_can('find_something') => sub {
47             my $secret_object = http_auth_handler->find_something(param->{id})
48             or return sub { status (404 ) };
49             http_conditional (
50             etag => $secret_object->etag,
51             last_modified => $secret_object->date_last_modified
52             ) =>sub { http_choose_accept (
53             'application/json' => sub { to_json $secret_object },
54             'application/xml' => sub { to_xml $secret_object },
55             { default => undef }
56             ) }
57             };
58              
59             Or a little more verbose
60              
61             use Dancer2::Plugin::HTTP:::Bundle
62            
63             get '/secrets/:id' => http_auth_handler_can('find_something') => sub {
64            
65             # what content-type does the client want
66             http_choose_accept (
67            
68             [ 'application/json', 'application/xml' ] => sub {
69            
70             # find the resource
71            
72             my $secret_object =
73             http_auth_handler->find_something(param->{id});
74            
75             unless ( $secret_object ) {
76             status (404); # Not Found
77             return;
78             }
79            
80             # set caching information
81            
82             http_cache_max_age 3600;
83             http_cache_private;
84            
85             # make the request conditional
86             # maybe we do not need to serialize
87            
88             http_conditional (
89             etag => $secret_object->etag,
90             last_modified => $secret_object->date_last_modified
91             ) => sub {
92             for (http_accept) {
93             when ('application/json') {
94             return to_json ( $secret_object )
95             }
96             when ('application/xml') {
97             return to_xml ( $secret_object )
98             }
99             }
100             }
101            
102             },
103            
104             [ 'image/png', 'image/jpeg' ] => sub {
105             ...
106             },
107            
108             { default => undef }
109             )
110            
111             };
112              
113             =head1 HTTP... and the RFC's
114              
115             =item RFC 7234 - Hypertext Transfer Protocol (HTTP/1.1): Caching
116              
117             The Hypertext Transfer Protocol (HTTP) is a stateless application-
118             level protocol for distributed, collaborative, hypertext information
119             systems. This document defines HTTP caches and the associated header
120             fields that control cache behavior or indicate cacheable response
121             messages.
122              
123             L
124              
125              
126             =head1 AUTHOR
127              
128             Theo van Hoesel, C<< >>
129              
130             =head1 BUGS
131              
132             Please report any bugs or feature requests to C, or through
133             the web interface at L. I will be notified, and then you'll
134             automatically be notified of progress on your bug as I make changes.
135              
136              
137              
138              
139             =head1 SUPPORT
140              
141             You can find documentation for this module with the perldoc command.
142              
143             perldoc Dancer2::Plugin::HTTP
144              
145              
146             You can also look for information at:
147              
148             =over 4
149              
150             =item * RT: CPAN's request tracker (report bugs here)
151              
152             L
153              
154             =item * AnnoCPAN: Annotated CPAN documentation
155              
156             L
157              
158             =item * CPAN Ratings
159              
160             L
161              
162             =item * Search CPAN
163              
164             L
165              
166             =back
167              
168              
169             =head1 ACKNOWLEDGEMENTS
170              
171              
172             =head1 LICENSE AND COPYRIGHT
173              
174             Copyright 2015 Theo van Hoesel.
175              
176             This program is free software; you can redistribute it and/or modify it
177             under the terms of the the Artistic License (2.0). You may obtain a
178             copy of the full license at:
179              
180             L
181              
182             Any use, modification, and distribution of the Standard or Modified
183             Versions is governed by this Artistic License. By using, modifying or
184             distributing the Package, you accept this license. Do not use, modify,
185             or distribute the Package, if you do not accept this license.
186              
187             If your Modified Version has been derived from a Modified Version made
188             by someone other than you, you are nevertheless required to ensure that
189             your Modified Version complies with the requirements of this license.
190              
191             This license does not grant you the right to use any trademark, service
192             mark, tradename, or logo of the Copyright Holder.
193              
194             This license includes the non-exclusive, worldwide, free-of-charge
195             patent license to make, have made, use, offer to sell, sell, import and
196             otherwise transfer the Package with respect to any patent claims
197             licensable by the Copyright Holder that are necessarily infringed by the
198             Package. If you institute patent litigation (including a cross-claim or
199             counterclaim) against any party alleging that the Package constitutes
200             direct or contributory patent infringement, then this Artistic License
201             to you shall terminate on the date that such litigation is filed.
202              
203             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
204             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
205             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
206             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
207             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
208             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
209             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
210             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
211              
212              
213             =cut
214              
215             1; # End of Dancer2::Plugin::HTTP::Bundle