blib/lib/Dancer/Plugin/REST.pm

filter: Statementstmt 100% 59 / 59 Branchbran 87.5% 14 / 16 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 84.9% 90 / 106 CC 14 SCAR 12.3
CC
14
Cov
90%
__ANON__
6.0
__ANON__
5.4
__ANON__
3.2
CRAP 3.4
↑ Summary
1 package Dancer::Plugin::REST;
2 9 our $AUTHORITY = 'cpan:SUKRIA';
3 # ABSTRACT: A plugin for writing RESTful apps with Dancer
4 9 $Dancer::Plugin::REST::VERSION = '0.12';
5 9 ▶use 5.24.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
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 9 ▶use Dancer::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 my $content_types = {
16 json => 'application/json',
17 yml => 'text/x-yaml',
18 xml => 'application/xml',
19 };
20  
21 9 our $default_serializer;
22  
23 4 ▶register prepare_serializer_for_format => sub {
Subroutine __ANON__: called CC=3 SCAR=11.5
24 4 my $conf = plugin_setting;
25 my $serializers = (
26 ($conf && exists $conf->{serializers})
27 ? $conf->{serializers}
28 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
29 'yml' => 'YAML',
30 'xml' => 'XML',
31 'dump' => 'Dumper',
32 }
33 );
34  
35 15 ▶ hook 'before' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
36 # remember what was there before
37 15 ▶ $default_serializer ||= setting 'serializer';
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
38  
39 15 ▶ my $format = params->{'format'} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $format = params()->{"format"} or return 11 4 15
40  
41 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) 10 1 11
42 or return halt(
43 Dancer::Error->new(
44 code => 404,
45 title => "unsupported format requested",
46 message => "unsupported format requested: " . $format
47 )->render
48 );
49  
50 10 set serializer => $serializer;
51  
52 # check if we were supposed to deserialize the request
53 10 Dancer::Serializer->process_request(
54 Dancer::SharedData->request
55 );
56  
57 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
58 4 };
59  
60 14 ▶ hook after => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 # put it back the way it was
62 14 set serializer => $default_serializer;
63 }
64 4 };
65  
66 9 my %triggers_map = (
67 get => \&get,
68 update => \&put,
69 create => \&post,
70 delete => \&del,
71 );
72  
73 4 ▶register resource => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
74 4 ▶ croak "resource invoked without arguments" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
75  
76 4 my ($resource, %triggers) = @_;
77  
78 4 ▶ while( my( $t, $sub ) = each %triggers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %triggers) 13 3 16
79 13 ▶ my $method = $triggers_map{$t}
Branch: 1 branch
BranchTrueFalseTotal
my $method = $triggers_map{$t} or croak("action '${t}' not recognized") 12 1 13
80 or croak "action '$t' not recognized";
81  
82 12 ▶ if ( $t eq 'create' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq 'create') { } 3 9 12
83 3 $method->( "/${resource}" => $triggers{$t} );
84 3 $method->( "/${resource}.:format" => $triggers{$t} );
85 }
86 else {
87 $method->( "/${resource}/:id$_" => $triggers{$t} )
88 9 for '.:format', '';
89 }
90 }
91  
92 9 };
93  
94 8 ▶register send_entity => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
95 # entity, status_code
96 8 ▶ status($_[1] || 200);
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 = Dancer::HTTP->codes->%*;
101  
102 9 for my $code (keys %http_codes) {
103 306 my $helper_name = lc($http_codes{$code});
104 306 $helper_name =~ s/[^\w]+/_/gms;
105 306 $helper_name = "status_${helper_name}";
106  
107 8 ▶ register $helper_name => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
108 8 ▶ my $entity = ($code >= 400 ) ? { error => $_[0]} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$code >= 400 ? : 3 5 8
109 8 send_entity($entity, $code);
110 306 };
111 }
112  
113 9 register_plugin;
114 9 1;
115  
116 __END__
↑ Summary