| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Dancer-Plugin-Resource | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2013 by Matthew Phillips. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | package Dancer::Plugin::Resource; | ||||||||||||||||||||||||||||
| 10 | # ABSTRACT: A plugin for writing declarative RESTful apps with Dancer | ||||||||||||||||||||||||||||
| 11 | 9 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 9 | our $VERSION = '1.131120'; # VERSION | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 9 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 9 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 9 | ▶ | use Lingua::EN::Inflect::Number; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 9 | our $RESOURCE_DEBUG = 0; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 9 | my $content_types = { | |||||||||||||||||||||||||||
| 26 | json => 'application/json', | ||||||||||||||||||||||||||||
| 27 | yml => 'text/x-yaml', | ||||||||||||||||||||||||||||
| 28 | xml => 'application/xml', | ||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 9 | my %routes; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # thanks leont | ||||||||||||||||||||||||||||
| 34 | 146 | ▶ | sub _function_exists { | ||||||||||||||||||||||||||
|
Subroutine
_function_exists: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 35 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 146 | my $funcname = shift; | |||||||||||||||||||||||||||
| 37 | 146 | ▶ | return \&{$funcname} if defined &{$funcname}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 129 | return; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | sub prepare_serializer_for_format { | ||||||||||||||||||||||||||
|
Subroutine
prepare_serializer_for_format: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 42 | 2 | my $conf = plugin_setting; | |||||||||||||||||||||||||||
| 43 | my $serializers = ( | ||||||||||||||||||||||||||||
| 44 | ($conf && exists $conf->{serializers}) | ||||||||||||||||||||||||||||
| 45 | ? $conf->{serializers} | ||||||||||||||||||||||||||||
| 46 | 2 | ▶ | : { 'json' => 'JSON', | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$conf and exists $conf->{"serializers"}
MC/DC0%0/2
$conf and exists $conf->{"serializers"}
$conf
exists $conf->{"serializers"}
Truth table33%1/3
A and B
A= $confB= exists $conf->{"serializers"}
| |||||||||||||||||||||||||||||
| 47 | 'yml' => 'YAML', | ||||||||||||||||||||||||||||
| 48 | 'xml' => 'XML', | ||||||||||||||||||||||||||||
| 49 | 'dump' => 'Dumper', | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 9 | ▶ | hook 'before' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 54 | 9 | my $format = params->{'format'}; | |||||||||||||||||||||||||||
| 55 | 9 | ▶ | if (not defined $format) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 4 | set serializer => 'Mutable'; | |||||||||||||||||||||||||||
| 57 | 4 | return; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 5 | ▶ | return unless defined $format; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 5 | my $serializer = $serializers->{$format}; | |||||||||||||||||||||||||||
| 63 | 5 | ▶ | unless (defined $serializer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | return halt( | |||||||||||||||||||||||||||
| 65 | Dancer::Error->new( | ||||||||||||||||||||||||||||
| 66 | code => 404, | ||||||||||||||||||||||||||||
| 67 | message => "unsupported format requested: " . $format | ||||||||||||||||||||||||||||
| 68 | ) | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 4 | set serializer => $serializer; | |||||||||||||||||||||||||||
| 73 | 4 | ▶ | my $ct = $content_types->{$format} || setting('content_type'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$content_types->{$format} || setting('content_type')
MC/DC0%0/2
$content_types->{$format} || setting('content_type')
$content_types->{$format}
setting('content_type')
Truth table33%1/3
A || B
A= $content_types->{$format}B= setting('content_type')
| |||||||||||||||||||||||||||||
| 74 | 4 | content_type $ct; | |||||||||||||||||||||||||||
| 75 | 2 | }; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 9 | register prepare_serializer_for_format => \&prepare_serializer_for_format; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 8 | ▶ | register resource => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=16 SCAR=27.9
| |||||||||||||||||||||||||||||
| 80 | 8 | my ($resource, %options) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 8 | my $params = ':id'; | |||||||||||||||||||||||||||
| 83 | 8 | my ($old_prefix, $parent_prefix); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 8 | ▶ | unless ($options{skip_prepare_serializer} || ((caller)[1] =~ /^(?:t|xt)/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$options{"skip_prepare_serializer"} or (caller)[1] =~ /^(?:t|xt)/
MC/DC0%0/2
$options{"skip_prepare_serializer"} or (caller)[1] =~ /^(?:t|xt)/
$options{"skip_prepare_serializer"}
(caller)[1] =~ /^(?:t|xt)/
Truth table33%1/3
A or B
A= $options{"skip_prepare_serializer"}B= (caller)[1] =~ /^(?:t|xt)/
| |||||||||||||||||||||||||||||
| 86 | 0 | prepare_serializer_for_format; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # if this resource is a nested child resource, manage the prefix | ||||||||||||||||||||||||||||
| 90 | 8 | ▶ | $old_prefix = Dancer::App->current->prefix || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
'Dancer::App'->current->prefix || ''
MC/DC100%1/1
'Dancer::App'->current->prefix || ''
'Dancer::App'->current->prefix
Truth table100%2/2
A
A= 'Dancer::App'->current->prefix
| |||||||||||||||||||||||||||||
| 91 | 8 | $parent_prefix = ''; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 8 | ▶ | if ($options{parent} and $routes{$options{parent}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$options{"parent"} and $routes{$options{"parent"}}
MC/DC50%1/2
$options{"parent"} and $routes{$options{"parent"}}
$options{"parent"}
$routes{$options{"parent"}}
Truth table66%2/3
A and B
A= $options{"parent"}B= $routes{$options{"parent"}}
| |||||||||||||||||||||||||||||
| 94 | 2 | prefix $parent_prefix = $routes{$options{parent}}; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 6 | $parent_prefix = $old_prefix; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # create a default for the load funcs | ||||||||||||||||||||||||||||
| 101 | 8 | ▶ | $options{$_} ||= sub { undef } for (qw/load load_all/); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$options{$_} ||= sub {
undef;
}
MC/DC0%0/2
$options{$_} ||= sub {
undef;
}
$options{$_}
sub {
undef;
}
Truth table66%2/3
A ||= B
A= $options{$_}B= sub {
undef;
}
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # if member => 'foo' is passed, turn it into an array | ||||||||||||||||||||||||||||
| 104 | 8 | for my $type (qw/member collection/) { | |||||||||||||||||||||||||||
| 105 | 16 | ▶ | if ($options{$type} && ref $options{$type} eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$options{$type} and ref $options{$type} eq ""
MC/DC100%2/2
$options{$type} and ref $options{$type} eq ""
$options{$type}
ref $options{$type} eq ""
Truth table100%3/3
A and B
A= $options{$type}B= ref $options{$type} eq ""
| |||||||||||||||||||||||||||||
| 106 | 1 | $options{$type} = [$options{$type}]; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # by default take the singular resource as the param name (ie :user for users) | ||||||||||||||||||||||||||||
| 111 | 8 | my ($singular_resource, $plural_resource) = (Lingua::EN::Inflect::Number::to_S($resource), $resource); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # or if the user wants to override to take multiple params, ie /user/:foo/:bar/:baz | ||||||||||||||||||||||||||||
| 114 | # allow it. This could be useful for composite key schemas | ||||||||||||||||||||||||||||
| 115 | 8 | ▶ | if ( my $p = $options{params} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 1 | ▶ | $p = ref $p ? $p : [$p]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | $params = join '/', map ":${_}", @{$p}; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||
| 120 | 7 | $params = ":${singular_resource}_id"; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 8 | my ($package) = caller; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # main resource endpoints | ||||||||||||||||||||||||||||
| 126 | # CRUD | ||||||||||||||||||||||||||||
| 127 | 8 | _post( | |||||||||||||||||||||||||||
| 128 | _endpoint( | ||||||||||||||||||||||||||||
| 129 | path => $plural_resource, | ||||||||||||||||||||||||||||
| 130 | params => '', | ||||||||||||||||||||||||||||
| 131 | verbs => [qw/POST create/], | ||||||||||||||||||||||||||||
| 132 | function => $singular_resource | ||||||||||||||||||||||||||||
| 133 | ) | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | _get( | ||||||||||||||||||||||||||||
| 137 | _endpoint( | ||||||||||||||||||||||||||||
| 138 | path => $plural_resource, | ||||||||||||||||||||||||||||
| 139 | params => $params, | ||||||||||||||||||||||||||||
| 140 | verbs => [qw/GET get read/], | ||||||||||||||||||||||||||||
| 141 | loader => $options{load}, | ||||||||||||||||||||||||||||
| 142 | 8 | function => $singular_resource | |||||||||||||||||||||||||||
| 143 | ) | ||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | _put( | ||||||||||||||||||||||||||||
| 147 | _endpoint( | ||||||||||||||||||||||||||||
| 148 | path => $plural_resource, | ||||||||||||||||||||||||||||
| 149 | params => $params, | ||||||||||||||||||||||||||||
| 150 | verbs => [qw/PUT update/], | ||||||||||||||||||||||||||||
| 151 | loader => $options{load}, | ||||||||||||||||||||||||||||
| 152 | 8 | function => $singular_resource | |||||||||||||||||||||||||||
| 153 | ) | ||||||||||||||||||||||||||||
| 154 | ); | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | _del( | ||||||||||||||||||||||||||||
| 157 | _endpoint( | ||||||||||||||||||||||||||||
| 158 | path => $plural_resource, | ||||||||||||||||||||||||||||
| 159 | params => $params, | ||||||||||||||||||||||||||||
| 160 | verbs => [qw/DELETE delete/], | ||||||||||||||||||||||||||||
| 161 | loader => $options{load}, | ||||||||||||||||||||||||||||
| 162 | 8 | function => $singular_resource | |||||||||||||||||||||||||||
| 163 | ) | ||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | _get( | ||||||||||||||||||||||||||||
| 167 | _endpoint( | ||||||||||||||||||||||||||||
| 168 | path => $plural_resource, | ||||||||||||||||||||||||||||
| 169 | params => '', | ||||||||||||||||||||||||||||
| 170 | verbs => [qw/INDEX index/], | ||||||||||||||||||||||||||||
| 171 | loader => $options{load_all}, | ||||||||||||||||||||||||||||
| 172 | 8 | function => $singular_resource | |||||||||||||||||||||||||||
| 173 | ) | ||||||||||||||||||||||||||||
| 174 | ); | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # member routes are actions on the given id. ie /users/:user_id/foo | ||||||||||||||||||||||||||||
| 177 | 8 | for my $member (@{$options{member}}) { | |||||||||||||||||||||||||||
| 178 | 1 | my $path = "${plural_resource}/$params/${member}"; | |||||||||||||||||||||||||||
| 179 | 1 | my $member_param = ""; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | _post( | ||||||||||||||||||||||||||||
| 182 | _endpoint( | ||||||||||||||||||||||||||||
| 183 | path => $path, | ||||||||||||||||||||||||||||
| 184 | params => '', | ||||||||||||||||||||||||||||
| 185 | verbs => [qw/POST create/], | ||||||||||||||||||||||||||||
| 186 | loader => $options{load}, | ||||||||||||||||||||||||||||
| 187 | 1 | function => "${singular_resource}_${member}" | |||||||||||||||||||||||||||
| 188 | ) | ||||||||||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | _get( | ||||||||||||||||||||||||||||
| 192 | _endpoint( | ||||||||||||||||||||||||||||
| 193 | path => $path, | ||||||||||||||||||||||||||||
| 194 | params => $member_param, | ||||||||||||||||||||||||||||
| 195 | verbs => [qw/GET get read/], | ||||||||||||||||||||||||||||
| 196 | loader => $options{load}, | ||||||||||||||||||||||||||||
| 197 | 1 | function => "${singular_resource}_${member}" | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | ) | ||||||||||||||||||||||||||||
| 200 | ); | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | _put( | ||||||||||||||||||||||||||||
| 203 | _endpoint( | ||||||||||||||||||||||||||||
| 204 | path => $path, | ||||||||||||||||||||||||||||
| 205 | params => $member_param, | ||||||||||||||||||||||||||||
| 206 | verbs => [qw/PUT update/], | ||||||||||||||||||||||||||||
| 207 | loader => $options{load}, | ||||||||||||||||||||||||||||
| 208 | 1 | function => "${singular_resource}_${member}" | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | ) | ||||||||||||||||||||||||||||
| 211 | ); | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | _del( | ||||||||||||||||||||||||||||
| 214 | _endpoint( | ||||||||||||||||||||||||||||
| 215 | path => $path, | ||||||||||||||||||||||||||||
| 216 | params => $member_param, | ||||||||||||||||||||||||||||
| 217 | verbs => [qw/DELETE delete/], | ||||||||||||||||||||||||||||
| 218 | loader => $options{load}, | ||||||||||||||||||||||||||||
| 219 | 1 | function => "${singular_resource}_${member}" | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | ) | ||||||||||||||||||||||||||||
| 222 | ); | ||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # collection routes are actions on the collection. ie /users/foo | ||||||||||||||||||||||||||||
| 226 | 8 | for my $collection (@{$options{collection}}) { | |||||||||||||||||||||||||||
| 227 | 1 | my $path = "${plural_resource}/${collection}"; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | _post( | ||||||||||||||||||||||||||||
| 230 | _endpoint( | ||||||||||||||||||||||||||||
| 231 | path => $path, | ||||||||||||||||||||||||||||
| 232 | params => '', | ||||||||||||||||||||||||||||
| 233 | verbs => [qw/POST create/], | ||||||||||||||||||||||||||||
| 234 | loader => $options{load_all}, | ||||||||||||||||||||||||||||
| 235 | 1 | function => "${plural_resource}_${collection}" | |||||||||||||||||||||||||||
| 236 | ) | ||||||||||||||||||||||||||||
| 237 | ); | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | _get( | ||||||||||||||||||||||||||||
| 240 | _endpoint( | ||||||||||||||||||||||||||||
| 241 | path => $path, | ||||||||||||||||||||||||||||
| 242 | params => '', | ||||||||||||||||||||||||||||
| 243 | verbs => [qw/GET get read/], | ||||||||||||||||||||||||||||
| 244 | loader => $options{load_all}, | ||||||||||||||||||||||||||||
| 245 | 1 | function => "${plural_resource}_${collection}" | |||||||||||||||||||||||||||
| 246 | ) | ||||||||||||||||||||||||||||
| 247 | ); | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | _put( | ||||||||||||||||||||||||||||
| 250 | _endpoint( | ||||||||||||||||||||||||||||
| 251 | path => $path, | ||||||||||||||||||||||||||||
| 252 | params => '', | ||||||||||||||||||||||||||||
| 253 | verbs => [qw/PUT update/], | ||||||||||||||||||||||||||||
| 254 | loader => $options{load_all}, | ||||||||||||||||||||||||||||
| 255 | 1 | function => "${plural_resource}_${collection}" | |||||||||||||||||||||||||||
| 256 | ) | ||||||||||||||||||||||||||||
| 257 | ); | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | _del( | ||||||||||||||||||||||||||||
| 260 | _endpoint( | ||||||||||||||||||||||||||||
| 261 | path => $path, | ||||||||||||||||||||||||||||
| 262 | params => '', | ||||||||||||||||||||||||||||
| 263 | verbs => [qw/DELETE delete/], | ||||||||||||||||||||||||||||
| 264 | loader => $options{load_all}, | ||||||||||||||||||||||||||||
| 265 | 1 | function => "${plural_resource}_${collection}" | |||||||||||||||||||||||||||
| 266 | ) | ||||||||||||||||||||||||||||
| 267 | ); | ||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | # save every defined resource if it is referred as a parent in a nested child resource | ||||||||||||||||||||||||||||
| 271 | 8 | $routes{$resource} = "${parent_prefix}/${plural_resource}/${params}"; | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | # restore existing prefix if saved | ||||||||||||||||||||||||||||
| 274 | 8 | ▶ | prefix $old_prefix if $old_prefix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | 9 | }; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 144 | ▶ | sub _debug { $RESOURCE_DEBUG and print @_ } | ||||||||||||||||||||||||||
|
Subroutine
_debug: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 10 | ▶ | sub _post { | ||||||||||||||||||||||||||
|
Subroutine
_post: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 280 | 10 | my ($route, $sub) = @_; | |||||||||||||||||||||||||||
| 281 | 10 | for ($route . '.:format', $route) { | |||||||||||||||||||||||||||
| 282 | 20 | ▶ | _debug("=> POST " .(Dancer::App->current->prefix||'').$_."\n"); | ||||||||||||||||||||||||||
|
Condition100%2/2
'Dancer::App'->current->prefix || ''
MC/DC100%1/1
'Dancer::App'->current->prefix || ''
'Dancer::App'->current->prefix
Truth table100%2/2
A
A= 'Dancer::App'->current->prefix
| |||||||||||||||||||||||||||||
| 283 | 20 | post($_ => $sub); | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 18 | ▶ | sub _get { | ||||||||||||||||||||||||||
|
Subroutine
_get: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 288 | 18 | my ($route, $sub) = @_; | |||||||||||||||||||||||||||
| 289 | 18 | for ($route . '.:format', $route) { | |||||||||||||||||||||||||||
| 290 | 36 | ▶ | _debug("=> GET " .(Dancer::App->current->prefix||'').$_."\n"); | ||||||||||||||||||||||||||
|
Condition100%2/2
'Dancer::App'->current->prefix || ''
MC/DC100%1/1
'Dancer::App'->current->prefix || ''
'Dancer::App'->current->prefix
Truth table100%2/2
A
A= 'Dancer::App'->current->prefix
| |||||||||||||||||||||||||||||
| 291 | 36 | get($_ => $sub); | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 10 | ▶ | sub _put { | ||||||||||||||||||||||||||
|
Subroutine
_put: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 296 | 10 | my ($route, $sub) = @_; | |||||||||||||||||||||||||||
| 297 | 10 | for ($route . '.:format', $route) { | |||||||||||||||||||||||||||
| 298 | 20 | ▶ | _debug("=> PUT " .(Dancer::App->current->prefix||'').$_."\n"); | ||||||||||||||||||||||||||
|
Condition100%2/2
'Dancer::App'->current->prefix || ''
MC/DC100%1/1
'Dancer::App'->current->prefix || ''
'Dancer::App'->current->prefix
Truth table100%2/2
A
A= 'Dancer::App'->current->prefix
| |||||||||||||||||||||||||||||
| 299 | 20 | put($_ => $sub); | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 10 | ▶ | sub _del { | ||||||||||||||||||||||||||
|
Subroutine
_del: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 304 | 10 | my ($route, $sub) = @_; | |||||||||||||||||||||||||||
| 305 | 10 | for ($route . '.:format', $route) { | |||||||||||||||||||||||||||
| 306 | 20 | ▶ | _debug("=> DEL " .(Dancer::App->current->prefix||'').$_."\n"); | ||||||||||||||||||||||||||
|
Condition100%2/2
'Dancer::App'->current->prefix || ''
MC/DC100%1/1
'Dancer::App'->current->prefix || ''
'Dancer::App'->current->prefix
Truth table100%2/2
A
A= 'Dancer::App'->current->prefix
| |||||||||||||||||||||||||||||
| 307 | 20 | del($_ => $sub); | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 48 | ▶ | sub _endpoint { | ||||||||||||||||||||||||||
|
Subroutine
_endpoint: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 312 | 48 | my %opts = @_; | |||||||||||||||||||||||||||
| 313 | 48 | my ($function, $word, $params, $verbs, $load_func) = @opts{qw/function path params verbs loader/}; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 48 | my $package = caller(1); | |||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 48 | my $wrapped; | |||||||||||||||||||||||||||
| 318 | 48 | for my $verb (@$verbs) { | |||||||||||||||||||||||||||
| 319 | # allow both foo_GET and GET_foo | ||||||||||||||||||||||||||||
| 320 | 81 | ▶ | my $func = _function_exists("${package}::${verb}_${function}") || | ||||||||||||||||||||||||||
|
Condition100%3/3
_function_exists("${package}::${verb}_$function") || _function_exists("${package}::${function}_$verb")
MC/DC100%2/2
_function_exists("${package}::${verb}_$function") || _function_exists("${package}::${function}_$verb")
_function_exists("${package}::${verb}_$function")
_function_exists("${package}::${function}_$verb")
Truth table100%3/3
A || B
A= _function_exists("${package}::${verb}_$function")B= _function_exists("${package}::${function}_$verb")
| |||||||||||||||||||||||||||||
| 321 | _function_exists("${package}::${function}_${verb}"); | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 81 | ▶ | if ($func) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 17 | _debug("${package}::${verb}_${function} "); | |||||||||||||||||||||||||||
| 325 | 17 | ▶ | $wrapped = sub { $func->($load_func ? $load_func->() : (), @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 17 | last; # we only want to attach to the first successful verb | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 48 | ▶ | if (not $wrapped) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | 31 | _debug("undef "); | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # if we've gotten this far, no route exists. use a default | ||||||||||||||||||||||||||||
| 335 | 31 | ▶ | $wrapped = sub { status_method_not_allowed('Method not allowed.'); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 48 | ▶ | my $route | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | = $params ? "/${word}/${params}" | ||||||||||||||||||||||||||||
| 340 | : "/${word}"; | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 48 | return ($route, $wrapped); | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 20 | ▶ | register send_entity => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 346 | 20 | my ($entity, $http_code) = @_; | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 20 | ▶ | $http_code ||= 200; | ||||||||||||||||||||||||||
|
Condition50%1/2
$http_code ||= 200
MC/DC0%0/1
$http_code ||= 200
$http_code
Truth table50%1/2
A
A= $http_code
| |||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 20 | status($http_code); | |||||||||||||||||||||||||||
| 351 | 20 | $entity; | |||||||||||||||||||||||||||
| 352 | 9 | }; | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 9 | my %http_codes = ( | |||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | # 1xx | ||||||||||||||||||||||||||||
| 357 | 100 => 'Continue', | ||||||||||||||||||||||||||||
| 358 | 101 => 'Switching Protocols', | ||||||||||||||||||||||||||||
| 359 | 102 => 'Processing', | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | # 2xx | ||||||||||||||||||||||||||||
| 362 | 200 => 'OK', | ||||||||||||||||||||||||||||
| 363 | 201 => 'Created', | ||||||||||||||||||||||||||||
| 364 | 202 => 'Accepted', | ||||||||||||||||||||||||||||
| 365 | 203 => 'Non-Authoritative Information', | ||||||||||||||||||||||||||||
| 366 | 204 => 'No Content', | ||||||||||||||||||||||||||||
| 367 | 205 => 'Reset Content', | ||||||||||||||||||||||||||||
| 368 | 206 => 'Partial Content', | ||||||||||||||||||||||||||||
| 369 | 207 => 'Multi-Status', | ||||||||||||||||||||||||||||
| 370 | 210 => 'Content Different', | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | # 3xx | ||||||||||||||||||||||||||||
| 373 | 300 => 'Multiple Choices', | ||||||||||||||||||||||||||||
| 374 | 301 => 'Moved Permanently', | ||||||||||||||||||||||||||||
| 375 | 302 => 'Found', | ||||||||||||||||||||||||||||
| 376 | 303 => 'See Other', | ||||||||||||||||||||||||||||
| 377 | 304 => 'Not Modified', | ||||||||||||||||||||||||||||
| 378 | 305 => 'Use Proxy', | ||||||||||||||||||||||||||||
| 379 | 307 => 'Temporary Redirect', | ||||||||||||||||||||||||||||
| 380 | 310 => 'Too many Redirect', | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | # 4xx | ||||||||||||||||||||||||||||
| 383 | 400 => 'Bad Request', | ||||||||||||||||||||||||||||
| 384 | 401 => 'Unauthorized', | ||||||||||||||||||||||||||||
| 385 | 402 => 'Payment Required', | ||||||||||||||||||||||||||||
| 386 | 403 => 'Forbidden', | ||||||||||||||||||||||||||||
| 387 | 404 => 'Not Found', | ||||||||||||||||||||||||||||
| 388 | 405 => 'Method Not Allowed', | ||||||||||||||||||||||||||||
| 389 | 406 => 'Not Acceptable', | ||||||||||||||||||||||||||||
| 390 | 407 => 'Proxy Authentication Required', | ||||||||||||||||||||||||||||
| 391 | 408 => 'Request Time-out', | ||||||||||||||||||||||||||||
| 392 | 409 => 'Conflict', | ||||||||||||||||||||||||||||
| 393 | 410 => 'Gone', | ||||||||||||||||||||||||||||
| 394 | 411 => 'Length Required', | ||||||||||||||||||||||||||||
| 395 | 412 => 'Precondition Failed', | ||||||||||||||||||||||||||||
| 396 | 413 => 'Request Entity Too Large', | ||||||||||||||||||||||||||||
| 397 | 414 => 'Request-URI Too Long', | ||||||||||||||||||||||||||||
| 398 | 415 => 'Unsupported Media Type', | ||||||||||||||||||||||||||||
| 399 | 416 => 'Requested range unsatisfiable', | ||||||||||||||||||||||||||||
| 400 | 417 => 'Expectation failed', | ||||||||||||||||||||||||||||
| 401 | 418 => 'Teapot', | ||||||||||||||||||||||||||||
| 402 | 422 => 'Unprocessable entity', | ||||||||||||||||||||||||||||
| 403 | 423 => 'Locked', | ||||||||||||||||||||||||||||
| 404 | 424 => 'Method failure', | ||||||||||||||||||||||||||||
| 405 | 425 => 'Unordered Collection', | ||||||||||||||||||||||||||||
| 406 | 426 => 'Upgrade Required', | ||||||||||||||||||||||||||||
| 407 | 449 => 'Retry With', | ||||||||||||||||||||||||||||
| 408 | 450 => 'Parental Controls', | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | # 5xx | ||||||||||||||||||||||||||||
| 411 | 500 => 'Internal Server Error', | ||||||||||||||||||||||||||||
| 412 | 501 => 'Not Implemented', | ||||||||||||||||||||||||||||
| 413 | 502 => 'Bad Gateway', | ||||||||||||||||||||||||||||
| 414 | 503 => 'Service Unavailable', | ||||||||||||||||||||||||||||
| 415 | 504 => 'Gateway Time-out', | ||||||||||||||||||||||||||||
| 416 | 505 => 'HTTP Version not supported', | ||||||||||||||||||||||||||||
| 417 | 507 => 'Insufficient storage', | ||||||||||||||||||||||||||||
| 418 | 509 => 'Bandwidth Limit Exceeded', | ||||||||||||||||||||||||||||
| 419 | ); | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 9 | for my $code (keys %http_codes) { | |||||||||||||||||||||||||||
| 422 | 486 | my $helper_name = lc($http_codes{$code}); | |||||||||||||||||||||||||||
| 423 | 486 | $helper_name =~ s/[^\w]+/_/gms; | |||||||||||||||||||||||||||
| 424 | 486 | $helper_name = "status_${helper_name}"; | |||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 20 | ▶ | register $helper_name => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 427 | 20 | ▶ | if ($code >= 400 && ref $_[0] eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$code >= 400 and ref $_[0] eq ""
MC/DC50%1/2
$code >= 400 and ref $_[0] eq ""
$code >= 400
ref $_[0] eq ""
Truth table66%2/3
A and B
A= $code >= 400B= ref $_[0] eq ""
| |||||||||||||||||||||||||||||
| 428 | 7 | send_entity({error => $_[0]}, $code); | |||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | else { | ||||||||||||||||||||||||||||
| 431 | 13 | send_entity($_[0], $code); | |||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | 486 | }; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 9 | register_plugin; | |||||||||||||||||||||||||||
| 437 | 9 | 1; | |||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | __END__ | ||||||||||||||||||||||||||||