SolarBeam/Response.pm

filter: Statementstmt 94.0% 95 / 101 Branchbran 79.1% 19 / 24 Conditioncond 60.0% 15 / 25 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 70.5% 12 / 17 Podpod 100% 2 / 2 totaltotal 80.4% 148 / 184 CC 30 SCAR 27.3
CC
30
Cov
86%
parse
26.6
facet_fields_as_hashes
4.2
BEGIN
2.0
CRAP 15.3
↑ Summary SolarBeam/Query.pm »
1 package SolarBeam::Response;
2 4 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use Data::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Mojo::JSON 'decode_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Mojo::JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Mojo::Util 'decamelize';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use constant DEBUG => $ENV{SOLARBEAM_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'SOLARBEAM_DEBUG'} || 0
l!l
04
MC/DC0%0/1
$ENV{'SOLARBEAM_DEBUG'} || 0
$ENV{'SOLARBEAM_DEBUG'}
Truth table50%1/2
A
A= $ENV{'SOLARBEAM_DEBUG'}
Aresult
11
00
10  
11 0 ▶has docs => sub { +[] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
12 4 has error => undef;
13 0 ▶has facet_dates => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
14 3 ▶has facet_fields => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
15 0 ▶has facet_queries => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
16 3 ▶has facet_ranges => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
17 4 has num_found => 0;
18 5 ▶has pager => sub { Data::Page->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
19 0 ▶has params => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
20 4 has query_time => 0;
21 4 has start => 0;
22 0 ▶has terms => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
23 4 has 'tx';
24  
25 2 ▶sub facet_fields_as_hashes {
Subroutine facet_fields_as_hashes: called CC=4 SCAR=14.2
Pod documented
26 2 my $self = shift;
27  
28 2 ▶ return $self->{facet_fields_as_hashes} ||= do {
Condition33%1/3
$self->{"facet_fields_as_hashes"} ||= do { my $facet_fields = $self->facet_fields; my %res; foreach my $k (keys %$facet_fields) { $res{$k} = {map({@$_{"value", "count"};} @{$facet_fields->{$k} or [];})}; } ; return \%res }
l!l&&r!l&&!r
002
MC/DC0%0/2
$self->{"facet_fields_as_hashes"} ||= do { my $facet_fields = $self->facet_fields; my %res; foreach my $k (keys %$facet_fields) { $res{$k} = {map({@$_{"value", "count"};} @{$facet_fields->{$k} or [];})}; } ; return \%res }
$self->{"facet_fields_as_hashes"} do { my $facet_fields = $self->facet_fields; my %res; foreach my $k (keys %$facet_fields) { $res{$k} = {map({@$_{"value", "count"};} @{$facet_fields->{$k} or [];})}; } ; return \%res }
Truth table33%1/3
A ||= B
A= $self->{"facet_fields_as_hashes"}B= do { my $facet_fields = $self->facet_fields; my %res; foreach my $k (keys %$facet_fields) { $res{$k} = {map({@$_{"value", "count"};} @{$facet_fields->{$k} or [];})}; } ; return \%res }
ABresult
1X1
011
000
29 2 my $facet_fields = $self->facet_fields;
30 2 my %res;
31 2 for my $k (keys %$facet_fields) {
32 1 ▶ $res{$k} = {map { @$_{qw(value count)} } @{$facet_fields->{$k} || []}};
Condition50%1/2
$facet_fields->{$k} || []
l!l
10
MC/DC0%0/1
$facet_fields->{$k} || []
$facet_fields->{$k}
Truth table50%1/2
A
A= $facet_fields->{$k}
Aresult
11
00
33 }
34 2 return \%res;
35 };
36 }
37  
38 12 ▶sub parse {
Subroutine parse: called CC=25 SCAR=32.8
Pod documented
39 12 my ($self, $tx) = @_;
40 12 my $res = $tx->res;
41 12 ▶ my $data = $res->json || {};
Condition100%2/2
$res->json || {}
l!l
75
MC/DC100%1/1
$res->json || {}
$res->json
Truth table100%2/2
A
A= $res->json
Aresult
11
00
42 12 my $header = $data->{responseHeader};
43 12 my $response = $data->{response};
44 12 my $facets = $data->{facet_counts};
45 12 my $terms = $data->{terms};
46 12 my $field;
47 12 $self->tx($tx);
48  
49 12 ▶ if ($data->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"error"}) 1 11 12
50 1 ▶ $self->error({code => $data->{error}{code} || $tx->res->code, message => $data->{error}{msg}});
Condition33%1/3
$data->{'error'}{'code'} || $tx->res->code
l!l&&r!l&&!r
100
MC/DC0%0/2
$data->{'error'}{'code'} || $tx->res->code
$data->{'error'}{'code'} $tx->res->code
Truth table33%1/3
A || B
A= $data->{'error'}{'code'}B= $tx->res->code
ABresult
1X1
011
000
51 1 return $self;
52 }
53  
54 11 ▶ if ($res->code and !$header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code and not $header) 3 8 11
Condition100%3/3
$res->code and not $header
!ll&&!rl&&r
353
MC/DC100%2/2
$res->code and not $header
$res->code not $header
Truth table100%3/3
A and not(B)
A= $res->codeB= not $header
ABresult
0X0
100
111
55 3 my $dom = $res->dom;
56  
57 3 ▶ if ($dom and $dom->at('title')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dom and $dom->at('title')) { } 0 3 3
Condition33%1/3
$dom and $dom->at("title")
!ll&&!rl&&r
030
MC/DC0%0/2
$dom and $dom->at("title")
$dom $dom->at("title")
Truth table33%1/3
A and B
A= $domB= $dom->at("title")
ABresult
0X0
100
111
58 0 $self->error({code => $res->code, message => $dom->at('title')->text});
59 }
60 else {
61 3 ▶ $self->error({code => $res->code, message => $res->body || 'Missing response headers.'});
Condition100%2/2
$res->body || 'Missing response headers.'
l!l
21
MC/DC100%1/1
$res->body || 'Missing response headers.'
$res->body
Truth table100%2/2
A
A= $res->body
Aresult
11
00
62 }
63 3 return $self;
64 }
65  
66 8 ▶ if ($tx->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->error) 3 5 8
67 3 ▶ $tx->error->{message} ||= 'Unknown error';
Condition100%2/2
$tx->error->{"message"} ||= "Unknown error"
l!l
21
MC/DC100%1/1
$tx->error->{"message"} ||= "Unknown error"
$tx->error->{"message"}
Truth table100%2/2
A
A= $tx->error->{"message"}
Aresult
11
00
68 3 $self->error($tx->error);
69 3 return $self;
70 }
71  
72 5 for $field (keys %$header) {
73 14 my $method = decamelize ucfirst $field;
74 14 ▶ $self->$method($header->{$field}) if $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) 4 10 14
75 }
76  
77 5 for $field (keys %$response) {
78 13 my $method = decamelize ucfirst $field;
79 13 ▶ $self->$method($response->{$field}) if $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) 12 1 13
80 }
81  
82 5 for $field (keys %$facets) {
83 8 ▶ $self->$field($facets->{$field}) if $self->can($field);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($field) 8 0 8
84 }
85  
86 5 my $ff = $self->facet_fields;
87 5 ▶ if ($ff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ff) 5 0 5
88 5 for $field (keys %$ff) {
89 1 $ff->{$field} = $self->_build_count_list($ff->{$field});
90 }
91 }
92  
93 5 ▶ if ($self->facet_ranges) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->facet_ranges) 5 0 5
94 5 for $field (keys %{$self->facet_ranges}) {
95 1 my $range = $self->facet_ranges->{$field};
96 1 $range->{counts} = $self->_build_count_list($range->{counts});
97 }
98 }
99  
100 5 ▶ if ($terms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($terms) 1 4 5
101 1 my $sane_terms = {};
102 1 for $field (keys %$terms) {
103 2 $sane_terms->{$field} = $self->_build_count_list($terms->{$field});
104 }
105 1 $self->terms($sane_terms);
106 }
107  
108 5 ▶ if (!$self->error && $response) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->error and $response) 5 0 5
Condition33%1/3
not $self->error and $response
!ll&&!rl&&r
005
MC/DC0%0/2
not $self->error and $response
not $self->error $response
Truth table33%1/3
not(A) and B
A= not $self->errorB= $response
ABresult
0X0
100
111
109 5 $self->pager->total_entries($self->num_found);
110 }
111  
112 5 $self;
113 }
114  
115 4 ▶sub _build_count_list {
Subroutine _build_count_list: called CC=2 SCAR=6.9
116 4 my ($self, $list) = @_;
117 4 my @result = ();
118 4 ▶ for (my $i = 1; $i < @$list; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$list) 130 4 134
119 130 push @result, {value => $list->[$i - 1], count => $list->[$i]};
120 }
121 4 return \@result;
122 }
123  
124 4 1;
125  
126 =encoding utf8
127  
128 =head1 NAME
129  
130 SolarBeam::Response - Represents a Solr search response
131  
132 =head1 SYNOPSIS
133  
134 use SolarBeam::Response;
135 my $tx = Mojo::UserAgent->new->post($solr_url, form => \%query);
136 my $res = SolarBeam::Response->new->parse($tx);
137  
138 if ($res->error) {
139 die sprintf "%s: %s", $res->error->{code} || 0, $res->error->{message};
140 }
141  
142 for my $doc (@{$res->docs}) {
143 say $doc->{surname};
144 }
145  
146 =head1 DESCRIPTION
147  
148 L<SolarBeam::Response> holds the response from L<SolarBeam/autocomplete> or
149 L<SolarBeam/search>.
150  
151 =head1 ATTRIBUTES
152  
153 =head2 docs
154  
155 $array_ref = $self->docs;
156 $self = $self->docs([{}, ...]);
157  
158 Holds a list of the documents retrieved from Solr.
159  
160 =head2 error
161  
162 $hash_ref = $self->error;
163 $self = $self->error({message => "Error message", code => 500});
164  
165 Holds either a hash-ref with error details or C<undef()> if no error is
166 detected. This attribute is modeled the same way as L<Mojo::Transaction/error>,
167 but can also contain detailed error messages from the Solr server.
168  
169 =head2 facet_dates
170  
171 $hash_ref = $self->facet_dates;
172  
173 TODO.
174  
175 =head2 facet_fields
176  
177 $hash_ref = $self->facet_fields;
178  
179 TODO.
180  
181 =head2 facet_queries
182  
183 $hash_ref = $self->facet_queries;
184  
185 TODO.
186  
187 =head2 facet_ranges
188  
189 $hash_ref = $self->facet_ranges;
190  
191 TODO.
192  
193 =head2 num_found
194  
195 $int = $self->num_found;
196  
197 Holds the number of matching documents. This number can be higher than the
198 number of elements in L</docs>.
199  
200 =head2 pager
201  
202 $pager = $self->pager;
203 $self = $self->pager(Data::Page->new);
204  
205 Holds a L<Data::Page> object.
206  
207 =head2 params
208  
209 $hash_ref = $self->params;
210  
211 Holds the search params sent to Solr.
212  
213 =head2 query_time
214  
215 $int = $self->query_time;
216  
217 The time the search took.
218  
219 =head2 start
220  
221 $int = $self->start;
222  
223 Offset of the search result.
224  
225 =head2 terms
226  
227 $hash_ref = $self->terms;
228  
229 =head2 tx
230  
231 last tx passed to parse
232  
233 TODO
234  
235 =head1 METHODS
236  
237 =head2 facet_fields_as_hashes
238  
239 $hash_ref = $self->facet_fields_as_hashes;
240  
241 Turns the arrays in L</facet_fields> into hashes instead. Example:
242  
243 $self->facet_fields = {colors => [{value => "red", count => 42}]};
244 $self->facet_fields_as_hashes = {colors => {red => 42}}
245  
246 =head2 parse
247  
248 $self = $self->parse(Mojo::Transaction->new);
249  
250 Used to parse the result from a query. Will populate the different
251 L</ATTRIBUTES>.
252  
253 =head1 SEE ALSO
254  
255 L<SolarBeam>.
256  
257 =cut
↑ Summary SolarBeam/Query.pm »