File Coverage

lib/Web/DataService/Plugin/Dancer.pm
Criterion Covered Total %
statement 10 51 19.6
branch 0 6 0.0
condition 0 3 0.0
subroutine 4 26 15.3
pod 0 24 0.0
total 14 110 12.7


line stmt bran cond sub pod time code
1             #
2             # Web::DataService::Plugin::Dancer.pm
3             #
4             # This plugin provides Web::DataService with the ability to use Dancer.pm as
5             # its "foundation framework". Web::DataService uses the foundation framework
6             # to parse HTTP requests, to marshall and send back HTTP responses, and to
7             # provide configuration information for the data service.
8             #
9             # Other plugins will eventually be developed to fill this same role using
10             # Mojolicious and other frameworks.
11             #
12             # Author: Michael McClennen
13              
14              
15 1     1   60 use strict;
  1         2  
  1         47  
16              
17             package Web::DataService::Plugin::Dancer;
18              
19 1     1   6 use Carp qw( carp croak );
  1         2  
  1         928  
20              
21              
22             sub initialize_plugin {
23            
24 1     1 0 5 Dancer::set(warnings => 0);
25             #Dancer::set(app_handles_errors => 1);
26             }
27              
28              
29             # The following methods are called with the parameters specified: $ds is a
30             # reference to a data service instance, $request is a reference to the request
31             # instance defined by Web::DataService. If a request object was defined by
32             # the foundation framework, it will be available as $request->{outer}. The
33             # data service instance is also available as $request->{ds}.
34              
35             # ============================================================================
36              
37             # read_config ( ds, name, param )
38             #
39             # This method returns configuration information from the application
40             # configuration file used by the foundation framework. If $param is given,
41             # then return the value of that configuration parameter (if any). This value
42             # is looked up first under the configuration group $name (if given), and if not
43             # found is then looked up directly.
44             #
45             # If $param is not given, then return the configuration group $name if that
46             # was given, or else a hash of the entire set of configuration parameters.
47              
48             sub read_config {
49            
50 1     1 0 3 my ($class, $ds) = @_;
51            
52 1         3 my $config_hash = Dancer::config;
53 1         15 $ds->{_config} = $config_hash;
54             }
55              
56              
57             # store_request ( outer, inner )
58             #
59             # Add to the specified "outer" request object a link to our "inner" request
60             # object.
61              
62             sub store_inner {
63            
64 0     0 0   my ($plugin, $outer, $inner) = @_;
65            
66 0           Dancer::var('wds_request', $inner);
67             }
68              
69              
70             # retrieve_request ( outer )
71             #
72             # Return the "inner" link from the specified request object.
73              
74             sub retrieve_inner {
75              
76 0     0 0   my ($plugin, $outer) = @_;
77            
78 0           return Dancer::var('wds_request');
79             }
80              
81              
82             # store_outer ( outer )
83             #
84             # Store the current 'outer' request object for later use. This is a no-op for
85             # Dancer, since the current 'outer' request object is always available.
86              
87       0 0   sub store_outer {
88              
89             }
90              
91              
92             # retrieve_outer ( )
93             #
94             # Return the 'outer' request object for the request being currently handled.
95              
96             sub retrieve_outer {
97            
98 0     0 0   return Dancer::request;
99             }
100              
101              
102             # get_connection ( )
103             #
104             # This method returns a database connection. If you wish to use it, make sure
105             # that you "use Dancer::Plugin::Database" in your main program.
106              
107             sub get_connection {
108            
109 0     0 0   return Dancer::Plugin::Database::database();
110             }
111              
112              
113             # get_base_url ( )
114             #
115             # Return the base URL for the data service.
116              
117             sub get_base_url {
118            
119 0     0 0   return Dancer::request->base;
120             }
121              
122              
123             # get_request_url ( request )
124             #
125             # Return the full URL that generated the current request
126              
127             sub get_request_url {
128            
129 0     0 0   return Dancer::request->uri;
130             }
131              
132              
133             # get_request_path ( request )
134             #
135             # Return the request path
136              
137             sub get_request_path {
138              
139 0     0 0   return Dancer::request->path;
140             }
141              
142              
143             # get_http_method ( request )
144             #
145             # Return the HTTP method associated with this request.
146              
147             sub get_http_method {
148              
149 0     0 0   return Dancer::request->method;
150             }
151              
152              
153             # get_content_type ( request )
154             #
155             # Return the content type associated with this request.
156              
157             sub get_content_type {
158              
159 0     0 0   return Dancer::request->content_type;
160             }
161              
162              
163             # get_params ( request )
164             #
165             # Return the parameters for the current request.
166              
167             sub get_params {
168            
169 0     0 0   my ($plugin, $request, @rest) = @_;
170            
171 0           my $params = Dancer::params(@rest);
172 0           delete $params->{splat};
173 0           return $params;
174             }
175              
176              
177             # get_param ( param )
178             #
179             # Return the specified raw parameter value for the current request.
180              
181             sub get_param {
182            
183 0     0 0   my ($plugin, $request, $param) = @_;
184            
185 0           return Dancer::params->{$param};
186             }
187              
188              
189             # get_request_body ( )
190             #
191             # Return the body of the request, or the empty string if there wasn't one.
192              
193             sub get_request_body {
194              
195 0     0 0   my ($plugin, $request) = @_;
196              
197 0 0         if ( Dancer::request->body() )
    0          
198             {
199 0           return Dancer::request->body();
200             }
201              
202             elsif ( Dancer::request->content_type eq 'application/x-www-form-urlencoded' )
203             {
204 0           my $body = Dancer::request->params('body');
205 0           return $body;
206             }
207              
208             else
209             {
210 0           return '';
211             }
212             }
213              
214              
215             # set_cors_header ( request, arg )
216             #
217             # Set the CORS access control header according to the argument.
218              
219             sub set_cors_header {
220              
221 0     0 0   my ($plugin, $request, $arg) = @_;
222            
223 0 0 0       if ( defined $arg && $arg eq '*' )
224             {
225 0           Dancer::header "Access-Control-Allow-Origin" => "*";
226             }
227             }
228              
229              
230             # set_content_type ( outer, type )
231             #
232             # Set the response content type.
233              
234             sub set_content_type {
235            
236 0     0 0   my ($plugin, $request, $type) = @_;
237            
238 0           Dancer::content_type $type;
239             }
240              
241              
242             # set_header ( outer, header, value )
243             #
244             # Set an arbitrary header in the response.
245              
246             sub set_header {
247            
248 0     0 0   my ($plugin, $request, $header, $value) = @_;
249            
250 0           Dancer::header $header => $value;
251             }
252              
253              
254             # set_status ( outer, status )
255             #
256             # Set the response status code.
257              
258             sub set_status {
259            
260 0     0 0   my ($class, $request, $code) = @_;
261            
262 0           Dancer::status $code;
263             }
264              
265              
266             # set_body ( outer, body )
267             #
268             # Set the response body.
269              
270             sub set_body {
271            
272 0     0 0   my ($class, $request, $body) = @_;
273            
274 0           Dancer::SharedData->response->content($body);
275             }
276              
277            
278             # file_path ( @components )
279             #
280             # Concatenate the specified file paths together, in a file-system-independent
281             # manner.
282              
283             sub file_path {
284              
285 0     0 0   shift;
286 0           return Dancer::path(@_);
287             }
288              
289              
290             # file_readable ( filename )
291             #
292             # Return true if the specified file exists and is readable, false otherwise.
293              
294             sub file_readable {
295            
296 0     0 0   my $file_name = Dancer::path(Dancer::setting('public'), $_[1]);
297 0           return -r $file_name;
298             }
299              
300              
301             # file_exists ( filename )
302             #
303             # Return true if the specified file exists, false otherwise.
304              
305             sub file_exists {
306              
307 0     0 0   my $file_name = Dancer::path(Dancer::setting('public'), $_[1]);
308 0           return -e $file_name;
309             }
310              
311              
312             # send_file ( outer, filename )
313             #
314             # Send as the response the contents of the specified file. For Dancer, the path
315             # is always evaluated relative to the 'public' directory.
316              
317             sub send_file {
318            
319 0     0 0   return Dancer::send_file($_[2]);
320             }
321              
322              
323             1;