blib/lib/Dancer/Serializer/Mutable.pm

filter: Statementstmt 100% 74 / 74 Branchbran 80.0% 16 / 20 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 12 / 12 Podpod 80.0% 4 / 5 totaltotal 93.9% 109 / 116 CC 14 SCAR 12.8
CC
14
Cov
95%
_request_content_types
6.1
_load_serializer
5.0
_response_content_types
4.0
CRAP 3.6
« lib/Dancer/Session.pm ↑ Summary lib/Dancer/Hook.pm »
1 package Dancer::Serializer::Mutable;
2 5 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: Serialize and deserialize content using the appropriate HTTP header
4 5 $Dancer::Serializer::Mutable::VERSION = '1.3522';
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use base 'Dancer::Serializer::Abstract', 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Dancer::SharedData;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our @EXPORT_OK = qw/ template_or_serialize /;
12  
13 5 my $serializer = {
14 'text/x-yaml' => 'YAML',
15 'text/html' => 'YAML',
16 'text/xml' => 'XML',
17 'text/x-json' => 'JSON',
18 'application/json' => 'JSON',
19 };
20  
21 5 my $loaded_serializer = {};
22 5 my $_content_type;
23  
24 2 ▶sub template_or_serialize {
Subroutine template_or_serialize: called CC=2 SCAR=6.9
Pod documented
25 2 my( $template, $data ) = @_;
26  
27 2 my( $content_type ) = @{ _response_content_types(Dancer::SharedData->request) };
28  
29 # TODO the accept value coming from the browser can
30 # be quite complex (e.g.,
31 # text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
32 # ), but that simple heuristic should be good enough
33 # for most cases
34 2 ▶ if ( $content_type =~ qr#text/html# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type =~ qr"text/html") 1 1 2
35 1 return Dancer::template(@_);
36 }
37  
38 1 return $data;
39 }
40  
41 17 ▶sub _request_content_types {
Subroutine _request_content_types: called CC=6 SCAR=18.0
42 17 my $request = shift;
43  
44 17 my $params;
45  
46 17 ▶ if ($request) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request) 17 0 17
47 17 $params = $request->params;
48 }
49  
50 # we push in @content_types by order of desirability
51 # I.e.: we want $content_types[0] more than $content_types[1]
52 17 my @content_types;
53  
54 17 my $method = $request->method;
55  
56 17 ▶ if ($method =~ /^(?:POST|PUT|GET|DELETE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(?:POST|PUT|GET|DELETE)$/) 17 0 17
57 push @content_types, $request->{content_type}
58 17 ▶ if $request->{content_type};
Branch: 1 branch
BranchTrueFalseTotal
if $request->{"content_type"} 8 9 17
59  
60 push @content_types, $params->{content_type}
61 17 ▶ if $params && $params->{content_type};
Branch: 1 branch
BranchTrueFalseTotal
if $params and $params->{"content_type"} 1 16 17
Condition66%2/3
$params and $params->{"content_type"}
!ll&&!rl&&r
0161
MC/DC50%1/2
$params and $params->{"content_type"}
$params $params->{"content_type"}
Truth table66%2/3
A and B
A= $paramsB= $params->{"content_type"}
ABresult
0X0
100
111
62 }
63 17 push @content_types, 'application/json';
64  
65 # remove duplicates
66 17 my %seen;
67 17 return [ grep { not $seen{$_}++ } @content_types ];
68 }
69  
70 11 ▶sub _response_content_types {
Subroutine _response_content_types: called CC=4 SCAR=13.9
71 11 my $request = shift;
72 11 my @content_types;
73  
74 push @content_types, $request->{accept}
75 11 ▶ if $request->{accept};
Branch: 1 branch
BranchTrueFalseTotal
if $request->{"accept"} 4 7 11
76  
77 push @content_types, $request->{accept_type}
78 11 ▶ if $request->{'accept_type'};
Branch: 1 branch
BranchTrueFalseTotal
if $request->{"accept_type"} 1 10 11
79  
80 # Both above could be '*/*' which means it is our choice.
81  
82 # Default to the same format as in the request:
83 11 for (@{_request_content_types($request)}) {
84 15 push @content_types, $_;
85 }
86  
87 # remove duplicates
88 11 my %seen;
89 11 return [ grep { not $seen{$_}++ } @content_types ];
90 }
91  
92 6 ▶sub serialize {
Subroutine serialize: called CC=1 SCAR=0.0
Pod documented
93 6 my ($self, $entity) = @_;
94 6 my $request = Dancer::SharedData->request;
95 6 my $content_types = _response_content_types($request);
96 6 my $serializer = $self->_load_serializer($request, $content_types);
97 6 return $serializer->serialize($entity);
98 }
99  
100 4 ▶sub deserialize {
Subroutine deserialize: called CC=1 SCAR=0.0
Pod documented
101 4 my ($self, $content) = @_;
102 4 my $request = Dancer::SharedData->request;
103 4 my $content_types = _request_content_types($request);
104 4 my $serializer = $self->_load_serializer($request, $content_types);
105 4 return $serializer->deserialize($content);
106 }
107  
108 7 ▶sub content_type {
Subroutine content_type: called CC=1 SCAR=0.0
Pod documented
109 7 my $self = shift;
110 7 $_content_type;
111 }
112  
113 10 ▶sub support_content_type {
Subroutine support_content_type: called CC=1 SCAR=0.0
Pod undocumented
114 10 my ($self, $ct) = @_;
115 10 grep /^$ct$/, keys %$serializer;
116 }
117  
118 10 ▶sub _load_serializer {
Subroutine _load_serializer: called CC=5 SCAR=16.2
119 10 my ($self, $request, $content_types) = @_;
120  
121 10 foreach my $ct (@$content_types) {
122 # 'content_type' => 'text/xml; charset=utf-8'
123 10 my $oct = $ct;
124 10 $ct = (split ';', $ct)[0];
125 10 ▶ if (exists $serializer->{$ct}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $serializer->{$ct}) 10 0 10
126 10 my $module = "Dancer::Serializer::" . $serializer->{$ct};
127 10 ▶ if (!exists $loaded_serializer->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $loaded_serializer->{$module}) 4 6 10
128 4 ▶ if (Dancer::ModuleLoader->load($module)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Dancer::ModuleLoader"->load($module)) 4 0 4
129 4 my $serializer_object = $module->new;
130 4 $loaded_serializer->{$module} = $serializer_object;
131 }
132 }
133 10 $_content_type = $oct;
134 10 return $loaded_serializer->{$module};
135 }
136 }
137 }
138  
139 5 1;
140  
141 __END__
« lib/Dancer/Session.pm ↑ Summary lib/Dancer/Hook.pm »