Twilio/TwiML/TwiML.pm

filter: Statementstmt 100% 83 / 83 Branchbran 100% 26 / 26 Conditioncond 90.9% 10 / 11 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 20 / 20 Podpod 100% 7 / 7 totaltotal 98.7% 152 / 154 CC 19 SCAR 11.8
CC
19
Cov
99%
_attribute_text
7.0
_child
5.0
new
4.0
CRAP 3.3
« Twilio/Page.pm ↑ Summary Twilio/TokenPaginationPage.pm »
1 package Twilio::TwiML::TwiML;
2  
3 5 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 use overload
7 13 ▶ q{""} => sub { $_[0]->as_xml },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 1 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
9 5 fallback => 1;
10  
11 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use Twilio::Json;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Twilio::Serialize;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 our $VERSION = '2.001';
17  
18 5 my $DECLARATION = qq{<?xml version="1.0" encoding="UTF-8"?>\n};
19  
20 5 my %TEXT_ESCAPE = ('&' => '&amp;', '<' => '&lt;', '>' => '&gt;', "\r" => '&#13;');
21 5 my %ATTRIBUTE_ESCAPE = (%TEXT_ESCAPE, q{"} => '&quot;', "\n" => '&#10;', "\t" => '&#9;');
22  
23 521 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
24 521 my ($class, $name, $value, $attributes) = @_;
25  
26 521 ▶ my $self = bless { name => $name, attributes => {}, children => [defined $value ? $value : ()] },
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 282 239 521
27 $class;
28  
29 521 ▶ my %given = %{ $attributes // {} };
Condition100%2/2
$attributes // {}
l!l
322199
MC/DC100%1/1
$attributes // {}
$attributes
Truth table100%2/2
A
A= $attributes
Aresult
11
00
30 521 $self->set_attribute($_, $given{$_}) for sort keys %given;
31 521 return $self;
32 }
33  
34 5 ▶sub append {
Subroutine append: called CC=1 SCAR=0.0
Pod documented
35 5 my ($self, $child) = @_;
36 5 push @{ $self->{children} }, $child;
37 5 return $self;
38 }
39  
40 330 ▶sub nest {
Subroutine nest: called CC=1 SCAR=0.0
Pod documented
41 330 my ($self, $child) = @_;
42 330 push @{ $self->{children} }, $child;
43 330 return $child;
44 }
45  
46 907 ▶sub set_attribute {
Subroutine set_attribute: called CC=3 SCAR=11.0
Pod documented
47 907 my ($self, $name, $value) = @_;
48  
49 # A name no element knows is written as it is, so new TwiML works before this library has it.
50 907 my $known = $self->attribute_table->{$name};
51 907 ▶ if ($known) {
Branch: 1 branch
BranchTrueFalseTotal
if ($known) 642 265 907
52 642 $name = $known->{name};
53 642 ▶ $value = Twilio::Serialize::boolean_to_string($value) if $known->{boolean};
Branch: 1 branch
BranchTrueFalseTotal
if $known->{"boolean"} 80 562 642
54 }
55 907 $self->{attributes}{$name} = $value;
56 907 return $self;
57 }
58  
59 30 ▶sub attribute_table { return {} }
Subroutine attribute_table: called CC=1 SCAR=0.0
Pod documented
60  
61 25 ▶sub add_child {
Subroutine add_child: called CC=1 SCAR=0.0
Pod documented
62 25 my ($self, $name, $value, $attributes) = @_;
63 25 require Twilio::TwiML::GenericNode;
64 25 return $self->nest(Twilio::TwiML::GenericNode->new($name, $value, $attributes));
65 }
66  
67 200 ▶sub as_xml {
Subroutine as_xml: called CC=1 SCAR=0.0
Pod documented
68 200 my ($self) = @_;
69 200 return $DECLARATION . $self->_element . "\n";
70 }
71  
72 522 ▶sub _element {
Subroutine _element: called CC=2 SCAR=6.9
73 522 my ($self) = @_;
74  
75 522 my $attributes = join q{}, map { $self->_attribute($_) } sort keys %{ $self->{attributes} };
76 522 my $start = "<$self->{name}$attributes";
77 522 ▶ return "$start/>" if !@{ $self->{children} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"children"};} 162 360 522
78  
79 360 my $content = join q{}, map { _child($_) } @{ $self->{children} };
80 360 return "$start>$content</$self->{name}>";
81 }
82  
83 # Any other object is text by its own conversion, such as a URI given to <Play>. A hash or an
84 # array is written as JSON, the content <Task> takes, as twilio-python does.
85 616 ▶sub _child {
Subroutine _child: called CC=5 SCAR=16.1
86 616 my ($child) = @_;
87 616 ▶ return $child->_element if blessed($child) && $child->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($child) and $child->isa(__PACKAGE__) 322 294 616
Condition100%3/3
builtin::blessed($child) and $child->isa(__PACKAGE__)
!ll&&!rl&&r
2922322
MC/DC100%2/2
builtin::blessed($child) and $child->isa(__PACKAGE__)
builtin::blessed($child) $child->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($child)B= $child->isa(__PACKAGE__)
ABresult
0X0
100
111
88  
89 294 ▶ my $text = ref $child eq 'HASH' || ref $child eq 'ARRAY' ? Twilio::Json::encode($child) : $child;
Branch: 1 branch
BranchTrueFalseTotal
ref $child eq 'HASH' || ref $child eq 'ARRAY' ? : 2 292 294
Condition100%3/3
ref $child eq "HASH" or ref $child eq "ARRAY"
l!l&&r!l&&!r
11292
MC/DC100%2/2
ref $child eq "HASH" or ref $child eq "ARRAY"
ref $child eq "HASH" ref $child eq "ARRAY"
Truth table100%3/3
A or B
A= ref $child eq "HASH"B= ref $child eq "ARRAY"
ABresult
1X1
011
000
90 294 return _escaped($text, \%TEXT_ESCAPE, qr/[&<>\r]/);
91 }
92  
93 908 ▶sub _attribute {
Subroutine _attribute: called CC=2 SCAR=6.9
94 908 my ($self, $name) = @_;
95  
96 908 my $value = $self->{attributes}{$name};
97 908 ▶ return q{} if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 906 908
98  
99 906 my $text = _escaped(_attribute_text($value), \%ATTRIBUTE_ESCAPE, qr/[&<>\r\n\t"]/);
100 906 return qq{ $name="$text"};
101 }
102  
103 # A JSON boolean and a reference to 1 or 0 say true or false. Elements that know an attribute is
104 # a boolean turn a plain Perl value into one of these first.
105 906 ▶sub _attribute_text {
Subroutine _attribute_text: called CC=7 SCAR=19.5
106 906 my ($value) = @_;
107  
108 906 ▶ return join q{ }, @{$value} if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 38 868 906
109 868 ▶ return ${$value} ? 'true' : 'false' if ref $value eq 'SCALAR';
Branch: 2 branches
BranchTrueFalseTotal
if ref $value eq "SCALAR" 2 866 868
${$value;} ? : 1 1 2
110 866 ▶ return $value ? 'true' : 'false' if blessed($value) && $value->isa('JSON::PP::Boolean');
Branch: 2 branches
BranchTrueFalseTotal
if builtin::blessed($value) and $value->isa("JSON::PP::Boolean") 3 863 866
$value ? : 2 1 3
Condition66%2/3
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
!ll&&!rl&&r
86303
MC/DC50%1/2
builtin::blessed($value) and $value->isa("JSON::PP::Boolean")
builtin::blessed($value) $value->isa("JSON::PP::Boolean")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("JSON::PP::Boolean")
ABresult
0X0
100
111
111 863 return "$value";
112 }
113  
114 # Text may arrive as characters or as UTF-8 bytes, as in the rest of the library, and the
115 # document says it is UTF-8.
116 1200 ▶sub _escaped {
Subroutine _escaped: called CC=2 SCAR=6.9
117 1200 my ($text, $escapes, $pattern) = @_;
118  
119 1200 my $encoded = "$text";
120 1200 ▶ utf8::encode($encoded) if utf8::is_utf8($encoded);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($encoded) 4 1196 1200
121 1200 $encoded =~ s/($pattern)/$escapes->{$1}/g;
122 1200 return $encoded;
123 }
124  
125 5 1;
126  
127 __END__
« Twilio/Page.pm ↑ Summary Twilio/TokenPaginationPage.pm »