File Coverage

blib/lib/JMAP/Tester/Response/Sentence.pm
Criterion Covered Total %
statement 23 23 100.0
branch 5 6 83.3
condition n/a
subroutine 10 10 100.0
pod 6 6 100.0
total 44 45 97.7


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