lib/Web/DataService/Plugin/Dancer.pm

filter: Statementstmt 22.9% 11 / 48 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 15.3% 4 / 26 Podpod 0.0% 0 / 24 totaltotal 14.2% 15 / 105 CC 3 SCAR 10.6
CC
3
Cov
21%
set_cors_header
12.0
store_inner
2.0
retrieve_inner
2.0
CRAP 2.9
« Build ↑ Summary blib/lib/Web/DataService/Plugin/Dancer.pm »
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 <mmcclenn@cpan.org>
13  
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 package Web::DataService::Plugin::Dancer;
18  
19 1 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 sub initialize_plugin {
23
24 1 ▶ Dancer::set(warnings => 0);
Subroutine initialize_plugin: called CC=1 SCAR=0.0
Pod undocumented
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 ▶ my ($class, $ds) = @_;
Subroutine read_config: called CC=1 SCAR=0.0
Pod undocumented
51
52 1 my $config_hash = Dancer::config;
53 1 $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 ▶ my ($plugin, $outer, $inner) = @_;
Subroutine store_inner: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ my ($plugin, $outer) = @_;
Subroutine retrieve_inner: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶sub store_outer {
Subroutine store_outer: not called CC=1 SCAR=0.0
Pod undocumented
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 ▶ return Dancer::request;
Subroutine retrieve_outer: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ return Dancer::Plugin::Database::database();
Subroutine get_connection: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ return Dancer::request->base;
Subroutine get_base_url: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ return Dancer::request->uri;
Subroutine get_request_url: not called CC=1 SCAR=6.9
Pod undocumented
130 }
131  
132  
133 # get_request_path ( request )
134 #
135 # Return the request path
136  
137 sub get_request_path {
138  
139 0 ▶ return Dancer::request->path;
Subroutine get_request_path: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ return Dancer::request->method;
Subroutine get_http_method: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ return Dancer::request->content_type;
Subroutine get_content_type: not called CC=1 SCAR=6.9
Pod undocumented
160 }
161  
162  
163 # get_params ( request )
164 #
165 # Return the parameters for the current request.
166  
167 sub get_params {
168
169 0 ▶ my ($plugin, $request, @rest) = @_;
Subroutine get_params: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ my ($plugin, $request, $param) = @_;
Subroutine get_param: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ my ($plugin, $request) = @_;
Subroutine get_request_body: not called CC=1 SCAR=6.9
Pod undocumented
196
197 0 return Dancer::request->body();
198 }
199  
200  
201 # set_cors_header ( request, arg )
202 #
203 # Set the CORS access control header according to the argument.
204  
205 sub set_cors_header {
206  
207 0 ▶ my ($plugin, $request, $arg) = @_;
Subroutine set_cors_header: not called CC=3 SCAR=24.8
Pod undocumented
208
209 0 ▶ if ( defined $arg && $arg eq '*' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg and $arg eq "*") 0 0 0
Condition0%0/3
defined $arg and $arg eq "*"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $arg and $arg eq "*"
defined $arg $arg eq "*"
Truth table0%0/3
A and B
A= defined $argB= $arg eq "*"
ABresult
0X0
100
111
210 {
211 0 Dancer::header "Access-Control-Allow-Origin" => "*";
212 }
213 }
214  
215  
216 # set_content_type ( outer, type )
217 #
218 # Set the response content type.
219  
220 sub set_content_type {
221
222 0 ▶ my ($plugin, $request, $type) = @_;
Subroutine set_content_type: not called CC=1 SCAR=6.9
Pod undocumented
223
224 0 Dancer::content_type $type;
225 }
226  
227  
228 # set_header ( outer, header, value )
229 #
230 # Set an arbitrary header in the response.
231  
232 sub set_header {
233
234 0 ▶ my ($plugin, $request, $header, $value) = @_;
Subroutine set_header: not called CC=1 SCAR=6.9
Pod undocumented
235
236 0 Dancer::header $header => $value;
237 }
238  
239  
240 # set_status ( outer, status )
241 #
242 # Set the response status code.
243  
244 sub set_status {
245
246 0 ▶ my ($class, $request, $code) = @_;
Subroutine set_status: not called CC=1 SCAR=6.9
Pod undocumented
247
248 0 Dancer::status $code;
249 }
250  
251  
252 # set_body ( outer, body )
253 #
254 # Set the response body.
255  
256 sub set_body {
257
258 0 ▶ my ($class, $request, $body) = @_;
Subroutine set_body: not called CC=1 SCAR=6.9
Pod undocumented
259
260 0 Dancer::SharedData->response->content($body);
261 }
262  
263
264 # file_path ( @components )
265 #
266 # Concatenate the specified file paths together, in a file-system-independent
267 # manner.
268  
269 sub file_path {
270  
271 0 ▶ shift;
Subroutine file_path: not called CC=1 SCAR=6.9
Pod undocumented
272 0 return Dancer::path(@_);
273 }
274  
275  
276 # file_readable ( filename )
277 #
278 # Return true if the specified file exists and is readable, false otherwise.
279  
280 sub file_readable {
281
282 0 ▶ my $file_name = Dancer::path(Dancer::setting('public'), $_[1]);
Subroutine file_readable: not called CC=1 SCAR=6.9
Pod undocumented
283 0 return -r $file_name;
284 }
285  
286  
287 # file_exists ( filename )
288 #
289 # Return true if the specified file exists, false otherwise.
290  
291 sub file_exists {
292  
293 0 ▶ my $file_name = Dancer::path(Dancer::setting('public'), $_[1]);
Subroutine file_exists: not called CC=1 SCAR=6.9
Pod undocumented
294 0 return -e $file_name;
295 }
296  
297  
298 # send_file ( outer, filename )
299 #
300 # Send as the response the contents of the specified file. For Dancer, the path
301 # is always evaluated relative to the 'public' directory.
302  
303 sub send_file {
304
305 0 ▶ return Dancer::send_file($_[2]);
Subroutine send_file: not called CC=1 SCAR=6.9
Pod undocumented
306 }
307  
308  
309 1 1;
« Build ↑ Summary blib/lib/Web/DataService/Plugin/Dancer.pm »