SolarBeam.pm

filter: Statementstmt 84.2% 107 / 127 Branchbran 90.6% 29 / 32 Conditioncond 40.0% 4 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 79.1% 19 / 24 Podpod 100% 4 / 4 totaltotal 80.7% 164 / 203 CC 23 SCAR 13.5
CC
23
Cov
83%
__ANON__
12.0
_build_url
7.0
autocomplete
6.0
CRAP 3.9
« SolarBeam/Query.pm ↑ Summary SolarBeam/Util.pm »
1 package SolarBeam;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use SolarBeam::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use SolarBeam::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use SolarBeam::Util 'escape';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.05';
12  
13 1 ▶has ua => sub { Mojo::UserAgent->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 2 ▶has default_query => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 0 ▶sub autocomplete {
Subroutine autocomplete: not called CC=2 SCAR=17.9
Pod documented
17 0 my $cb = pop;
18 0 my ($self, $prefix, %options) = @_;
19 0 ▶ my $postfix = delete $options{'-postfix'} || '\w+';
Condition0%0/2
delete $options{'-postfix'} || '\\w+'
l!l
00
MC/DC0%0/1
delete $options{'-postfix'} || '\\w+'
delete $options{'-postfix'}
Truth table0%0/2
A
A= delete $options{'-postfix'}
Aresult
11
00
20  
21 0 $options{'regex.flag'} = 'case_insensitive';
22 0 $options{'regex'} = quotemeta($prefix) . $postfix;
23 0 my $options = {terms => \%options, -endpoint => 'terms'};
24 0 my $url = $self->_build_url($options);
25  
26 Mojo::IOLoop->delay(
27 0 ▶ sub { $self->ua->get($url, shift->begin) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 my ($delay, $tx) = @_;
30 0 $self->$cb(SolarBeam::Response->new->parse($tx));
31 }
32 0 );
33  
34 0 return $self;
35 }
36  
37 3 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
38 3 my $self = shift->SUPER::new(@_);
39 3 ▶ $self->url($self->{url}) if $self->{url};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"url"} 1 2 3
40 3 $self;
41 }
42  
43 1 ▶sub search {
Subroutine search: called CC=1 SCAR=1.9
Pod documented
44 1 my $cb = pop;
45 1 my ($self, $query, %options) = @_;
46 1 my $options = \%options;
47 1 my $page = $options->{page};
48  
49 1 $options->{-query} = $query;
50 1 my $url = $self->_build_url($options);
51 1 my $q = $url->query;
52 1 $url->query(Mojo::Parameters->new);
53  
54 Mojo::IOLoop->delay(
55 0 ▶ sub { $self->ua->post($url, form => $q->to_hash, shift->begin) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
57 0 my ($delay, $tx) = @_;
58 0 my $res = SolarBeam::Response->new->parse($tx);
59  
60 0 ▶ if ($page && !$res->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page and not $res->error) 0 0 0
Condition0%0/3
$page and not $res->error
!ll&&!rl&&r
000
MC/DC0%0/2
$page and not $res->error
$page not $res->error
Truth table0%0/3
A and not(B)
A= $pageB= not $res->error
ABresult
0X0
100
111
61 0 $res->pager->current_page($page);
62 0 $res->pager->entries_per_page($options->{rows});
63 }
64  
65 0 $self->$cb($res);
66 }
67 1 );
68  
69 0 return $self;
70 }
71  
72 17 ▶sub url {
Subroutine url: called CC=3 SCAR=11.0
Pod documented
73 17 my $self = shift;
74 17 ▶ return $self->{url} ||= Mojo::URL->new('http://localhost:8983/solr') unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 14 3 17
Condition66%2/3
$self->{"url"} ||= "Mojo::URL"->new("http://localhost:8983/solr")
l!l&&r!l&&!r
1310
MC/DC0%0/2
$self->{"url"} ||= "Mojo::URL"->new("http://localhost:8983/solr")
$self->{"url"} "Mojo::URL"->new("http://localhost:8983/solr")
Truth table66%2/3
A ||= B
A= $self->{"url"}B= "Mojo::URL"->new("http://localhost:8983/solr")
ABresult
1X1
011
000
75 3 $self->{url} = Mojo::URL->new(shift);
76 3 return $self;
77 }
78  
79 3 ▶sub _build_hash {
Subroutine _build_hash: called CC=3 SCAR=11.0
80 3 my ($self, %fields) = @_;
81 3 my @query;
82  
83 3 for my $field (keys %fields) {
84 3 my $val = $fields{$field};
85 3 ▶ my @vals = ref($val) eq 'ARRAY' ? @{$val} : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 1 2 3
86 3 push @query, join(' OR ', map { $field . ':(' . escape($_) . ')' } @vals);
87 }
88  
89 3 return '(' . join(' AND ', @query) . ')';
90 }
91  
92 10 ▶sub _build_query {
Subroutine _build_query: called CC=3 SCAR=11.0
93 10 my ($self, $query) = @_;
94  
95 10 my $type = ref($query);
96 10 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 3 7 10
elsif ($type eq 'ARRAY') { } 3 4 7
97 3 $self->_build_hash(%{$query});
98 }
99 elsif ($type eq 'ARRAY') {
100 3 my ($raw, @params) = @$query;
101 3 $raw =~ s|%@|escape(shift @params)|ge;
102 3 my %params = @params;
103 3 $raw =~ s|%([a-z]+)|escape($params{$1})|ge;
104 3 $raw;
105 }
106 else {
107 4 $query;
108 }
109 }
110  
111 10 ▶sub _build_url {
Subroutine _build_url: called CC=7 SCAR=19.5
112 10 my ($self, $options) = @_;
113 10 my $endpoint = delete $options->{-endpoint};
114 10 my $query = delete $options->{-query};
115 10 my $url = $self->url->clone;
116  
117 10 ▶ $url->path($endpoint || 'select');
Condition100%2/2
$endpoint || 'select'
l!l
19
MC/DC100%1/1
$endpoint || 'select'
$endpoint
Truth table100%2/2
A
A= $endpoint
Aresult
11
00
118 10 ▶ $url->query(q => $self->_build_query($query)) if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 1 9 10
119 10 $url->query($self->default_query);
120 10 $url->query({wt => 'json'});
121  
122 10 ▶ if ($options->{page}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"page"}) 1 9 10
123 1 $self->_handle_page($options->{page}, $options);
124 }
125  
126 10 ▶ if ($options->{fq}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"fq"}) 3 7 10
127 3 $self->_handle_fq($options->{fq}, $options);
128 }
129  
130 10 ▶ if ($options->{facet}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"facet"}) 2 8 10
131 2 $self->_handle_facet($options->{facet}, $options);
132 }
133  
134 10 ▶ if ($options->{terms}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"terms"}) 1 9 10
135 1 $self->_handle_nested_hash('terms', $options->{terms}, $options);
136 }
137  
138 10 $url->query($options);
139  
140 10 return $url;
141 }
142  
143 3 ▶sub _handle_fq {
Subroutine _handle_fq: called CC=2 SCAR=6.9
144 3 my ($self, $fq, $options) = @_;
145  
146 3 ▶ if (ref($fq) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fq eq 'ARRAY') { } 2 1 3
147 2 my @queries = map { $self->_build_query($_) } @{$fq};
148 2 $options->{fq} = \@queries;
149 }
150 else {
151 1 $options->{fq} = $self->_build_query($fq);
152 }
153 3 return;
154 }
155  
156 2 ▶sub _handle_facet {
Subroutine _handle_facet: called CC=1 SCAR=0.0
157 2 my ($self, $facet, $options) = @_;
158 2 $self->_handle_nested_hash('facet', $facet, $options);
159 }
160  
161 15 ▶sub _handle_nested_hash {
Subroutine _handle_nested_hash: called CC=5 SCAR=16.1
162 15 my ($self, $prefix, $content, $options) = @_;
163 15 my $type = ref $content;
164  
165 15 ▶ if ($type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'HASH') { } 4 11 15
166 4 ▶ $content->{-value} or $content->{-value} = 'true';
Branch: 1 branch
BranchTrueFalseTotal
$content->{"-value"} or $content->{"-value"} = "true" 1 3 4
167  
168 4 for my $key (keys %{$content}) {
169 12 my $name = $prefix;
170 12 ▶ $name .= '.' . $key if $key ne '-value';
Branch: 1 branch
BranchTrueFalseTotal
if $key ne "-value" 8 4 12
171 12 $self->_handle_nested_hash($name, $content->{$key}, $options);
172 }
173 }
174 else {
175 11 $options->{$prefix} = $content;
176 }
177 }
178  
179 1 ▶sub _handle_page {
Subroutine _handle_page: called CC=2 SCAR=7.0
180 1 my ($self, $page, $options) = @_;
181 1 ▶ die "You must provide both page and rows" unless $options->{rows};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"rows"} 0 1 1
182 1 $options->{start} = ($page - 1) * $options->{rows};
183 1 return delete $options->{page};
184 }
185  
186 2 1;
187  
188 =encoding utf8
189  
190 =head1 NAME
191  
192 SolarBeam - Async Solr search driver
193  
194 =head1 VERSION
195  
196 0.04
197  
198 =head1 SYNOPSIS
199  
200 use SolarBeam;
201 my $solr = SolarBeam->new;
202 $solr->search(...);
203  
204 =head1 DESCRIPTION
205  
206 Interface to acquire Solr index engine connections.
207  
208 L<SolarBeam> is currently EXPERIMENTAL.
209  
210 =head1 ATTRIBUTES
211  
212 L<SolarBeam> implements the the following attributes.
213  
214 =head2 ua
215  
216 $ua = $self->ua
217 $self = $self->ua(Mojo::UserAgent->new);
218  
219 A L<Mojo::UserAgent> compatible object.
220  
221 =head2 url
222  
223 $url = $self->url;
224  
225 Solr endpoint as a L<Mojo::URL> object. Note that passing in L</url> as a
226 string to L</new> also works.
227  
228 =head2 default_query
229  
230 A hashref with default parameters used for every query.
231  
232 =head1 METHODS
233  
234 =head2 new
235  
236 $self = SolarBeam->new(%attributes);
237  
238 Object constructor.
239  
240 =head2 search
241  
242 $self = $self->search($query, [%options], sub { my ($self, $res) = @_; });
243  
244 Used to search for data in Solr. C<$res> is a L<SolarBeam::Response> object.
245  
246 Example C<$query>:
247  
248 =over 2
249  
250 =item * Hash
251  
252 $self->search({surname => q("Thorsen"), age => [33, 34]});
253  
254 The query above will result in this Solr query:
255  
256 (surname:("Thorsen") AND age:(33) OR age:(34))
257  
258 =item * String
259  
260 $self->search("active:1");
261  
262 The query above will result in this Solr query:
263  
264 active:1
265  
266 =back
267  
268 C<%options> can hold Solr query parameters and some special instuctions
269 to this module, such a "page" and "rows".
270  
271 =over 2
272  
273 =item * page
274  
275 Used to calculate the offset together with L</rows>. Will also be used to set
276 L<Data::Page> attributes in L<SolarBeam::Response/pager>:
277  
278 $res->pager->current_page($page);
279  
280 =item * rows
281  
282 Used to calculate the offset together with L</page>. Will also be used to set
283 L<Data::Page> attributes in L<SolarBeam::Response/pager>:
284  
285 $res->pager->entries_per_page($rows);
286  
287 =back
288  
289 =head2 autocomplete
290  
291 $self = $self->autocomplete($prefix, [%options], sub { my ($self, $res) = @_; });
292  
293 TODO.
294  
295 C<$res> is a L<SolarBeam::Response> object.
296  
297 C<%options> can be:
298  
299 =over 2
300  
301 =item * -postfix - defaults to \w+
302  
303 =item * regex.flag -
304  
305 =item * regex -
306  
307 =back
308  
309 =head1 COPYRIGHT AND LICENSE
310  
311 Copyright (C) 2011-2016, Magnus Holm
312  
313 This program is free software, you can redistribute it and/or modify it under
314 the terms of the Artistic License version 2.0.
315  
316 =head1 AUTHOR
317  
318 Magnus Holm - C<judofyr@gmail.com>
319  
320 Jan Henning Thorsen - C<jhthorsen@cpan.org>
321  
322 Nicolas Mendoza - C<mendoza@pvv.ntnu.no>
323  
324 =cut
« SolarBeam/Query.pm ↑ Summary SolarBeam/Util.pm »