blib/lib/Raisin.pm

filter: Statementstmt 84.5% 142 / 168 Branchbran 66.6% 32 / 48 Conditioncond 28.5% 4 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 94.2% 33 / 35 Podpod 36.8% 7 / 19 totaltotal 74.6% 218 / 292 CC 32 SCAR 24.9
CC
32
Cov
77%
psgi
45.5
session
6.0
before_finalize
4.8
CRAP 12.0
« blib/lib/Raisin/Plugin/Swagger.pm ↑ Summary blib/lib/Raisin/Param.pm »
1 #!perl
2 #PODNAME: Raisin
3 #ABSTRACT: A REST API microframework for Perl.
4  
5 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Raisin;
9 14 $Raisin::VERSION = '0.94';
10 14 ▶use Carp qw(croak carp longmess);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 14 ▶use Raisin::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use Raisin::Routes;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use Raisin::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 14 ▶use Raisin::Middleware::Formatter;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 14 ▶use Raisin::Encoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 14 ▶use Raisin::Decoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 14 ▶use Plack::Util::Accessor qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
24 middleware
25 mounted
26 routes
27  
28 decoder
29 encoder
30 14 );
31  
32 9 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
33 9 my ($class, %args) = @_;
34  
35 9 my $self = bless { %args }, $class;
36  
37 9 $self->middleware({});
38 9 $self->mounted([]);
39 9 $self->routes(Raisin::Routes->new);
40  
41 9 $self->decoder(Raisin::Decoder->new);
42 9 $self->encoder(Raisin::Encoder->new);
43  
44 9 $self;
45 }
46  
47 2 ▶sub mount_package {
Subroutine mount_package: called CC=1 SCAR=0.0
Pod undocumented
48 2 my ($self, $package) = @_;
49 2 push @{ $self->{mounted} }, $package;
50 2 Plack::Util::load_class($package);
51 }
52  
53 10 ▶sub load_plugin {
Subroutine load_plugin: called CC=2 SCAR=6.9
Pod undocumented
54 10 my ($self, $name, @args) = @_;
55 10 ▶ return if $self->{loaded_plugins}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"loaded_plugins"}{$name} 2 8 10
56  
57 8 my $class = Plack::Util::load_class($name, 'Raisin::Plugin');
58 8 my $module = $self->{loaded_plugins}{$name} = $class->new($self);
59  
60 8 $module->build(@args);
61 }
62  
63 2 ▶sub add_middleware {
Subroutine add_middleware: called CC=1 SCAR=0.0
Pod undocumented
64 2 my ($self, $name, @args) = @_;
65 2 $self->{middleware}{$name} = \@args;
66 }
67  
68 # Routes
69 51 ▶sub add_route {
Subroutine add_route: called CC=1 SCAR=0.0
Pod undocumented
70 51 my ($self, %params) = @_;
71 51 $self->routes->add(%params);
72 }
73  
74 # Resource description
75 9 ▶sub resource_desc {
Subroutine resource_desc: called CC=2 SCAR=6.9
Pod undocumented
76 9 my ($self, $ns, $desc) = @_;
77 9 ▶ $self->{resource_desc}{$ns} = $desc if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 7 2 9
78 9 $self->{resource_desc}{$ns};
79 }
80  
81 # Hooks
82 128 ▶sub hook {
Subroutine hook: called CC=2 SCAR=6.9
Pod undocumented
83 128 my ($self, $name) = @_;
84 128 ▶ $self->{hooks}{$name} || sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$self->{"hooks"}{$name} or sub { } 4 124 128
85 }
86  
87 4 ▶sub add_hook {
Subroutine add_hook: called CC=1 SCAR=0.0
Pod undocumented
88 4 my ($self, $name, $code) = @_;
89 4 $self->{hooks}{$name} = $code;
90 }
91  
92 # Application
93 7 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod documented
94 7 my $self = shift;
95 7 ▶ my $psgi = sub { $self->psgi(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
96  
97 7 $self->{allowed_methods} = $self->generate_allowed_methods;
98  
99 # Add middleware
100 7 for my $class (keys %{ $self->{middleware} }) {
101 # Make sure the middleware was not already loaded
102 2 ▶ next if $self->{_loaded_middleware}->{$class}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_loaded_middleware"}{$class}++ 0 2 2
103  
104 2 my $mw = Plack::Util::load_class($class, 'Plack::Middleware');
105 2 my $args = $self->{middleware}{$class};
106 2 $psgi = $mw->wrap($psgi, @$args);
107 }
108  
109 7 $psgi = Raisin::Middleware::Formatter->wrap(
110 $psgi,
111 default_format => $self->default_format,
112 format => $self->format,
113 decoder => $self->decoder,
114 encoder => $self->encoder,
115 raisin => $self,
116 );
117  
118 # load fallback logger (Raisin::Logger)
119 7 $self->load_plugin('Logger', fallback => 1);
120  
121 7 return $psgi;
122 }
123  
124 7 ▶sub generate_allowed_methods {
Subroutine generate_allowed_methods: called CC=2 SCAR=7.1
Pod undocumented
125 7 my $self = shift;
126  
127 7 my %allowed_methods_by_endpoint;
128  
129 # `options` for each `path`
130 7 for my $path (keys %{ $self->routes->list }) {
131 my $methods = join ', ',
132 11 sort(keys(%{ $self->routes->list->{$path} }), 'OPTIONS');
133  
134 $self->add_route(
135 method => 'OPTIONS',
136 path => $path,
137 0 ▶ code => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 $self->res->headers([Allow => $methods]);
139 0 undef;
140 },
141 11 );
142  
143 11 $allowed_methods_by_endpoint{$path} = $methods;
144 }
145  
146 7 \%allowed_methods_by_endpoint;
147 }
148  
149 32 ▶sub psgi {
Subroutine psgi: called CC=15 SCAR=38.2
Pod undocumented
150 32 my ($self, $env) = @_;
151  
152 # New for each response
153 32 my $req = $self->req(Raisin::Request->new($env));
154 32 my $res = $self->res(Plack::Response->new);
155  
156 # Generate API description
157 32 ▶ if ($self->can('swagger_build_spec')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("swagger_build_spec")) 19 13 32
158 19 $self->swagger_build_spec;
159 }
160  
161 my $ret = eval {
162 32 $self->hook('before')->($self);
163  
164 # Find a route
165 32 my $route = $self->routes->find($req->method, $req->path);
166 # The requested path exists but requested method not
167 32 ▶ if (!$route && $self->{allowed_methods}{ $req->path }) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $route and $self->{'allowed_methods'}{$req->path}) { } 0 32 32
elsif (not $route) { } 0 32 32
Condition33%1/3
not $route and $self->{"allowed_methods"}{$req->path}
!ll&&!rl&&r
3200
MC/DC0%0/2
not $route and $self->{"allowed_methods"}{$req->path}
not $route $self->{"allowed_methods"}{$req->path}
Truth table33%1/3
not(A) and B
A= not $routeB= $self->{"allowed_methods"}{$req->path}
ABresult
0X0
100
111
168 0 $res->status(HTTP_METHOD_NOT_ALLOWED);
169 0 return $res->finalize;
170 }
171 # Nothing found
172 elsif (!$route) {
173 0 $res->status(HTTP_NOT_FOUND);
174 0 return $res->finalize;
175 }
176  
177 32 my $code = $route->code;
178 32 ▶ if (!$code || ($code && ref($code) ne 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $code or $code and ref $code ne "CODE") 0 32 32
Condition33%2/6
not $code or $code and ref $code ne "CODE"
l!l&&r!l&&!r
0032
$code and ref $code ne "CODE"
!ll&&!rl&&r
0320
MC/DC0%0/3
not $code or $code and ref $code ne "CODE"
not $code $code ref $code ne "CODE"
Truth table25%1/4
not(A) or B and C
A= not $codeB= $codeC= ref $code ne "CODE"
ABCresult
1XX1
0111
00X0
0100
179 0 $self->log(error => "route ${ \$req->path } returns nothing or not CODE");
180  
181 0 $res->status(HTTP_INTERNAL_SERVER_ERROR);
182 0 $res->body('Internal error');
183  
184 0 return $res->finalize;
185 }
186  
187 32 $self->hook('before_validation')->($self);
188  
189 # Validation and coercion of declared params
190 32 ▶ if (!$req->prepare_params($route->params, $route->named)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->prepare_params($route->params, $route->named)) 0 32 32
191 0 $res->status(HTTP_BAD_REQUEST);
192 0 $res->body('Invalid Parameters');
193 0 return $res->finalize;
194 }
195  
196 32 $self->hook('after_validation')->($self);
197  
198 # Evaluate an endpoint
199 32 my $data = $code->($req->declared_params);
200 32 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 32 0 32
201 # Delayed response
202 32 ▶ return $data if ref($data) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "CODE" 0 32 32
203  
204 32 $res->body($data);
205 }
206  
207 32 $self->hook('after')->($self);
208  
209 32 1;
210 32 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $ret = eval { do { $self->hook("before")->($self); my $route = $self->routes->find($req->method, $req->path); if (not $route and $self->{"allowed_methods"}{$req->path}) { $res->status(405); return $res->finalize; } elsif (not $route) { $res->status(404); return $res->finalize; } ; my $code = $route->code; if (not $code or $code and ref $code ne "CODE") { $self->log("error", "route ${\$req->path;} returns nothing or not CODE"); $res->status(500); $res->body("Internal error"); return $res->finalize; } ; $self->hook("before_validation")->($self); unless ($req->prepare_params($route->params, $route->named)) { $res->status(400); $res->body("Invalid Parameters"); return $res->finalize; } ; $self->hook("after_validation")->($self); my $data = &$code($req->declared_params); if (defined $data) { return $data if ref $data eq "CODE"; $res->body($data); } ; $self->hook("after")->($self); 1 } } or do { my($e) = longmess($@); $self->log("error", $e); my $msg = $ENV{"PLACK_ENV"} && $ENV{"PLACK_ENV"} eq "deployment" ? "Internal Error" : $e; $res->status(500); $res->body($msg) } 32 0 32
211 0 my ($e) = longmess($@);
212 0 $self->log(error => $e);
213  
214 my $msg = $ENV{PLACK_ENV}
215 0 ▶ && $ENV{PLACK_ENV} eq 'deployment' ? 'Internal Error' : $e;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PLACK_ENV'} && $ENV{'PLACK_ENV'} eq 'deployment' ? : 0 0 0
Condition0%0/3
$ENV{"PLACK_ENV"} and $ENV{"PLACK_ENV"} eq "deployment"
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"PLACK_ENV"} and $ENV{"PLACK_ENV"} eq "deployment"
$ENV{"PLACK_ENV"} $ENV{"PLACK_ENV"} eq "deployment"
Truth table0%0/3
A and B
A= $ENV{"PLACK_ENV"}B= $ENV{"PLACK_ENV"} eq "deployment"
ABresult
0X0
100
111
216  
217 0 $res->status(HTTP_INTERNAL_SERVER_ERROR);
218 0 $res->body($msg);
219 };
220  
221 32 ▶ if (ref($ret) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ret eq "CODE") 0 32 32
222 0 return $ret;
223 }
224  
225 32 $self->finalize;
226 }
227  
228 # Finalize response
229 32 ▶sub before_finalize {
Subroutine before_finalize: called CC=4 SCAR=15.6
Pod undocumented
230 32 my $self = shift;
231  
232 32 ▶ $self->res->status(HTTP_OK) unless $self->res->status;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->res->status 32 0 32
233 32 ▶ $self->res->header('X-Framework' => 'Raisin ' . (__PACKAGE__->VERSION || 'dev'));
Condition50%1/2
__PACKAGE__->VERSION || 'dev'
l!l
320
MC/DC0%0/1
__PACKAGE__->VERSION || 'dev'
__PACKAGE__->VERSION
Truth table50%1/2
A
A= __PACKAGE__->VERSION
Aresult
11
00
234  
235 32 ▶ if ($self->api_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->api_version) 0 32 32
236 0 $self->res->header('X-API-Version' => $self->api_version);
237 }
238 }
239  
240 32 ▶sub finalize {
Subroutine finalize: called CC=1 SCAR=0.0
Pod undocumented
241 32 my $self = shift;
242 32 $self->before_finalize;
243 32 $self->res->finalize;
244 }
245  
246 # Application defaults
247 12 ▶sub default_format {
Subroutine default_format: called CC=3 SCAR=11.0
Pod documented
248 12 my ($self, $format) = @_;
249  
250 12 ▶ if ($format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 3 9 12
251 3 $self->{default_format} = $format;
252 }
253  
254 12 ▶ $self->{default_format} || 'yaml';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"default_format"} or "yaml" 6 6 12
255 }
256  
257 12 ▶sub format {
Subroutine format: called CC=3 SCAR=11.1
Pod documented
258 12 my ($self, $format) = @_;
259  
260 12 ▶ if ($format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 2 10 12
261 2 my @decoders = keys %{ $self->decoder->all };
262  
263 2 ▶ if (grep { lc($format) eq $_ } @decoders) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {lc $format eq $_;} @decoders) { } 2 0 2
264 2 $self->{format} = lc $format;
265 2 $self->default_format(lc $format);
266 }
267 else {
268 0 carp 'Invalid format, choose one of: ', join(', ', @decoders);
269 }
270 }
271  
272 12 $self->{format};
273 }
274  
275 40 ▶sub api_version {
Subroutine api_version: called CC=2 SCAR=6.9
Pod documented
276 40 my ($self, $version) = @_;
277 40 ▶ $self->{version} = $version if $version;
Branch: 1 branch
BranchTrueFalseTotal
if $version 3 37 40
278 $self->{version}
279 40 }
280  
281 # Request and Response and shortcuts
282 38 ▶sub req {
Subroutine req: called CC=2 SCAR=6.9
Pod documented
283 38 my ($self, $req) = @_;
284 38 ▶ $self->{req} = $req if $req;
Branch: 1 branch
BranchTrueFalseTotal
if $req 33 5 38
285 38 $self->{req};
286 }
287  
288 177 ▶sub res {
Subroutine res: called CC=2 SCAR=6.9
Pod documented
289 177 my ($self, $res) = @_;
290 177 ▶ $self->{res} = $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 34 143 177
291 177 $self->{res};
292 }
293  
294 0 ▶sub session {
Subroutine session: not called CC=2 SCAR=17.9
Pod documented
295 0 my $self = shift;
296  
297 0 ▶ if (not $self->req->env->{'psgix.session'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->req->env->{"psgix.session"}) 0 0 0
298 0 croak "No Session middleware wrapped";
299 }
300  
301 0 $self->req->session;
302 }
303  
304 14 1;
305  
306 __END__
« blib/lib/Raisin/Plugin/Swagger.pm ↑ Summary blib/lib/Raisin/Param.pm »