File Coverage

blib/lib/JMAP/Tester/Response/Sentence.pm
Criterion Covered Total %
statement 46 50 92.0
branch 5 6 83.3
condition n/a
subroutine 11 12 91.6
pod 6 7 85.7
total 68 75 90.6


line stmt bran cond sub pod time code
1 2     2   22 use v5.20.0;
  2         7  
2             package JMAP::Tester::Response::Sentence 0.110;
3             # ABSTRACT: a single triple within a JMAP response
4              
5 2     2   10 use Moo;
  2         4  
  2         9  
6              
7 2     2   772 use experimental 'signatures';
  2         6  
  2         10  
8              
9 2     2   345 use namespace::clean;
  2         4  
  2         12  
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         4  
  2         1  
  2         2  
37 2         9 $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 2062 sub as_triple ($self) { [ $self->name, $self->arguments, $self->client_id ] }
  16         27  
  16         22  
  16         130  
55              
56 3     3 1 24 sub as_stripped_triple ($self) {
  3         5  
  3         3  
57 3         6 $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 407 sub as_pair ($self) { [ $self->name, $self->arguments ] }
  20         32  
  20         27  
  20         145  
72              
73 3     3 1 23 sub as_stripped_pair ($self) {
  3         4  
  3         3  
74 3         7 $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 11     11 1 16 sub as_set ($self) {
  11         16  
  11         12  
86 11 100       51 unless ($self->name =~ m{/set$}) {
87 1         10 return $self->sentence_broker->response->abort(
88             sprintf(qq{tried to call ->as_set on sentence named "%s"}, $self->name)
89             );
90             }
91              
92 10         802 require JMAP::Tester::Response::Sentence::Set;
93 10         194 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 108 sub assert_named ($self, $name) {
  5         7  
  5         6  
  5         5  
112 5 50       12 Carp::confess("no name given") unless defined $name;
113              
114 5 100       22 return $self if $self->name eq $name;
115              
116 2         23 $self->sentence_broker->response->abort(
117             sprintf qq{expected sentence named "%s" but got "%s"}, $name, $self->name
118             );
119             }
120              
121 0     0 0   sub TO_JSON ($self) {
  0            
  0            
122 0           return [ $self->name, $self->arguments, $self->client_id ];
123             }
124              
125             1;
126              
127             __END__