| 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.3520'; | |||||||||||||||||||||||||||
| 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 | sub template_or_serialize { | ||||||||||||||||||||||||||||
| 25 | 2 | ▶ | my( $template, $data ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
template_or_serialize: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 35 | 1 | return Dancer::template(@_); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | return $data; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub _request_content_types { | ||||||||||||||||||||||||||||
| 42 | 17 | ▶ | my $request = shift; | ||||||||||||||||||||||||||
|
Subroutine
_request_content_types: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 17 | my $params; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 17 | ▶ | if ($request) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 57 | push @content_types, $request->{content_type} | ||||||||||||||||||||||||||||
| 58 | 17 | ▶ | if $request->{content_type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | push @content_types, $params->{content_type} | ||||||||||||||||||||||||||||
| 61 | 17 | ▶ | if $params && $params->{content_type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$params and $params->{"content_type"}
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"}
| |||||||||||||||||||||||||||||
| 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 | sub _response_content_types { | ||||||||||||||||||||||||||||
| 71 | 11 | ▶ | my $request = shift; | ||||||||||||||||||||||||||
|
Subroutine
_response_content_types: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 72 | 11 | my @content_types; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | push @content_types, $request->{accept} | ||||||||||||||||||||||||||||
| 75 | 11 | ▶ | if $request->{accept}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | push @content_types, $request->{accept_type} | ||||||||||||||||||||||||||||
| 78 | 11 | ▶ | if $request->{'accept_type'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub serialize { | ||||||||||||||||||||||||||||
| 93 | 6 | ▶ | my ($self, $entity) = @_; | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | sub deserialize { | ||||||||||||||||||||||||||||
| 101 | 4 | ▶ | my ($self, $content) = @_; | ||||||||||||||||||||||||||
|
Subroutine
deserialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | sub content_type { | ||||||||||||||||||||||||||||
| 109 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
content_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 7 | $_content_type; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | sub support_content_type { | ||||||||||||||||||||||||||||
| 114 | 10 | ▶ | my ($self, $ct) = @_; | ||||||||||||||||||||||||||
|
Subroutine
support_content_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 10 | grep /^$ct$/, keys %$serializer; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub _load_serializer { | ||||||||||||||||||||||||||||
| 119 | 10 | ▶ | my ($self, $request, $content_types) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_load_serializer: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 126 | 10 | my $module = "Dancer::Serializer::" . $serializer->{$ct}; | |||||||||||||||||||||||||||
| 127 | 10 | ▶ | if (!exists $loaded_serializer->{$module}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 4 | ▶ | if (Dancer::ModuleLoader->load($module)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||