blib/lib/Dancer/Plugin/SporeDefinitionControl.pm

filter: Statementstmt 92.7% 128 / 138 Branchbran 80.4% 37 / 46 Conditioncond 84.2% 16 / 19 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 86.6% 13 / 15 Podpod 100% 1 / 1 totaltotal 87.7% 201 / 229 CC 38 SCAR 25.2
CC
38
Cov
89%
__ANON__
23.2
_returned_options_methods
6.1
_load_path_validation
6.0
CRAP 12.5
↑ Summary
1 package Dancer::Plugin::SporeDefinitionControl;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Dancer::Plugin::REST '0.04';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use YAML qw/LoadFile DumpFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Dancer::Plugin::SporeDefinitionControl
15  
16 Dancer Plugin to control validity of route from a Spore configuration file
17  
18 =head1 VERSION
19  
20 Version 0.16
21  
22 =cut
23  
24 7 our $VERSION = '0.16';
25  
26 =head1 SYNOPSIS
27  
28 Dancer required version : 1.3002
29  
30 in your Dancer project, use this plugin and register :
31  
32 package MyDancer::Server;
33  
34 use Dancer::Plugin::SporeDefinitionControl;
35  
36 check_spore_definition();
37  
38 In your config file :
39  
40 plugins:
41 SporeDefinitionControl:
42 spore_spec_path: path/to/route_config.yaml
43  
44 The yaml path file can be relative (root project base) or absolute.
45  
46 in your file path/to/route_config.yaml, put your SPORE config :
47  
48 base_url: http://localhost:4500
49 version: 0.2
50 format:
51 - json
52 - xml
53 - yml
54 methods:
55 get_object:
56 required_params:
57 - id
58 - name_object
59 optional_params:
60 - created_at
61 path: /object/:id
62 method: GET
63 create_object:
64 required_params:
65 - name_object
66 optional_params:
67 - created_at
68 path: /object/create
69 method: POST
70 update_object:
71 required_params:
72 - id
73 - name_object
74 optional_params:
75 - created_at
76 path: /object/:id
77 method: PUT
78 delete_object:
79 required_params:
80 - id
81 - name_object
82 optional_params:
83 - created_at
84 path: /object/:id
85 method: DELETE
86  
87 =head1 INITIALISATION
88  
89 Load yaml config file
90  
91 =cut
92  
93  
94 #Load definition spore file from plugin config
95  
96 7 our $path_validation;
97 #_load_path_validation();
98 7 $path_validation =_load_path_validation();
99 sub _load_path_validation
100 {
101 7 ▶ my $rh_file = {};
Subroutine _load_path_validation: called CC=6 SCAR=17.9
102  
103 7 my $path_to_spore_def = plugin_setting->{'spore_spec_path'};
104 7 my $build_options_route = plugin_setting->{'build_options_route'};
105 7 ▶ if ($path_to_spore_def)
Branch: 1 branch
BranchTrueFalseTotal
if ($path_to_spore_def) 6 1 7
106 {
107 6 ▶ $path_to_spore_def = File::Spec->catfile( setting('appdir') , $path_to_spore_def) unless (File::Spec->file_name_is_absolute($path_to_spore_def));
Branch: 1 branch
BranchTrueFalseTotal
unless "File::Spec"->file_name_is_absolute($path_to_spore_def) 6 0 6
108 6 $rh_file = LoadFile($path_to_spore_def);
109 }
110  
111 7 my $path_valid;
112  
113 #load validation hash
114 7 foreach my $method_name (keys(%{$rh_file->{'methods'}}))
115 {
116 28 my $method = $rh_file->{'methods'}->{$method_name}->{'method'};
117 28 my $complet_path = $rh_file->{'methods'}->{$method_name}->{'path'};
118 28 my ($path, $query_params) = split(/\?/, $complet_path);
119 28 my @additional_params;
120 28 ▶ if (defined $query_params)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query_params) 2 26 28
121 {
122 2 @additional_params = map { $_ =~ s/=.*//g; $_ } split( /\&/, $query_params) ;
123 2 push @{$rh_file->{'methods'}->{$method_name}->{'required_params'}}, @additional_params;
124 }
125  
126 28 push @{$path_valid->{path}->{$path}}, $method;
127 28 push @{$path_valid->{method}->{$method}->{$path}->{params}},
128 {
129 required_params => $rh_file->{'methods'}->{$method_name}->{'required_params'},
130 optional_params => $rh_file->{'methods'}->{$method_name}->{'optional_params'},
131 28 form_data_params => $rh_file->{'methods'}->{$method_name}->{'form-data'},
132 };
133 28 $path_valid->{method}->{$method}->{$path}->{functions}->{$method_name} = 1;
134  
135 }
136 7 ▶ init_routes_options($path_valid->{path}) if (defined $build_options_route);
Branch: 1 branch
BranchTrueFalseTotal
if defined $build_options_route 2 5 7
137 7 return $path_valid;
138 };
139  
140 =head2 init_routes_options
141  
142 force the routes on options method
143  
144 =cut
145  
146  
147  
148 sub init_routes_options(){
149 2 ▶ my $paths = shift;
Subroutine init_routes_options: called CC=2 SCAR=6.9
Pod documented
150 2 foreach my $path (keys %{$paths}){
151 ▶ options $path => sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
152 5 };
153  
154 }
155 }
156  
157  
158  
159  
160 =head1 SUBROUTINES/METHODS
161  
162 =head2 check_spore_definition
163  
164 define spore validation to do on entered request
165  
166 =cut
167  
168 register 'check_spore_definition' => sub {
169 hook before => sub {
170 64 ▶ my $req = request;
Subroutine __ANON__: called CC=23 SCAR=31.4
171 64 my %req_params = params;
172 64 ▶ die "method request must be defined" unless (defined( $req->method() ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $req->method 0 64 64
173 64 ▶ _returned_error( "route pattern request must be defined", 404) unless (defined( $req->{_route_pattern} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $req->{"_route_pattern"} 3 61 64
174  
175 # my $all_route_pattern = $req->{_route_pattern};
176 #my $detail_route_pattern = split /?/, $route_pattern;
177 61 ▶ $path_validation = _load_path_validation() if !$path_validation;
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer::Plugin::SporeDefinitionControl::path_validation 0 61 61
178 61 ▶ unless (defined( $path_validation->{method}->{$req->method()}) || uc($req->method()) eq "OPTIONS" )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"method"}{$req->method} or uc $req->method eq "OPTIONS") 8 53 61
Condition100%3/3
defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"method"}{$req->method} or uc $req->method eq "OPTIONS"
l!l&&r!l&&!r
5128
MC/DC100%2/2
defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"method"}{$req->method} or uc $req->method eq "OPTIONS"
defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"method"}{$req->method} uc $req->method eq "OPTIONS"
Truth table100%3/3
A or B
A= defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"method"}{$req->method}B= uc $req->method eq "OPTIONS"
ABresult
1X1
011
000
179 {
180 8 my $req_method = $req->method();
181 8 return _returned_error("no route define with method `$req_method'", 404);
182 }
183  
184 #TODO : return an error because path does not exist in specification
185 53 ▶ unless (defined( $path_validation->{method}->{$req->method()}->{$req->{_route_pattern}} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"method"}{$req->method}{$req->{"_route_pattern"}} or uc $req->method eq "OPTIONS" and defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"path"}{$req->{"_route_pattern"}}) 8 45 53
Condition66%4/6
defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"method"}{$req->method}{$req->{"_route_pattern"}} or uc $req->method eq "OPTIONS" and defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"path"}{$req->{"_route_pattern"}}
l!l&&r!l&&!r
32210
uc $req->method eq "OPTIONS" and defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"path"}{$req->{"_route_pattern"}}
!ll&&!rl&&r
8013
MC/DC0%0/3
defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"method"}{$req->method}{$req->{"_route_pattern"}} or uc $req->method eq "OPTIONS" and defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"path"}{$req->{"_route_pattern"}}
defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"method"}{$req->method}{$req->{"_route_pattern"}} uc $req->method eq "OPTIONS" defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"path"}{$req->{"_route_pattern"}}
Truth table50%2/4
A or B and C
A= defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"method"}{$req->method}{$req->{"_route_pattern"}}B= uc $req->method eq "OPTIONS"C= defined $Dancer::Plugin::SporeDefinitionControl::path_validation->{"path"}{$req->{"_route_pattern"}}
ABCresult
1XX1
0111
00X0
0100
186 || (uc($req->method()) eq "OPTIONS" && defined($path_validation->{path}->{$req->{_route_pattern}}))
187 )
188 {
189 8 my $req_route_pattern = $req->{_route_pattern};
190 8 return _returned_error("route pattern `$req_route_pattern' is not defined",404);
191 }
192  
193 45 my $is_ok = 0;
194 #IF method is OPTIONS list of methods set the headers and return ok
195 45 ▶ _returned_options_methods($path_validation->{path}->{$req->{_route_pattern}}) if (uc($req->method()) eq "OPTIONS" );
Branch: 1 branch
BranchTrueFalseTotal
if uc $req->method eq "OPTIONS" 13 32 45
196 32 my $error;
197 32 foreach my $route_defined (@{$path_validation->{method}->{$req->method()}->{$req->{_route_pattern}}->{params}})
198 {
199 40 my $ko;
200 40 my $ra_required_params = $route_defined->{'required_params'};
201 40 my $ra_optional_params = $route_defined->{'optional_params'};
202 40 my $ra_form_data_params;
203 40 ▶ if ($route_defined->{'form_data_params'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($route_defined->{"form_data_params"}) 2 38 40
204 2 foreach my $k (keys %{$route_defined->{'form_data_params'}}){
205 2 push @{$ra_form_data_params}, $k;
206 }
207 }
208  
209 # check if required params are present
210  
211 40 foreach my $required_param (@{$ra_required_params})
212 {
213 70 ▶ if (!defined params->{$required_param})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined params()->{$required_param}) 17 53 70
214 {
215 17 $error = "required params `$required_param' is not defined";
216 17 $ko = 1;
217 }
218 }
219 40 ▶ next if $ko;
Branch: 1 branch
BranchTrueFalseTotal
if $ko 15 25 40
220  
221 25 my @list_total = ('format');
222 25 ▶ @list_total = (@list_total, @{$ra_required_params}) if defined($ra_required_params);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ra_required_params 25 0 25
223 25 ▶ @list_total = (@list_total, @{$ra_optional_params}) if defined($ra_optional_params);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ra_optional_params 25 0 25
224 25 ▶ @list_total = (@list_total, @{$ra_form_data_params}) if defined($ra_form_data_params);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ra_form_data_params 2 23 25
225 # check for each params if they are specified in spore spec
226
227 25 foreach my $param (keys %req_params)
228 {
229 65 ▶ if (!(grep {/^$param$/} @list_total))
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/^$param$/;} @list_total) 10 55 65
230 {
231 10 $error = "parameter `$param' is unknown";
232 10 $ko = 1 ;
233 }
234 }
235 25 ▶ next if $ko;
Branch: 1 branch
BranchTrueFalseTotal
if $ko 10 15 25
236 15 $is_ok = 1;
237 }
238 32 ▶ return _returned_error($error,400) unless $is_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_ok 17 15 32
239  
240 5 ▶ };
Subroutine __ANON__: called CC=1 SCAR=0.0
241 7 };
242  
243  
244 =head2 get_functions_from_request
245  
246 return the hash of functions available from method and path.
247  
248 =cut
249  
250 register 'get_functions_from_request' => sub {
251 0 ▶ my $req = request;
Subroutine __ANON__: not called CC=2 SCAR=17.9
252  
253 0 ▶ $path_validation = _load_path_validation() if !$path_validation;
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer::Plugin::SporeDefinitionControl::path_validation 0 0 0
254 0 my $method = $req->method();
255 0 my $path = $req->{_route_pattern};
256 0 my $functions = $path_validation->{method}->{$method}->{$path}->{functions};
257 0 return $functions;
258 7 };
259  
260 # format the error returned
261 sub _returned_error
262 {
263 36 ▶ my $str_error = shift;
Subroutine _returned_error: called CC=4 SCAR=14.2
264 36 my $code_error = shift;
265 36 ▶ $code_error ||= 400;
Condition50%1/2
$code_error ||= 400
l!l
360
MC/DC0%0/1
$code_error ||= 400
$code_error
Truth table50%1/2
A
A= $code_error
Aresult
11
00
266 36 set serializer => 'JSON';
267 36 debug $str_error."\n";
268 #return halt(send_error($str_error,400));
269 36 ▶ if ($code_error == 400)
Branch: 2 branches
BranchTrueFalseTotal
if ($code_error == 400) { } 17 19 36
elsif ($code_error == 404) { } 19 0 19
270 {
271 17 return halt(status_bad_request($str_error));
272 }
273 elsif ($code_error == 404)
274 {
275 19 return halt(status_not_found($str_error));
276 }
277 else
278 0 {die "Unknown code";}
279 }
280  
281 sub _returned_options_methods
282 {
283 13 ▶ my $methods = shift;
Subroutine _returned_options_methods: called CC=6 SCAR=18.1
284 13 my %seen = ();
285 13 my @unique_methods = grep { !$seen{$_}++ } @{$methods};
286 13 my $build_options_route = plugin_setting->{'build_options_route'};
287 13 ▶ if (defined $methods){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $methods) { } 13 0 13
288 13 set serializer => 'JSON';
289 13 status 200;
290 13 ▶ header 'access-control-allow-credentials' => $build_options_route->{'header_allow_credentials'} || '';
Condition100%2/2
$build_options_route->{'header_allow_credentials'} || ''
l!l
112
MC/DC100%1/1
$build_options_route->{'header_allow_credentials'} || ''
$build_options_route->{'header_allow_credentials'}
Truth table100%2/2
A
A= $build_options_route->{'header_allow_credentials'}
Aresult
11
00
291 13 ▶ header 'access-control-allow-headers' => $build_options_route->{'header_allow_headers'} || '';
Condition100%2/2
$build_options_route->{'header_allow_headers'} || ''
l!l
112
MC/DC100%1/1
$build_options_route->{'header_allow_headers'} || ''
$build_options_route->{'header_allow_headers'}
Truth table100%2/2
A
A= $build_options_route->{'header_allow_headers'}
Aresult
11
00
292 13 header 'access-control-allow-methods' => join(",",@unique_methods,'OPTIONS');
293 13 ▶ header 'access-control-allow-origin' => $build_options_route->{'header_allow_allow_origin'} || '';
Condition100%2/2
$build_options_route->{'header_allow_allow_origin'} || ''
l!l
112
MC/DC100%1/1
$build_options_route->{'header_allow_allow_origin'} || ''
$build_options_route->{'header_allow_allow_origin'}
Truth table100%2/2
A
A= $build_options_route->{'header_allow_allow_origin'}
Aresult
11
00
294 13 ▶ header 'access-control-max-age' => $build_options_route->{'header_max_age'} || '';
Condition100%2/2
$build_options_route->{'header_max_age'} || ''
l!l
112
MC/DC100%1/1
$build_options_route->{'header_max_age'} || ''
$build_options_route->{'header_max_age'}
Truth table100%2/2
A
A= $build_options_route->{'header_max_age'}
Aresult
11
00
295 13 return halt('{"status":200,"message":"OK"}');
296 }
297 else{
298 0 set serializer => 'JSON';
299 0 status 404;
300 0 return halt('{"status":404,"message":"no route exists"}');
301 }
302 }
303  
304  
305 =head1 AUTHOR
306  
307 Nicolas Oudard, C<< <nicolas at oudard.org> >>
308  
309 =head1 BUGS
310  
311 Please report any bugs or feature requests to C<bug-dancer-plugin-sporedefinitioncontrol at rt.cpan.org>, or through
312 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer-Plugin-SporeDefinitionControl>. I will be notified, and then you'll
313 automatically be notified of progress on your bug as I make changes.
314  
315  
316 =head1 SUPPORT
317  
318 You can find documentation for this module with the perldoc command.
319  
320 perldoc Dancer::Plugin::SporeDefinitionControl
321  
322  
323 You can also look for information at:
324  
325 =over 4
326  
327 =item * RT: CPAN's request tracker
328  
329 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-SporeDefinitionControl>
330  
331 =item * AnnoCPAN: Annotated CPAN documentation
332  
333 L<http://annocpan.org/dist/Dancer-Plugin-SporeDefinitionControl>
334  
335 =item * CPAN Ratings
336  
337 L<http://cpanratings.perl.org/d/Dancer-Plugin-SporeDefinitionControl>
338  
339 =item * Search CPAN
340  
341 L<http://search.cpan.org/dist/Dancer-Plugin-SporeDefinitionControl/>
342  
343 =back
344  
345  
346 =head1 ACKNOWLEDGEMENTS
347  
348  
349 =head1 LICENSE AND COPYRIGHT
350  
351 Copyright 2010 Nicolas Oudard.
352  
353 This program is free software; you can redistribute it and/or modify it
354 under the terms of either: the GNU General Public License as published
355 by the Free Software Foundation; or the Artistic License.
356  
357 See http://dev.perl.org/licenses/ for more information.
358  
359  
360 =cut
361  
362 7 register_plugin;
363 7 1; # End of Dancer::Plugin::SporeDefinitionControl
↑ Summary