blib/lib/Dancer/Plugin/REST.pm

filter: Statementstmt 100% 56 / 56 Branchbran 87.5% 14 / 16 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 84.3% 86 / 102 CC 14 SCAR 12.5
CC
14
Cov
89%
__ANON__
6.0
__ANON__
5.4
__ANON__
3.2
CRAP 3.5
« Build ↑ Summary
1 package Dancer::Plugin::REST;
2 BEGIN {
3 9 ▶ $Dancer::Plugin::REST::AUTHORITY = 'cpan:SUKRIA';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 # ABSTRACT: A plugin for writing RESTful apps with Dancer
6 9 $Dancer::Plugin::REST::VERSION = '0.11';
7 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 my $content_types = {
15 json => 'application/json',
16 yml => 'text/x-yaml',
17 xml => 'application/xml',
18 };
19  
20 9 our $default_serializer;
21  
22 register prepare_serializer_for_format => sub {
23 4 ▶ my $conf = plugin_setting;
Subroutine __ANON__: called CC=3 SCAR=11.5
24 my $serializers = (
25 ($conf && exists $conf->{serializers})
26 ? $conf->{serializers}
27 4 ▶ : { 'json' => 'JSON',
Branch: 1 branch
BranchTrueFalseTotal
$conf && exists $conf->{'serializers'} ? : 0 4 4
Condition33%1/3
$conf and exists $conf->{"serializers"}
!ll&&!rl&&r
040
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"}
ABresult
0X0
100
111
28 'yml' => 'YAML',
29 'xml' => 'XML',
30 'dump' => 'Dumper',
31 }
32 );
33  
34 hook 'before' => sub {
35 # remember what was there before
36 15 ▶ $default_serializer ||= setting 'serializer';
Subroutine __ANON__: called CC=5 SCAR=16.8
Condition33%1/3
$Dancer::Plugin::REST::default_serializer ||= setting("serializer")
l!l&&r!l&&!r
0015
MC/DC0%0/2
$Dancer::Plugin::REST::default_serializer ||= setting("serializer")
$Dancer::Plugin::REST::default_serializer setting("serializer")
Truth table33%1/3
A ||= B
A= $Dancer::Plugin::REST::default_serializerB= setting("serializer")
ABresult
1X1
011
000
37  
38 15 ▶ my $format = params->{'format'} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $format = params()->{"format"} or return 4 11 15
39  
40 11 ▶ my $serializer = $serializers->{$format}
Branch: 1 branch
BranchTrueFalseTotal
my $serializer = $serializers->{$format} or return halt("Dancer::Error"->new("code", 404, "title", "unsupported format requested", "message", 'unsupported format requested: ' . $format)->render) 1 10 11
41 or return halt(
42 Dancer::Error->new(
43 code => 404,
44 title => "unsupported format requested",
45 message => "unsupported format requested: " . $format
46 )->render
47 );
48  
49 10 set serializer => $serializer;
50  
51 # check if we were supposed to deserialize the request
52 10 Dancer::Serializer->process_request(
53 Dancer::SharedData->request
54 );
55  
56 10 ▶ content_type $content_types->{$format} || setting('content_type');
Condition33%1/3
$content_types->{$format} || setting('content_type')
l!l&&r!l&&!r
1000
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')
ABresult
1X1
011
000
57 4 };
58  
59 hook after => sub {
60 # put it back the way it was
61 14 ▶ set serializer => $default_serializer;
Subroutine __ANON__: called CC=1 SCAR=0.0
62 }
63 4 };
64  
65 9 my %triggers_map = (
66 get => \&get,
67 update => \&put,
68 create => \&post,
69 delete => \&del,
70 );
71  
72 register resource => sub {
73 4 ▶ croak "resource invoked without arguments" unless @_;
Subroutine __ANON__: called CC=6 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
74  
75 4 my ($resource, %triggers) = @_;
76  
77 4 ▶ while( my( $t, $sub ) = each %triggers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %triggers) 13 3 16
78 13 ▶ my $method = $triggers_map{$t}
Branch: 1 branch
BranchTrueFalseTotal
my $method = $triggers_map{$t} or croak("action '${t}' not recognized") 1 12 13
79 or croak "action '$t' not recognized";
80  
81 12 ▶ if ( $t eq 'create' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq 'create') { } 3 9 12
82 3 $method->( "/${resource}" => $triggers{$t} );
83 3 $method->( "/${resource}.:format" => $triggers{$t} );
84 }
85 else {
86 9 for my $ext ( '.:format', '' ) {
87 18 $method->( "/${resource}/:id$ext" => $triggers{$t} );
88 }
89 }
90 }
91  
92 9 };
93  
94 register send_entity => sub {
95 # entity, status_code
96 8 ▶ status($_[1] || 200);
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$_[1] || 200
l!l
80
MC/DC0%0/1
$_[1] || 200
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
97 8 $_[0];
98 9 };
99  
100 9 my %http_codes = (
101  
102 # 1xx
103 100 => 'Continue',
104 101 => 'Switching Protocols',
105 102 => 'Processing',
106  
107 # 2xx
108 200 => 'OK',
109 201 => 'Created',
110 202 => 'Accepted',
111 203 => 'Non-Authoritative Information',
112 204 => 'No Content',
113 205 => 'Reset Content',
114 206 => 'Partial Content',
115 207 => 'Multi-Status',
116 210 => 'Content Different',
117  
118 # 3xx
119 300 => 'Multiple Choices',
120 301 => 'Moved Permanently',
121 302 => 'Found',
122 303 => 'See Other',
123 304 => 'Not Modified',
124 305 => 'Use Proxy',
125 307 => 'Temporary Redirect',
126 310 => 'Too many Redirect',
127  
128 # 4xx
129 400 => 'Bad Request',
130 401 => 'Unauthorized',
131 402 => 'Payment Required',
132 403 => 'Forbidden',
133 404 => 'Not Found',
134 405 => 'Method Not Allowed',
135 406 => 'Not Acceptable',
136 407 => 'Proxy Authentication Required',
137 408 => 'Request Time-out',
138 409 => 'Conflict',
139 410 => 'Gone',
140 411 => 'Length Required',
141 412 => 'Precondition Failed',
142 413 => 'Request Entity Too Large',
143 414 => 'Request-URI Too Long',
144 415 => 'Unsupported Media Type',
145 416 => 'Requested range unsatisfiable',
146 417 => 'Expectation failed',
147 418 => 'Teapot',
148 422 => 'Unprocessable entity',
149 423 => 'Locked',
150 424 => 'Method failure',
151 425 => 'Unordered Collection',
152 426 => 'Upgrade Required',
153 449 => 'Retry With',
154 450 => 'Parental Controls',
155  
156 # 5xx
157 500 => 'Internal Server Error',
158 501 => 'Not Implemented',
159 502 => 'Bad Gateway',
160 503 => 'Service Unavailable',
161 504 => 'Gateway Time-out',
162 505 => 'HTTP Version not supported',
163 507 => 'Insufficient storage',
164 509 => 'Bandwidth Limit Exceeded',
165 );
166  
167 9 for my $code (keys %http_codes) {
168 486 my $helper_name = lc($http_codes{$code});
169 486 $helper_name =~ s/[^\w]+/_/gms;
170 486 $helper_name = "status_${helper_name}";
171  
172 register $helper_name => sub {
173 8 ▶ if ($code >= 400) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($code >= 400) { } 3 5 8
174 3 send_entity({error => $_[0]}, $code);
175 }
176 else {
177 5 send_entity($_[0], $code);
178 }
179 486 };
180 }
181  
182 9 register_plugin;
183 9 1;
184  
185 __END__
« Build ↑ Summary