File Coverage

blib/lib/SignalWire/Agents/SWML/Document.pm
Criterion Covered Total %
statement 36 39 92.3
branch n/a
condition 3 6 50.0
subroutine 12 13 92.3
pod 0 9 0.0
total 51 67 76.1


line stmt bran cond sub pod time code
1             package SignalWire::Agents::SWML::Document;
2 2     2   94865 use strict;
  2         5  
  2         90  
3 2     2   9 use warnings;
  2         3  
  2         111  
4 2     2   618 use Moo;
  2         6373  
  2         10  
5 2     2   1657 use JSON ();
  2         2  
  2         960  
6              
7             has 'version' => (
8             is => 'ro',
9             default => sub { '1.0.0' },
10             );
11              
12             has 'sections' => (
13             is => 'rw',
14             default => sub { {} },
15             );
16              
17             sub add_section {
18 7     7 0 3211 my ($self, $name) = @_;
19 7   50     71 $self->sections->{$name} //= [];
20 7         19 return $self;
21             }
22              
23             sub add_verb {
24 9     9 0 45 my ($self, $section_name, $verb_name, $verb_data) = @_;
25 9   50     72 $self->sections->{$section_name} //= [];
26 9         22 push @{ $self->sections->{$section_name} }, { $verb_name => $verb_data };
  9         45  
27 9         24 return $self;
28             }
29              
30             sub add_raw_verb {
31 2     2 0 12 my ($self, $section_name, $verb_hash) = @_;
32 2   50     16 $self->sections->{$section_name} //= [];
33 2         5 push @{ $self->sections->{$section_name} }, $verb_hash;
  2         8  
34 2         5 return $self;
35             }
36              
37             sub get_section {
38 7     7 0 40 my ($self, $name) = @_;
39 7         45 return $self->sections->{$name};
40             }
41              
42             sub has_section {
43 4     4 0 21 my ($self, $name) = @_;
44 4         58 return exists $self->sections->{$name};
45             }
46              
47             sub clear_section {
48 2     2 0 1584 my ($self, $name) = @_;
49 2         13 $self->sections->{$name} = [];
50 2         32 return $self;
51             }
52              
53             sub to_hash {
54 5     5 0 3240 my ($self) = @_;
55             return {
56 5         127 version => $self->version,
57             sections => $self->sections,
58             };
59             }
60              
61             sub to_json {
62 2     2 0 3213 my ($self) = @_;
63 2         9 return JSON::encode_json($self->to_hash);
64             }
65              
66             sub to_pretty_json {
67 0     0 0   my ($self) = @_;
68 0           my $json = JSON->new->utf8->canonical->pretty;
69 0           return $json->encode($self->to_hash);
70             }
71              
72             1;