File Coverage

blib/lib/JMAP/Tester/Response/Sentence.pm
Criterion Covered Total %
statement 50 50 100.0
branch 5 6 83.3
condition n/a
subroutine 12 12 100.0
pod 6 7 85.7
total 73 75 97.3


line stmt bran cond sub pod time code
1 7     7   269815 use v5.20.0;
  7         32  
2             package JMAP::Tester::Response::Sentence 0.109;
3             # ABSTRACT: a single triple within a JMAP response
4              
5 7     7   631 use Moo;
  7         9312  
  7         59  
6              
7 7     7   4696 use experimental 'signatures';
  7         1635  
  7         60  
8              
9 7     7   2124 use namespace::clean;
  7         38483  
  7         71  
10              
11             #pod =head1 OVERVIEW
12             #pod
13             #pod These objects represent sentences in the JMAP response. That is, if your
14             #pod response is:
15             #pod
16             #pod [
17             #pod [ "messages", { ... }, "a" ], # 1
18             #pod [ "smellUpdates", { ... }, "b" ], # 2
19             #pod [ "smells", { ... }, "b" ], # 3
20             #pod ]
21             #pod
22             #pod ...then #1, #2, and #3 are each a single sentence.
23             #pod
24             #pod The first item in the triple is accessed with the C method. The second
25             #pod is accessed with the C method. The third, with the C
26             #pod method.
27             #pod
28             #pod =cut
29              
30             has name => (is => 'ro', required => 1);
31             has arguments => (is => 'ro', required => 1);
32             has client_id => (is => 'ro', required => 1);
33              
34             has sentence_broker => (is => 'ro', required => 1);
35              
36 2     2   3 sub _strip_json_types ($self, $whatever) {
  2         5  
  2         5  
  2         3  
37 2         16 $self->sentence_broker->strip_json_types($whatever);
38             }
39              
40             #pod =method as_triple
41             #pod
42             #pod =method as_stripped_triple
43             #pod
44             #pod C returns the underlying JSON data of the sentence, which may
45             #pod include objects used to convey type information for booleans, strings, and
46             #pod numbers.
47             #pod
48             #pod For unblessed data, use C.
49             #pod
50             #pod These return a three-element arrayref.
51             #pod
52             #pod =cut
53              
54 16     16 1 2270 sub as_triple ($self) { [ $self->name, $self->arguments, $self->client_id ] }
  16         31  
  16         23  
  16         170  
55              
56 3     3 1 44 sub as_stripped_triple ($self) {
  3         6  
  3         5  
57 3         13 $self->sentence_broker->strip_json_types($self->as_triple);
58             }
59              
60             #pod =method as_pair
61             #pod
62             #pod =method as_stripped_pair
63             #pod
64             #pod C returns the same thing as C, but without the
65             #pod C. That means it returns a two-element arrayref.
66             #pod
67             #pod C returns the same minus JSON type information.
68             #pod
69             #pod =cut
70              
71 20     20 1 446 sub as_pair ($self) { [ $self->name, $self->arguments ] }
  20         30  
  20         34  
  20         140  
72              
73 3     3 1 40 sub as_stripped_pair ($self) {
  3         7  
  3         5  
74 3         11 $self->sentence_broker->strip_json_types($self->as_pair);
75             }
76              
77             #pod =method as_set
78             #pod
79             #pod This method returns a L object for the
80             #pod current sentence. That's a specialized Sentence for C-style JMAP
81             #pod method responses.
82             #pod
83             #pod =cut
84              
85 5     5 1 176 sub as_set ($self) {
  5         15  
  5         11  
86 5 100       37 unless ($self->name =~ m{/set$}) {
87 1         13 return $self->sentence_broker->abort(
88             sprintf(qq{tried to call ->as_set on sentence named "%s"}, $self->name)
89             );
90             }
91              
92 4         824 require JMAP::Tester::Response::Sentence::Set;
93 4         161 return JMAP::Tester::Response::Sentence::Set->new({
94             name => $self->name,
95             arguments => $self->arguments,
96             client_id => $self->client_id,
97              
98             sentence_broker => $self->sentence_broker,
99             });
100             }
101              
102             #pod =method assert_named
103             #pod
104             #pod $sentence->assert_named("theName")
105             #pod
106             #pod This method aborts unless the sentence's name is the given name. Otherwise, it
107             #pod returns the sentence.
108             #pod
109             #pod =cut
110              
111 5     5 1 230 sub assert_named ($self, $name) {
  5         9  
  5         13  
  5         9  
112 5 50       18 Carp::confess("no name given") unless defined $name;
113              
114 5 100       38 return $self if $self->name eq $name;
115              
116 2         32 $self->sentence_broker->abort(
117             sprintf qq{expected sentence named "%s" but got "%s"}, $name, $self->name
118             );
119             }
120              
121 30     30 0 581 sub TO_JSON ($self) {
  30         52  
  30         46  
122 30         214 return [ $self->name, $self->arguments, $self->client_id ];
123             }
124              
125             1;
126              
127             __END__