| 1 | package Dancer::Serializer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 180 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | #ABSTRACT: serializer wrapper for Dancer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 180 | $Dancer::Serializer::VERSION = '1.3520'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # Factory for serializer engines | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 180 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 180 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 180 | ▶ | use Dancer::ModuleLoader; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 180 | ▶ | use Dancer::Engine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 180 | ▶ | use Dancer::Factory::Hook; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 180 | ▶ | use Dancer::Error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 180 | ▶ | use Dancer::SharedData; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 180 | Dancer::Factory::Hook->instance->install_hooks(qw/before_deserializer after_deserializer/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 180 | my $_engine; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | sub engine { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 150 | ▶ | $_engine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
engine: called CC=3 SCAR=11.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | and return $_engine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # don't create a new serializer unless it's defined in the config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # (else it's created using json, and that's *not* what we want) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 7 | my $serializer_name = Dancer::App->current->setting('serializer'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | ▶ | $serializer_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | and return Dancer::Serializer->init($serializer_name); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | sub init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 23 | ▶ | my ($class, $name, $config) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 23 | ▶ | $name ||= 'JSON'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$name ||= "JSON"
MC/DC100%1/1
$name ||= "JSON"
$name
Truth table100%2/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 23 | $_engine = Dancer::Engine->build('serializer' => $name, $config); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 22 | return $_engine; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # takes a response object and checks whether or not it should be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # serialized. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # returns an error object if the serializer fails | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | sub process_response { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 45 | ▶ | my ($class, $response) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_response: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 45 | my $content = $response->{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 45 | ▶ | if (ref($content) && (ref($content) ne 'GLOB')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $content and ref $content ne "GLOB"
MC/DC50%1/2
ref $content and ref $content ne "GLOB"
ref $content
ref $content ne "GLOB"
Truth table66%2/3
A and B
A= ref $contentB= ref $content ne "GLOB"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 30 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 30 | eval { $content = engine->serialize($content) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # the serializer failed, replace the response with an error object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 30 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | my $error = Dancer::Error->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | code => 500, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | message => "Serializer (" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | . ref($_engine) . ") " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | . "failed at serializing " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 7 | . $response->{content} . ":\n$@", | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 7 | $response = $error->render; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # the serializer succeeded, alter the response object accordingly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 23 | $response->header('Content-Type' => engine->content_type); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 23 | $response->{content} = $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 45 | return $response; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # deserialize input params in the request body, if matching the Serializer's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # content-type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | sub process_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 34 | ▶ | my ($class, $request) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_request: called CC=11 SCAR=24.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 34 | Dancer::Factory::Hook->execute_hooks('before_deserializer'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 34 | ▶ | return $request unless engine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # Content-Type may contain additional parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # (http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # which should be safe to ignore at this level. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # So accept either e.g. text/xml or text/xml; charset=utf-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 34 | my $content_type = $request->content_type; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 34 | $content_type =~ s/ \s* ; .+ $ //x; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 34 | ▶ | return $request unless engine->support_content_type($content_type); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 12 | ▶ | return $request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
$request->is_put or $request->is_post or $request->is_patch or $request->is_delete
$request->is_put or $request->is_post or $request->is_patch
$request->is_put or $request->is_post
MC/DC50%2/4
$request->is_put or $request->is_post or $request->is_patch or $request->is_delete
$request->is_put
$request->is_post
$request->is_patch
$request->is_delete
Truth table60%3/5
A or B or C or D
A= $request->is_putB= $request->is_postC= $request->is_patchD= $request->is_delete
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | unless $request->is_put | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | or $request->is_post | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | or $request->is_patch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | or $request->is_delete; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 11 | my $old_params = $request->params('body'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # try to deserialize | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 11 | my $new_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 11 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 11 | $new_params = engine->deserialize($request->body) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 11 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | Dancer::Logger::core "Unable to deserialize request body with " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | . engine() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | . " : \n$@"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | return $request; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 10 | ▶ | if(!ref $new_params or ref $new_params ne 'HASH'){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $new_params or ref $new_params ne "HASH"
MC/DC50%1/2
not ref $new_params or ref $new_params ne "HASH"
not ref $new_params
ref $new_params ne "HASH"
Truth table66%2/3
not(A) or B
A= not ref $new_paramsB= ref $new_params ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | return $request; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 8 | ▶ | (keys %$old_params) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | ? $request->_set_body_params({%$old_params, %$new_params}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | : $request->_set_body_params($new_params); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 8 | Dancer::Factory::Hook->execute_hooks('after_deserializer'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 8 | return $request; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 180 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||