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   13 use v5.10.0;
  1         3  
2             package JMAP::Tester::Response::Sentence 0.101;
3             # ABSTRACT: a single triple within a JMAP response
4              
5 1     1   7 use Moo;
  1         2  
  1         6  
6              
7 1     1   343 use namespace::clean;
  1         2  
  1         10  
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   6 my ($self, $whatever) = @_;
36 2         13 $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 2054 sub as_triple { [ $_[0]->name, $_[0]->arguments, $_[0]->client_id ] }
54              
55             sub as_stripped_triple {
56 3     3 1 53 $_[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 18     18 1 477 sub as_pair { [ $_[0]->name, $_[0]->arguments ] }
71              
72             sub as_stripped_pair {
73 3     3 1 50 $_[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 15 my ($self) = @_;
86              
87 5 100       40 unless ($self->name =~ m{/set$}) {
88 1         17 return $self->sentence_broker->abort(
89             sprintf(qq{tried to call ->as_set on sentence named "%s"}, $self->name)
90             );
91             }
92              
93 4         925 require JMAP::Tester::Response::Sentence::Set;
94 4         136 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       43 Carp::confess("no name given") unless defined $name;
116              
117 5 100       37 return $self if $self->name eq $name;
118              
119 2         40 $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__