| 1 | package RPC::ExtDirect::Serializer; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 28 | ▶ | no warnings 'uninitialized'; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 28 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 28 | ▶ | use JSON (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 28 | ▶ | use RPC::ExtDirect::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 28 | ▶ | use RPC::ExtDirect::Util::Accessor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 28 | use RPC::ExtDirect::Util qw/ | |||||||||||||||||||||||||||
| 14 | clean_error_message get_caller_info parse_global_flags | ||||||||||||||||||||||||||||
| 15 | 28 | ▶ | /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | ### PUBLIC CLASS METHOD (CONSTRUCTOR) ### | ||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||
| 19 | # Instantiate a new Serializer | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub new { | ||||||||||||||||||||||||||||
| 23 | 94 | ▶ | my ($class, %arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 94 | my $self = bless { %arg }, $class; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 94 | return $self; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | ### PUBLIC INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||
| 32 | # Serialize the data passed to it in JSON | ||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub serialize { | ||||||||||||||||||||||||||||
| 36 | 57 | ▶ | my ($self, %arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 57 | ▶ | my $data = delete $arg{data} || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $arg{'data'} || []
MC/DC0%0/1
delete $arg{'data'} || []
delete $arg{'data'}
Truth table50%1/2
A
A= delete $arg{'data'}
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Try to serialize each response separately; | ||||||||||||||||||||||||||||
| 41 | # if one fails it's better to return an exception | ||||||||||||||||||||||||||||
| 42 | # for one response than fail all of them | ||||||||||||||||||||||||||||
| 43 | 57 | my @serialized = map { $self->_encode_response($_, %arg) } | |||||||||||||||||||||||||||
| 44 | @$data; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 57 | ▶ | my $text = @serialized == 1 ? shift @serialized | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | : '[' . join(',', @serialized) . ']' | ||||||||||||||||||||||||||||
| 48 | ; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 57 | return $text; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | ### PUBLIC INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 54 | # | ||||||||||||||||||||||||||||
| 55 | # Turns JSONified POST request(s) into array of instantiated | ||||||||||||||||||||||||||||
| 56 | # RPC::ExtDirect::Request (Exception) objects. Returns arrayref. | ||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub decode_post { | ||||||||||||||||||||||||||||
| 60 | 28 | ▶ | my ($self, %arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
decode_post: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 28 | my $post_text = delete $arg{data}; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Try to decode data, return Exception upon failure | ||||||||||||||||||||||||||||
| 65 | 28 | local $@; | |||||||||||||||||||||||||||
| 66 | 28 | my $data = eval { $self->_decode_json($post_text) }; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 28 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 3 | my $error = $self->_clean_msg($@); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | my $msg = "ExtDirect error decoding POST data: '$error'"; | |||||||||||||||||||||||||||
| 72 | 3 | my $xcpt = $self->_exception({ | |||||||||||||||||||||||||||
| 73 | direction => 'deserialize', | ||||||||||||||||||||||||||||
| 74 | message => $msg, | ||||||||||||||||||||||||||||
| 75 | %arg, | ||||||||||||||||||||||||||||
| 76 | }); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 3 | return [ $xcpt ]; | |||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 25 | ▶ | $data = [ $data ] unless ref $data eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 25 | my @requests = map { $self->_request({ %$_, %arg }) } @$data; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 25 | return \@requests; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | ### PUBLIC INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 89 | # | ||||||||||||||||||||||||||||
| 90 | # Instantiates Request based on form submitted to ExtDirect handler | ||||||||||||||||||||||||||||
| 91 | # Returns arrayref with single Request. | ||||||||||||||||||||||||||||
| 92 | # | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub decode_form { | ||||||||||||||||||||||||||||
| 95 | 10 | ▶ | my ($self, %arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
decode_form: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 10 | my $form_href = delete $arg{data}; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # Create the Request (or Exception) | ||||||||||||||||||||||||||||
| 100 | 10 | my $request = $self->_request({ %$form_href, %arg }); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 10 | return [ $request ]; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 28 | RPC::ExtDirect::Util::Accessor::mk_accessors( | |||||||||||||||||||||||||||
| 106 | simple => [qw/ config api /], | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | ############## PRIVATE METHODS BELOW ############## | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 112 | # | ||||||||||||||||||||||||||||
| 113 | # Clean error message | ||||||||||||||||||||||||||||
| 114 | # | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub _clean_msg { | ||||||||||||||||||||||||||||
| 117 | 6 | ▶ | my ($self, $msg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_clean_msg: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 6 | return clean_error_message($msg); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 123 | # | ||||||||||||||||||||||||||||
| 124 | # Try encoding one response into JSON | ||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub _encode_response { | ||||||||||||||||||||||||||||
| 128 | 59 | ▶ | my ($self, $response, %arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_encode_response: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 59 | my $mute_exceptions = $arg{mute_exceptions}; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 59 | local $@; | |||||||||||||||||||||||||||
| 133 | 59 | my $text = eval { $self->_encode_json($response, %arg) }; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 59 | ▶ | if ( $@ and not $mute_exceptions ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$@ and not $mute_exceptions
MC/DC100%2/2
$@ and not $mute_exceptions
$@
not $mute_exceptions
Truth table100%3/3
A and not(B)
A= $@B= not $mute_exceptions
| |||||||||||||||||||||||||||||
| 136 | 3 | my $msg = $self->_clean_msg($@); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # It's not a given that response/exception hashrefs | ||||||||||||||||||||||||||||
| 139 | # will be actual blessed objects, so we have to peek | ||||||||||||||||||||||||||||
| 140 | # into them instead of using accessors | ||||||||||||||||||||||||||||
| 141 | my $exception = $self->_exception({ | ||||||||||||||||||||||||||||
| 142 | direction => 'serialize', | ||||||||||||||||||||||||||||
| 143 | action => $response->{action}, | ||||||||||||||||||||||||||||
| 144 | method => $response->{method}, | ||||||||||||||||||||||||||||
| 145 | tid => $response->{tid}, | ||||||||||||||||||||||||||||
| 146 | 3 | where => __PACKAGE__, | |||||||||||||||||||||||||||
| 147 | message => $msg, | ||||||||||||||||||||||||||||
| 148 | %arg, | ||||||||||||||||||||||||||||
| 149 | }); | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 3 | local $@; | |||||||||||||||||||||||||||
| 152 | 3 | $text = eval { | |||||||||||||||||||||||||||
| 153 | 3 | $self->_encode_json( $exception->result(), %arg ) | |||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||
| 155 | }; | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 59 | return $text; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 161 | # | ||||||||||||||||||||||||||||
| 162 | # Actually encode JSON | ||||||||||||||||||||||||||||
| 163 | # | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | sub _encode_json { | ||||||||||||||||||||||||||||
| 166 | 62 | ▶ | my ($self, $data, %arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_encode_json: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 62 | ▶ | my $config = $arg{config} || $self->config; | ||||||||||||||||||||||||||
|
Condition66%2/3
$arg{'config'} || $self->config
MC/DC50%1/2
$arg{'config'} || $self->config
$arg{'config'}
$self->config
Truth table66%2/3
A || B
A= $arg{'config'}B= $self->config
| |||||||||||||||||||||||||||||
| 169 | my $options = defined $arg{json_options} ? $arg{json_options} | ||||||||||||||||||||||||||||
| 170 | 62 | ▶ | : $config->json_options_serialize | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | ; | ||||||||||||||||||||||||||||
| 172 | my $debug = defined $arg{debug} ? $arg{debug} | ||||||||||||||||||||||||||||
| 173 | 61 | ▶ | : $config->debug_serialize | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | ; | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # We force UTF-8 as per Ext.Direct spec | ||||||||||||||||||||||||||||
| 177 | 61 | $options->{utf8} = 1; | |||||||||||||||||||||||||||
| 178 | $options->{canonical} = $debug | ||||||||||||||||||||||||||||
| 179 | 61 | ▶ | unless defined $options->{canonical}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 61 | return JSON::to_json($data, $options); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 185 | # | ||||||||||||||||||||||||||||
| 186 | # Actually decode JSON | ||||||||||||||||||||||||||||
| 187 | # | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | sub _decode_json { | ||||||||||||||||||||||||||||
| 190 | 28 | ▶ | my ($self, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_decode_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 28 | my $options = $self->config->json_options_deserialize; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 28 | return JSON::from_json($text, $options); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 198 | # | ||||||||||||||||||||||||||||
| 199 | # Return a new Request object | ||||||||||||||||||||||||||||
| 200 | # | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | sub _request { | ||||||||||||||||||||||||||||
| 203 | 42 | ▶ | my ($self, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_request: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 42 | my $api = $self->api; | |||||||||||||||||||||||||||
| 206 | 42 | my $config = $self->config; | |||||||||||||||||||||||||||
| 207 | 42 | my $request_class = $config->request_class_deserialize; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 42 | eval "require $request_class"; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 42 | return $request_class->new({ | |||||||||||||||||||||||||||
| 212 | config => $config, | ||||||||||||||||||||||||||||
| 213 | api => $api, | ||||||||||||||||||||||||||||
| 214 | %$arg | ||||||||||||||||||||||||||||
| 215 | }); | ||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | ### PRIVATE INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 219 | # | ||||||||||||||||||||||||||||
| 220 | # Return a new Exception object | ||||||||||||||||||||||||||||
| 221 | # | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | sub _exception { | ||||||||||||||||||||||||||||
| 224 | 10 | ▶ | my ($self, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_exception: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 10 | my $direction = $arg->{direction}; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 10 | my $config = $self->config; | |||||||||||||||||||||||||||
| 229 | 10 | my $getter_class = "exception_class_$direction"; | |||||||||||||||||||||||||||
| 230 | 10 | my $getter_debug = "debug_$direction"; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 10 | my $exception_class = $config->$getter_class(); | |||||||||||||||||||||||||||
| 233 | 10 | my $debug = $config->$getter_debug(); | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 10 | eval "require $exception_class"; | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 10 | ▶ | $arg->{debug} = !!$debug unless defined $arg->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 10 | ▶ | $arg->{where} = get_caller_info(2) unless defined $arg->{where}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 10 | $arg->{verbose} = $config->verbose_exceptions(); | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 10 | return $exception_class->new($arg); | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 28 | 1; | |||||||||||||||||||||||||||