Twilio/Serialize.pm

filter: Statementstmt 100% 86 / 86 Branchbran 95.2% 40 / 42 Conditioncond 81.8% 18 / 22 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 100% 18 / 18 Podpod 100% 8 / 8 totaltotal 94.7% 179 / 189 CC 33 SCAR 14.2
CC
33
Cov
96%
json_body
7.0
_json_boolean
5.7
_json_value
5.0
CRAP 4.2
« Twilio/Jwt/TaskRouter/Policy.pm ↑ Summary Twilio/Jwt/Grants/VoiceGrant.pm »
1 package Twilio::Serialize;
2  
3 15 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 ▶use Twilio::Json;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 our $VERSION = '2.001';
13  
14 592 ▶sub boolean_to_string {
Subroutine boolean_to_string: called CC=5 SCAR=16.1
Pod documented
15 592 my ($value) = @_;
16 592 ▶ return $value if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 115 477 592
17  
18 # The text "false" is true to Perl, and it is what a caller copying from a Twilio example
19 # is likely to write.
20 477 ▶ return $value if $value eq 'true' || $value eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "true" or $value eq "false" 7 470 477
Condition100%3/3
$value eq "true" or $value eq "false"
l!l&&r!l&&!r
43470
MC/DC100%2/2
$value eq "true" or $value eq "false"
$value eq "true" $value eq "false"
Truth table100%3/3
A or B
A= $value eq "true"B= $value eq "false"
ABresult
1X1
011
000
21 470 ▶ return $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 465 5 470
22 }
23  
24 256 ▶sub iso8601_date_time {
Subroutine iso8601_date_time: called CC=1 SCAR=0.0
Pod documented
25 256 my ($value) = @_;
26  
27 256 return _utc_format($value, '%Y-%m-%dT%H:%M:%SZ');
28 }
29  
30 63 ▶sub iso8601_date {
Subroutine iso8601_date: called CC=1 SCAR=0.0
Pod documented
31 63 my ($value) = @_;
32 63 return _utc_format($value, '%Y-%m-%d');
33 }
34  
35 # Time::Piece and DateTime both have epoch, so neither has to be a dependency here.
36 319 ▶sub _utc_format {
Subroutine _utc_format: called CC=3 SCAR=11.0
37 319 my ($value, $format) = @_;
38 319 ▶ if (blessed($value) && $value->can('epoch')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->can("epoch")) 219 100 319
Condition100%3/3
builtin::blessed($value) and $value->can("epoch")
!ll&&!rl&&r
982219
MC/DC100%2/2
builtin::blessed($value) and $value->can("epoch")
builtin::blessed($value) $value->can("epoch")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->can("epoch")
ABresult
0X0
100
111
39 219 return POSIX::strftime($format, gmtime $value->epoch);
40 }
41 100 return $value;
42 }
43  
44 88 ▶sub json_object {
Subroutine json_object: called CC=3 SCAR=11.0
Pod documented
45 88 my ($value) = @_;
46  
47 # An object could encode to anything, so only plain data is turned into JSON here.
48 88 my $type = ref $value;
49 88 ▶ return $type eq 'HASH' || $type eq 'ARRAY' ? Twilio::Json::encode($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'HASH' || $type eq 'ARRAY' ? : 81 7 88
Condition100%3/3
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
8017
MC/DC100%2/2
$type eq "HASH" or $type eq "ARRAY"
$type eq "HASH" $type eq "ARRAY"
Truth table100%3/3
A or B
A= $type eq "HASH"B= $type eq "ARRAY"
ABresult
1X1
011
000
50 }
51  
52 15 my %JSON_RULE = (
53 boolean => \&_json_boolean,
54 iso8601_date => \&iso8601_date,
55 iso8601_date_time => \&iso8601_date_time,
56 );
57  
58 122 ▶sub json_body {
Subroutine json_body: called CC=7 SCAR=19.5
Pod documented
59 122 my ($body, $rules) = @_;
60 122 ▶ return {} if !defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 1 121 122
61 121 ▶ croak 'A JSON body must be a hash reference' if ref $body ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $body ne "HASH" 1 120 121
62  
63 120 my %sent;
64 120 for my $name (keys %{$body}) {
65 391 ▶ my $rule = $JSON_RULE{ ($rules // {})->{$name} // q{} };
Condition75%3/4
+($rules // {})->{$name} // ""
l!l
26365
$rules // {}
l!l
3910
MC/DC0%0/1
+($rules // {})->{$name} // ""
$rules
Truth table50%1/2
A
A= $rules
Aresult
11
00
66 391 ▶ $sent{$name} = $rule ? $rule->($body->{$name}) : _json_value($body->{$name});
Branch: 1 branch
BranchTrueFalseTotal
$rule ? : 26 365 391
67 }
68 120 return \%sent;
69 }
70  
71 # A copy, so that the caller's data stays as it was, with every date object written as text.
72 479 ▶sub _json_value {
Subroutine _json_value: called CC=5 SCAR=16.1
73 479 my ($value) = @_;
74  
75 479 my $type = ref $value;
76 479 ▶ return { map { $_ => _json_value($value->{$_}) } keys %{$value} } if $type eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" 67 412 479
77 412 ▶ return [map { _json_value($_) } @{$value}] if $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ARRAY" 44 368 412
78 368 ▶ return iso8601_date_time($value) if blessed($value) && $value->can('epoch');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->can("epoch") 2 366 368
Condition66%2/3
builtin::blessed($value) and $value->can("epoch")
!ll&&!rl&&r
36602
MC/DC50%1/2
builtin::blessed($value) and $value->can("epoch")
builtin::blessed($value) $value->can("epoch")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->can("epoch")
ABresult
0X0
100
111
79 366 return $value;
80 }
81  
82 # JSON has booleans of its own, and Twilio does not take 1 or "true" for them.
83 14 ▶sub _json_boolean {
Subroutine _json_boolean: called CC=5 SCAR=17.5
84 14 my ($value) = @_;
85 14 ▶ return $value if !defined $value || ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value 0 14 14
Condition33%1/3
not defined $value or ref $value
l!l&&r!l&&!r
0014
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table33%1/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
86 14 ▶ return \0 if $value eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "false" 1 13 14
87 13 ▶ return $value ? \1 : \0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 13 0 13
88 }
89  
90 13 ▶sub map_values {
Subroutine map_values: called CC=2 SCAR=6.9
Pod documented
91 13 my ($values, $writer) = @_;
92 13 ▶ return $values if ref $values ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $values ne "ARRAY" 2 11 13
93 11 return [map { $writer->($_) } @{$values}];
94 }
95  
96 6 ▶sub prefixed_collapsible_map {
Subroutine prefixed_collapsible_map: called CC=3 SCAR=11.0
Pod documented
97 6 my ($map, $prefix) = @_;
98 6 ▶ return {} if !defined $map;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $map 1 5 6
99 5 ▶ croak "A value sent as $prefix.* must be a hash reference" if ref $map ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $map ne "HASH" 1 4 5
100  
101 4 my %flat;
102 4 _flatten(\%flat, $prefix, $map);
103 4 return \%flat;
104 }
105  
106 # Arrays are walked by index, as twilio-php walks PHP's arrays, so no reference is ever sent as
107 # its address.
108 13 ▶sub _flatten {
Subroutine _flatten: called CC=5 SCAR=16.1
109 13 my ($flat, $name, $value) = @_;
110  
111 13 my $type = ref $value;
112 13 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 6 7 13
elsif ($type eq 'ARRAY') { } 1 6 7
113 6 _flatten($flat, "$name.$_", $value->{$_}) for sort keys %{$value};
114 }
115 elsif ($type eq 'ARRAY') {
116 1 _flatten($flat, "$name.$_", $value->[$_]) for 0 .. $#{$value};
117 }
118 else {
119 6 $flat->{$name} = $value;
120 }
121 13 return;
122 }
123  
124 5237 ▶sub path_segment {
Subroutine path_segment: called CC=4 SCAR=13.9
Pod documented
125 5237 my ($value) = @_;
126  
127 # An empty part would point the request at another resource, such as /Accounts//Messages.json
128 # for a client with OAuth and no account SID.
129 5237 ▶ if (!defined $value || !length $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and length $value) 3 5234 5237
Condition100%3/3
defined $value and length $value
!ll&&!rl&&r
215234
MC/DC100%2/2
defined $value and length $value
defined $value length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
130 3 croak 'A URL needs an id that is missing. For account resources with a credential provider, '
131 . q{set the client's account_sid};
132 }
133 5234 my $encoded = "$value";
134 5234 ▶ utf8::encode($encoded) if utf8::is_utf8($encoded);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($encoded) 1 5233 5234
135 5234 $encoded =~ s/([^A-Za-z0-9._~-])/sprintf('%%%02X', ord $1)/ge;
136 5234 return $encoded;
137 }
138  
139 15 1;
140  
141 __END__
« Twilio/Jwt/TaskRouter/Policy.pm ↑ Summary Twilio/Jwt/Grants/VoiceGrant.pm »