File Coverage

blib/lib/JMAP/Tester/Response/Paragraph.pm
Criterion Covered Total %
statement 83 83 100.0
branch 20 20 100.0
condition 3 3 100.0
subroutine 16 16 100.0
pod 8 11 72.7
total 130 133 97.7


line stmt bran cond sub pod time code
1 6     6   416090 use v5.20.0;
  6         25  
2             package JMAP::Tester::Response::Paragraph 0.109;
3             # ABSTRACT: a group of sentences in a JMAP response
4              
5 6     6   742 use Moo;
  6         12276  
  6         41  
6 6     6   4898 use experimental 'signatures';
  6         2286  
  6         75  
7              
8 6     6   2122 use JMAP::Tester::Abort 'abort';
  6         22  
  6         60  
9              
10 6     6   862 use namespace::clean;
  6         10  
  6         61  
11              
12             #pod =head1 OVERVIEW
13             #pod
14             #pod These objects represent paragraphs in the JMAP response. That is, if your
15             #pod response is:
16             #pod
17             #pod [
18             #pod [ "messages", { ... }, "a" ], # 1
19             #pod [ "smellUpdates", { ... }, "b" ], # 2
20             #pod [ "smells", { ... }, "b" ], # 3
21             #pod ]
22             #pod
23             #pod ...then #1 forms one paragraph and #2 and #3 together form another. It goes by
24             #pod matching client ids.
25             #pod
26             #pod =cut
27              
28 1     1 0 24 sub client_id ($self) {
  1         3  
  1         2  
29 1         10 $self->_sentences->[0]->client_id;
30             }
31              
32             sub BUILD {
33             abort("tried to build 0-sentence paragraph")
34 23 100   23 0 10819 unless @{ $_[0]->_sentences };
  23         134  
35              
36 22         116 my $client_id = $_[0]->_sentences->[0]->client_id;
37             abort("tried to build paragraph with non-uniform client_ids")
38 22 100       44 if grep {; $_->client_id ne $client_id } @{ $_[0]->_sentences };
  37         473  
  22         80  
39             }
40              
41             has sentences => (is => 'bare', reader => '_sentences', required => 1);
42              
43             #pod =method sentences
44             #pod
45             #pod The C method returns a list of
46             #pod L objects, one for each sentence
47             #pod in the paragraph.
48             #pod
49             #pod =cut
50              
51 16     16 1 4337 sub sentences ($self) { @{ $self->_sentences } }
  16         29  
  16         24  
  16         28  
  16         61  
52              
53             #pod =method sentence
54             #pod
55             #pod my $sentence = $para->sentence($n);
56             #pod
57             #pod This method returns the Ith sentence of the paragraph.
58             #pod
59             #pod =cut
60              
61 6     6 1 139 sub sentence ($self, $n) {
  6         15  
  6         12  
  6         27  
62 6 100       98 abort("there is no sentence for index $n")
63             unless $self->_sentences->[$n];
64             }
65              
66             #pod =method single
67             #pod
68             #pod my $sentence = $para->single;
69             #pod my $sentence = $para->single($name);
70             #pod
71             #pod This method throws an exception if there is more than one sentence in the
72             #pod paragraph. If a C<$name> argument is given and the paragraph's single
73             #pod sentence doesn't have that name, an exception is raised.
74             #pod
75             #pod Otherwise, this method returns the sentence.
76             #pod
77             #pod =cut
78              
79 6     6 1 137 sub single ($self, $name = undef) {
  6         13  
  6         12  
  6         10  
80 6         16 my @sentences = $self->sentences;
81              
82 6 100       21 abort("more than one sentence in paragraph, but ->single called")
83             if @sentences > 1;
84              
85 5 100 100     38 abort("single sentence not of expected name <$name>")
86             if defined $name && $name ne $sentences[0]->name;
87              
88 4         16 return $sentences[0];
89             }
90              
91             #pod =method assert_n_sentences
92             #pod
93             #pod my ($s1, $s2, ...) = $paragraph->assert_n_sentences($n);
94             #pod
95             #pod This method returns all the sentences in the paragarph, as long as there are
96             #pod exactly C<$n>. Otherwise, it aborts.
97             #pod
98             #pod =cut
99              
100 3     3 1 127 sub assert_n_sentences ($self, $n) {
  3         7  
  3         6  
  3         5  
101 3 100       456 Carp::confess("no sentence count given") unless defined $n;
102              
103 2         6 my @sentences = $self->sentences;
104              
105 2 100       8 unless (@sentences == $n) {
106 1         11 abort("expected $n sentences but got " . @sentences)
107             }
108              
109 1         4 return @sentences;
110             }
111              
112             #pod =method sentence_named
113             #pod
114             #pod my $sentence = $paragraph->sentence_named($name);
115             #pod
116             #pod This method returns the sentence with the given name. If no such sentence
117             #pod exists, or if two sentences with the name exist, the tester will abort.
118             #pod
119             #pod =cut
120              
121 4     4 1 4505 sub sentence_named ($self, $name) {
  4         11  
  4         9  
  4         10  
122 4 100       533 Carp::confess("no sentence name given") unless defined $name;
123              
124 3         14 my @sentences = grep {; $_->name eq $name } $self->sentences;
  5         40  
125              
126 3 100       28 unless (@sentences) {
127 1         8 abort(qq{no sentence found with name "$name"});
128             }
129              
130 2 100       8 if (@sentences > 1) {
131 1         7 abort(qq{found more than one sentence with name "$name"});
132             }
133              
134 1         9 return $sentences[0];
135             }
136              
137             #pod =method as_triples
138             #pod
139             #pod =method as_stripped_triples
140             #pod
141             #pod C returns an arrayref containing the result of calling
142             #pod C on each sentence in the paragraph. C removes
143             #pod JSON types.
144             #pod
145             #pod =cut
146              
147 1     1 1 30 sub as_triples ($self) {
  1         3  
  1         3  
148 1         5 [ map {; $_->as_triple } $self->sentences ]
  2         8  
149             }
150              
151 1     1 1 25 sub as_stripped_triples ($self) {
  1         4  
  1         3  
152 1         5 [ map {; $_->as_stripped_triple } $self->sentences ]
  2         98  
153             }
154              
155             #pod =method as_pairs
156             #pod
157             #pod C returns an arrayref containing the result of calling C
158             #pod on each sentence in the paragraph. C removes JSON types.
159             #pod
160             #pod =cut
161              
162 1     1 1 23 sub as_pairs ($self) {
  1         4  
  1         2  
163 1         4 [ map {; $_->as_pair } $self->sentences ]
  2         8  
164             }
165              
166 1     1 0 24 sub as_stripped_pairs ($self) {
  1         3  
  1         2  
167 1         5 [ map {; $_->as_stripped_pair } $self->sentences ]
  2         65  
168             }
169              
170             1;
171              
172             __END__