| 1 | package Dancer::Plugin::CRUD; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | Dancer::Plugin::CRUD - A plugin for writing RESTful apps with Dancer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | Version 1.03 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 15 | our $VERSION = '1.03'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | This plugin is derived from L<Dancer::Plugin::REST|Dancer::Plugin::REST> and helps you write a RESTful webservice with Dancer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSYS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | package MyWebService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | use Dancer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | use Dancer::Plugin::CRUD; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | prepare_serializer_for_format; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | my $userdb = My::UserDB->new(...); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | resource('user', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 'read' => sub { $userdb->find(captures()->{'user_id'}) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # curl http://mywebservice/user/42.json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | { "id": 42, "name": "John Foo", email: "john.foo@example.com"} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # curl http://mywebservice/user/42.yml | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | id: 42 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | name: "John Foo" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | email: "john.foo@example.com" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 15 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 15 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 15 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 15 | ▶ | use Sub::Name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 15 | ▶ | use Text::Pluralize; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 15 | ▶ | use Validate::Tiny (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 15 | our $SUFFIX = '_id'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 15 | my $content_types = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | json => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | yml => 'text/x-yaml', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | xml => 'application/xml', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | dump => 'text/x-perl', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | jsonp => 'text/javascript', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 15 | my %triggers_map = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | get => \&get, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | index => \&get, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | read => \&get, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | post => \&post, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | create => \&post, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | put => \&put, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | update => \&put, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | del => \&del, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | delete => \&del, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | patch => \&patch, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 15 | my %alt_syntax = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | get => 'read', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | post => 'create', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | put => 'update', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | del => 'delete', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 15 | my %http_codes = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # 1xx | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 100 => 'Continue', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 101 => 'Switching Protocols', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 102 => 'Processing', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # 2xx | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 200 => 'OK', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 201 => 'Created', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 202 => 'Accepted', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 203 => 'Non-Authoritative Information', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 204 => 'No Content', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 205 => 'Reset Content', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 206 => 'Partial Content', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 207 => 'Multi-Status', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 210 => 'Content Different', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # 3xx | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 300 => 'Multiple Choices', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 301 => 'Moved Permanently', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 302 => 'Found', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 303 => 'See Other', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 304 => 'Not Modified', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 305 => 'Use Proxy', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 307 => 'Temporary Redirect', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 310 => 'Too many Redirect', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # 4xx | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 400 => 'Bad Request', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 401 => 'Unauthorized', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 402 => 'Payment Required', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 403 => 'Forbidden', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 404 => 'Not Found', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 405 => 'Method Not Allowed', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 406 => 'Not Acceptable', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 407 => 'Proxy Authentication Required', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 408 => 'Request Time-out', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 409 => 'Conflict', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 410 => 'Gone', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 411 => 'Length Required', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 412 => 'Precondition Failed', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 413 => 'Request Entity Too Large', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 414 => 'Request-URI Too Long', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 415 => 'Unsupported Media Type', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 416 => 'Requested range unsatisfiable', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 417 => 'Expectation failed', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 418 => 'Teapot', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 422 => 'Unprocessable entity', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 423 => 'Locked', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 424 => 'Method failure', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 425 => 'Unordered Collection', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 426 => 'Upgrade Required', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 449 => 'Retry With', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 450 => 'Parental Controls', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | # 5xx | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 500 => 'Internal Server Error', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 501 => 'Not Implemented', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 502 => 'Bad Gateway', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 503 => 'Service Unavailable', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 504 => 'Gateway Time-out', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 505 => 'HTTP Version not supported', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 507 => 'Insufficient storage', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 509 => 'Bandwidth Limit Exceeded', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 15 | our $default_serializer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 15 | my $stack = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | sub _generate_sub($) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 45 | ▶ | my %options = %{ shift() }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_sub: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 45 | my $resname = $options{stack}->[-1]->{resname}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 45 | my $rules = [ map { $_->{validation_rules}->{generic} } grep { exists $_->{validation_rules} } reverse @{ $options{stack} } ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 45 | ▶ | if (@$rules > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | push @$rules, $options{stack}->[-1]->{validation_rules}->{$options{action}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 12 | ▶ | if exists $options{stack}->[-1]->{validation_rules}->{$options{action}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | $rules = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 8 | fields => [ map { ( @{ $_->{fields} } ) } grep { exists $_->{fields} } @$rules ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 23 | checks => [ map { ( @{ $_->{checks} } ) } grep { exists $_->{checks} } @$rules ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 12 | filters => [ map { ( @{ $_->{filters} } ) } grep { exists $_->{filters} } @$rules ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 33 | $rules = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 45 | my $chain = [ map { $_->{chain} } grep { exists $_->{chain} } @{ $options{stack} } ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 46 | my @idfields = map { $_->{resname}.$SUFFIX } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 60 | ▶ | grep { (($options{action} =~ m'^(index|create)$') and ($_->{resname} eq $resname)) ? 0 : 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$options{"action"} =~ /^(index|create)$/u and $_->{"resname"} eq $resname
MC/DC100%2/2
$options{"action"} =~ /^(index|create)$/u and $_->{"resname"} eq $resname
$options{"action"} =~ /^(index|create)$/u
$_->{"resname"} eq $resname
Truth table100%3/3
A and B
A= $options{"action"} =~ /^(index|create)$/uB= $_->{"resname"} eq $resname
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 45 | @{ $options{stack} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 45 | my $subname = join('_', $resname, $options{action}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | return subname($subname, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 69 | ▶ | if (defined $rules) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=26.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | my $input = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 24 | %{ params('query') }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 24 | %{ params('body') }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 24 | ▶ | %{ captures() || {} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | my $result = Validate::Tiny->new($input, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | %$rules, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | fields => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | @idfields, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 24 | @{ $rules->{fields} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 24 | ▶ | unless ($result->success) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 24 | status(400); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 24 | return { error => $result->error }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | var validate => $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 45 | my @chain = @$chain; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 45 | ▶ | unless ($options{action} ~~ [qw[ read update delete patch ]]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 16 | pop @chain; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 45 | $_->() for @chain; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 45 | my @ret = $options{coderef}->(map { $_->{resname} } @{ $options{stack} }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 45 | ▶ | if (@ret and defined $ret[0] and ref $ret[0] eq '' and $ret[0] =~ m{^\d{3}$}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition77%7/9
@ret and defined $ret[0] and ref $ret[0] eq "" and $ret[0] =~ /^\d{3}$/u
@ret and defined $ret[0] and ref $ret[0] eq ""
@ret and defined $ret[0]
MC/DC50%2/4
@ret and defined $ret[0] and ref $ret[0] eq "" and $ret[0] =~ /^\d{3}$/u
@ret
defined $ret[0]
ref $ret[0] eq ""
$ret[0] =~ /^\d{3}$/u
Truth table60%3/5
A and B and C and D
A= @retB= defined $ret[0]C= ref $ret[0] eq ""D= $ret[0] =~ /^\d{3}$/u
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | # return ($http_status_code, ...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 1 | ▶ | if ($ret[0] >= 400) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # return ($http_error_code, $error_message) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 1 | status($ret[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 1 | return { error => $ret[1] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # return ($http_success_code, $payload) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | status($ret[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | return $ret[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | } elsif (status eq '200') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # http status wasn't changed yet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 42 | given ($options{action}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 42 | when ('create') { status(201); } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 33 | when ('update') { status(202); } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 27 | when ('delete') { status(202); } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # return payload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 44 | ▶ | return (wantarray ? @ret : $ret[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 45 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | sub _prefix { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 7 | ▶ | my ($prefix, $cb) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prefix: called CC=7 SCAR=22.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 7 | my $app = Dancer::App->current; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 7 | ▶ | my $app_prefix = defined $app->app_prefix ? $app->app_prefix : ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 7 | my $previous = Dancer::App->current->prefix; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 7 | ▶ | if ($app->on_lexical_prefix) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | if (ref $previous eq 'Regexp') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | $app->prefix(qr/${previous}${prefix}/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | my $previous_ = quotemeta($previous); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | $app->prefix(qr/${previous_}${prefix}/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 7 | ▶ | if (ref $app_prefix eq 'Regexp') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | $app->prefix(qr/${app_prefix}${prefix}/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 7 | my $app_prefix_ = quotemeta($app_prefix); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 7 | $app->prefix(qr/${app_prefix_}${prefix}/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 7 | ▶ | if (ref($cb) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 7 | $app->incr_lexical_prefix; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 7 | eval { $cb->() }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 7 | my $e = $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 7 | $app->dec_lexical_prefix; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 7 | $app->prefix($previous); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 7 | ▶ | die $e if $e; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | =head2 C<< prepare_serializer_for_format >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | When this pragma is used, a before filter is set by the plugin to automatically | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | change the serializer when a format is detected in the URI. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | That means that each route you define with a B<:format> token will trigger a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | serializer definition, if the format is known. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | This lets you define all the REST actions you like as regular Dancer route | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | handlers, without explicitly handling the outgoing data format. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | register prepare_serializer_for_format => sub () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 3 | ▶ | my $conf = plugin_setting; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | my $serializers = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 'json' => 'JSON', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 'jsonp' => 'JSONP', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 'yml' => 'YAML', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 'xml' => 'XML', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 'dump' => 'Dumper', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 3 | ▶ | (exists $conf->{serializers} ? %{$conf->{serializers}} : ()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | hook(before => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # remember what was there before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 14 | ▶ | $default_serializer ||= setting('serializer'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.9
Condition33%1/3
$Dancer::Plugin::CRUD::default_serializer ||= setting("serializer")
MC/DC0%0/2
$Dancer::Plugin::CRUD::default_serializer ||= setting("serializer")
$Dancer::Plugin::CRUD::default_serializer
setting("serializer")
Truth table33%1/3
A ||= B
A= $Dancer::Plugin::CRUD::default_serializerB= setting("serializer")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 14 | ▶ | my $format = defined captures() ? captures->{format} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 14 | ▶ | $format ||= param('format') or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$format ||= param("format")
MC/DC100%2/2
$format ||= param("format")
$format
param("format")
Truth table100%3/3
A ||= B
A= $formatB= param("format")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 10 | ▶ | my $serializer = $serializers->{$format} or return halt(Dancer::Error->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | code => 404, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | title => "unsupported format requested", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | message => "unsupported format requested: " . $format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | )->render); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 9 | set(serializer => $serializer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # check if we were supposed to deserialize the request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 9 | Dancer::Serializer->process_request(Dancer::SharedData->request); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 9 | ▶ | content_type($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')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 3 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | hook(after => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | # put it back the way it was | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 13 | ▶ | set(serializer => $default_serializer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 3 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | =head2 C<< resource >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | This keyword lets you declare a resource your application will handle. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | Derived from L<Dancer::Plugin::REST|Dancer::Plugin::REST>, this method has rewritten to provide a more slightly convention. C<get> has been renamed to C<read> and three new actions has been added: C<index>, C<patch>, C<prefix> and C<prefix_id> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | Also, L<Text::Pluralize|Text::Pluralize> is applied to resource name with count=1 for singular variant and count=2 for plural variant. If you don't provide a singular/plural variant (i.e. resource name contains parenthesis) the singular and the plural becomes same. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | The id name is derived from singular resource name, appended with C<_id>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | resource 'user(s)' => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | index => sub { ... }, # return all users | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | read => sub { ... }, # return user where id = captures->{user_id} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | create => sub { ... }, # create a new user with params->{user} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | delete => sub { ... }, # delete user where id = captures->{user_id} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | update => sub { ... }, # update user with params->{user} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | patch => sub { ... }, # patches user with params->{user} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | prefix => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | # prefixed resource in plural | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | # routes are only possible with regex! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | get qr{/foo} => sub { ... }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | prefix_id => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | # prefixed resource in singular with id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | # captures->{user_id} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # routes are only possible with regex! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | get qr{/bar} => sub { ... }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | # this defines the following routes: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | # prefix_id => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | # GET /user/:user_id/bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | # prefix => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | # GET /users/foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | # index => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | # GET /users.:format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | # GET /users | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | # create => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | # POST /user.:format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | # POST /user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | # read => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | # GET /user/:id.:format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # GET /user/:id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | # delete => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | # DELETE /user/:id.:format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | # DELETE /user/:id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | # update => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | # PUT /user/:id.:format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | # PUT /user/:id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | # patch => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | # PATCH /user.:format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | # PATCH /user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | The routes are created in the above order. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | Returns a hash with arrayrefs of all created L<Dancer::Route|Dancer::Route> objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | Hint: resources can be stacked with C<prefix>/C<prefix_id>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | resource foo => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | prefix => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | get '/bar' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | return 'Hi!' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | }, # GET /foo/bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | prefix_id => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | get '/bar' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | return 'Hey '.captures->{foo_id} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | }; # GET /foo/123/bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | resource bar => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | read => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | return 'foo is ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | . captures->{foo_id} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | .' and bar is ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | . captures->{bar_id} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | }; # GET /foo/123/bar/456 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | When is return value is a HTTP status code (three digits), C<status(...)> is applied to it. A second return value may be the value to be returned to the client itself: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | return 200 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | return 404 => 'This object has not been found.' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | return 201 => { ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | The default HTTP status code ("200 OK") differs in some actions: C<create> response with "201 Created", C<delete> and C<update> response with "202 Accepted". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | =head3 Change of suffix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | The appended suffix, C<_id> for default, can be changed by setting C<< $Dancer::Plugin::CRUD::SUFFIX >>. This affects both captures names and the suffix of parameterized C<prefix> method: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | $Dancer::Plugin::CRUD::SUFFIX = 'Id'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | resource 'User' => prefixId => sub { return captures->{'UserId'} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | =head3 Automatic validation of parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | Synopsis: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | resource foo => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | validation => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | generic => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | checks => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | foo_id => Validate::Tiny::is_like(qr{^\d+}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | read => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | $foo_id = var('validate')->data('foo_id'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | The keyword C<validation> specifies rules for L<Validation::Tiny|Validation::Tiny>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | The parameter input resolves to following order: C<params('query')>, C<params('body')>, C<captures()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | The rules and the result of C<Dancer::params()> are applied to C<Validate::Tiny::new> and stored in C<var('validate')>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | The hashref C<validation> accepts seven keywords: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | =item I<generic> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | These are generic rules, used in every action. For the actions I<index> and I<create>, the fields I<<< C<< $resource >>_id >>> are ignored, since they aren't needed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | =item I<index>, I<create>, I<read>, I<update>, I<delete> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | These rules are merged together with I<generic>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | =item I<prefix>, I<prefix_id> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | These rules are merged together with I<generic>, but they can only used when C<resource()> is used in the prefix subs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | =item I<wrap> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | These rules apply when in a prefix or prefix_id routine the I<wrap> keyword is used: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | resource foo => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | validation => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | wrap => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | GET => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | bar => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | fields => [qw[ name ]] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | prefix => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | wrap GET => bar => sub { ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | The id-fields (I<<< C<< $resource >>_id >>>, ...) are automatically prepended to the I<fields> param of Validate::Tiny. There is no need to define them especially. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | An advantage is the feature of stacking resources and to define validation rules only once. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | resource foo => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | validation => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | generic => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | checks => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | foo_id => Validate::Tiny::is_like(qr{^\d+}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | prefix_id => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | resource bar => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | validation => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | generic => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | checks => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | bar_id => Validate::Tiny::is_like(qr{^\d+}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | read => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | $foo_id = var('validate')->data('foo_id'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | $bar_id = var('validate')->data('foo_id'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | =head3 Chaining actions together | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | To avoid redundant code, the keyword I<chain> may used to define a coderef executing every times the resource (and possible parent resources) is triggered, irrespective of the method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | resource foo => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | chain => sub { var onetwothree => 123 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | index => sub { return var('onetwothree') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | prefix_id => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | resource bar => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | chain => sub { var fourfivesix => 456 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | index => sub { return var('onetwothree').var('fourfivesix') }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | When resource I<foo> is triggered, the variable C<onetwothree> is set to 123. When resource I<bar> is triggered, the variable C<onetwothree> is set to 123 and, of course, C<fourfivesix> is set to 456. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | This is useful to obtain parent objects from DB and store it into the var stack. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | B<WARNING>: This feature may change in a future release. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | register(resource => sub ($%) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 14 | ▶ | my $resource = my $resource1 = my $resource2 = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | 14 | my %triggers = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | 14 | my $c = quotemeta '()|{}'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | 14 | ▶ | if ($resource =~ m{[$c]}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | 4 | $resource1 = pluralize($resource1, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 4 | $resource2 = pluralize($resource2, 2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 14 | my %options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 14 | push @$stack => \%options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 14 | $options{resname} = $resource1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 14 | my $altsyntax = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | 14 | ▶ | if (exists $triggers{altsyntax}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 1 | $altsyntax = delete $triggers{altsyntax}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 14 | my $idregex = qr{[^\/\.\:\?]+}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 14 | ▶ | if (exists $triggers{idregex}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 0 | $idregex = delete $triggers{idregex}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 14 | $options{prefix} = qr{/\Q$resource2\E}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | 14 | $options{prefix_id} = qr{/\Q$resource1\E/(?<$resource1$SUFFIX>$idregex)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | 14 | ▶ | if (exists $triggers{validation}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 3 | $options{validation_rules} = delete $triggers{validation}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 14 | ▶ | if (exists $triggers{chain}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 2 | $options{chain} = delete $triggers{chain}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 14 | ▶ | if (exists $triggers{'prefix'.$SUFFIX}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 6 | my $subref = delete $triggers{'prefix'.$SUFFIX}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 6 | $options{prefixed_with_id} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 6 | ▶ | my @prefixes = map { $_->{prefixed_with_id} ? $_->{prefix_id} : $_->{prefix} } grep { exists $_->{prefix} } @$stack; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 6 | local $" = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | 6 | _prefix(qr{@prefixes}, $subref); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 6 | delete $options{prefixed_with_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 14 | ▶ | if (exists $triggers{prefix}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 1 | my $subref = delete $triggers{'prefix'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | 1 | $options{prefixed_with_id} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 1 | ▶ | my @prefixes = map { $_->{prefixed_with_id} ? $_->{prefix_id} : $_->{prefix} } grep { exists $_->{prefix} } @$stack; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 1 | local $" = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | 1 | _prefix(qr{@prefixes}, $subref); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 1 | delete $options{prefixed_with_id}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | 14 | my %routes; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 14 | foreach my $action (qw(index create read delete update patch)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 84 | ▶ | next unless exists $triggers{$action}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 40 | my $route; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 40 | given ($action) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 40 | when ('index') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 7 | $route = qr{/\Q$resource2\E}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 33 | when ('create') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 7 | $route = qr{/\Q$resource1\E}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 26 | default { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 26 | $route = qr{/\Q$resource1\E/(?<$resource1$SUFFIX>$idregex)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | my $sub = _generate_sub({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | stack => $stack, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | action => $action, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 40 | coderef => $triggers{$action}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 40 | $routes{$action} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 40 | ▶ | if ($altsyntax) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 5 | push @{$routes{$action}} => $triggers_map{ get }->(qr{$route/\Q$action\E\.(?<format>json|jsonp|yml|xml|dump)} => $sub); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 5 | push @{$routes{$action}} => $triggers_map{ get }->(qr{$route/\Q$action\E} => $sub); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 40 | push @{$routes{$action}} => $triggers_map{$action}->(qr{$route\.(?<format>json|jsonp|yml|xml|dump)} => $sub); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 40 | push @{$routes{$action}} => $triggers_map{$action}->( $route => $sub); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 14 | pop @$stack; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | 14 | return %routes; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | 15 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | =head2 C<< wrap >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | This keyword wraps validation rules and format accessors. For return values see C<resource>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | Synopsis: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | resource foo => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | prefix_id => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | wrap GET => bar => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | # same as get('/bar', sub { ... }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | # and get('/bar.:format', sub { ... }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | # var('validate') is also availble, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | # when key 'validation' is defined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | I<wrap> uses the same wrapper as for the actions in I<resource>. Any beviour there also applies here. For a better explaination, these resolves to the same routes: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | resource foo => read => sub { ... }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | wrap read => foo => sub { ... }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | The first argument is an CRUD action (I<index>, I<create>, I<read>, I<update>, I<delete>) or a HTTP method (I<GET>, I<POST>, I<PUT>, I<DELETE>, I<PATCH>) and is case-insensitve. The second argument is a route name. A leading slash will be prepended if the route contains to slashes. The third argument is the well known coderef. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | Please keep in mind that I<wrap> creates two routes: I<<< /C<< $route >> >>> and I<<< /C<< $route >>.:format >>>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | Returns a list of all created L<Dancer::Route|Dancer::Route> objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | register(wrap => sub($$$) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 5 | ▶ | my ($action, $route, $coderef) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | 5 | ▶ | my @route = grep { defined and length } split m{/+}, $route; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 5 | ▶ | my $parent = @$stack ? $stack->[-1] : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 5 | foreach my $route (@route) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | 5 | push @$stack => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | resname => $route | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | 5 | ▶ | if (defined $parent) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 5 | ▶ | if (exists $parent->{validation_rules} and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
exists $parent->{"validation_rules"} and exists $parent->{"validation_rules"}{"wrap"} and exists $parent->{"validation_rules"}{"wrap"}{$action} and exists $parent->{"validation_rules"}{"wrap"}{$action}{$route}
exists $parent->{"validation_rules"} and exists $parent->{"validation_rules"}{"wrap"} and exists $parent->{"validation_rules"}{"wrap"}{$action}
exists $parent->{"validation_rules"} and exists $parent->{"validation_rules"}{"wrap"}
MC/DC0%0/4
exists $parent->{"validation_rules"} and exists $parent->{"validation_rules"}{"wrap"} and exists $parent->{"validation_rules"}{"wrap"}{$action} and exists $parent->{"validation_rules"}{"wrap"}{$action}{$route}
exists $parent->{"validation_rules"}
exists $parent->{"validation_rules"}{"wrap"}
exists $parent->{"validation_rules"}{"wrap"}{$action}
exists $parent->{"validation_rules"}{"wrap"}{$action}{$route}
Truth table20%1/5
A and B and C and D
A= exists $parent->{"validation_rules"}B= exists $parent->{"validation_rules"}{"wrap"}C= exists $parent->{"validation_rules"}{"wrap"}{$action}D= exists $parent->{"validation_rules"}{"wrap"}{$action}{$route}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | exists $parent->{validation_rules}->{wrap} and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | exists $parent->{validation_rules}->{wrap}->{$action} and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | exists $parent->{validation_rules}->{wrap}->{$action}->{$route} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 5 | $stack->[-1]->{validation_rules} = { lc($action) => $parent->{validation_rules}->{wrap}->{$action}->{$route} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | 5 | my $sub = _generate_sub({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | action => lc($action), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | stack => $stack, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | coderef => $coderef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | 5 | pop @$stack for @route; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 5 | my @routes; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 5 | push @routes => $triggers_map{lc($action)}->(qr{/\Q$route\E\.(?<format>json|jsonp|yml|xml|dump)} => $sub); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | 5 | push @routes => $triggers_map{lc($action)}->(qr{/\Q$route\E} => $sub); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | 5 | return @routes; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | 15 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | =head2 helpers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | Some helpers are available. This helper will set an appropriate HTTP status for you. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | =head3 status_ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | status_ok({users => {...}}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | Set the HTTP status to 200 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | =head3 status_created | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | status_created({users => {...}}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | Set the HTTP status to 201 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | =head3 status_accepted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | status_accepted({users => {...}}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | Set the HTTP status to 202 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | =head3 status_bad_request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | status_bad_request("user foo can't be found"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | Set the HTTP status to 400. This function as for argument a scalar that will be used under the key B<error>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | =head3 status_not_found | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | status_not_found("users doesn't exists"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | Set the HTTP status to 404. This function as for argument a scalar that will be used under the key B<error>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | register send_entity => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | # entity, status_code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 2 | ▶ | status($_[1] || 200); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$_[1] || 200
MC/DC0%0/1
$_[1] || 200
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | 2 | $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | 15 | for my $code (keys %http_codes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | 810 | my $helper_name = lc($http_codes{$code}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | 810 | $helper_name =~ s/[^\w]+/_/gms; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | 810 | $helper_name = "status_${helper_name}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | register $helper_name => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | 2 | ▶ | if ($code >= 400) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | 2 | send_entity({error => $_[0]}, $code); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 0 | send_entity($_[0], $code); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | 810 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | =head1 LICENCE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | This module is released under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | This module has been rewritten by David Zurborg C<< <zurborg@cpan.org> >>, based on code written by Alexis Sukrieh C<< <sukria@sukria.net> >> and Franck Cuny. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | L<Dancer> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | L<http://en.wikipedia.org/wiki/Representational_State_Transfer> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | L<Dancer::Plugin::REST> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | L<Text::Pluralize> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | David Zurborg <zurborg@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | Alexis Sukrieh <sukria@sukria.net> (Author of Dancer::Plugin::REST) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | Franck Cuny <franckc@cpan.org> (Author of Dancer::Plugin::REST) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | Please report any bugs or feature requests trough my project management tool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | at L<http://development.david-zurb.org/projects/libdancer-plugin-crud-perl/issues/new>. I | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | will be notified, and then you'll automatically be notified of progress on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | perldoc Dancer::Plugin::CRUD | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | =item * Redmine: Homepage of this module | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | L<http://development.david-zurb.org/projects/libdancer-plugin-crud-perl> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-CRUD> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | L<http://annocpan.org/dist/Dancer-Plugin-CRUD> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | L<http://cpanratings.perl.org/d/[Dancer-Plugin-CRUD> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | L<http://search.cpan.org/dist/Dancer-Plugin-CRUD/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | This software is copyright (c) 2014 by David Zurborg <zurborg@cpan.org>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | 15 | register_plugin; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||