WebSearch.pm

filter: Statementstmt 92.2% 261 / 283 Branchbran 67.2% 78 / 116 Conditioncond 52.7% 57 / 108 MC/DCmcdc 20.0% 13 / 65 Subroutinesub 89.5% 43 / 48 Podpod 100% 12 / 12 totaltotal 73.4% 464 / 632 CC 109 SCAR 19.6
CC
109
Cov
78%
_fetch_results
12.9
__ANON__
12.2
configure_unknown
12.0
CRAP 7.1
« WebSearch/Provider/Reddit/OAuth.pm ↑ Summary WebSearch/Provider.pm »
1 package Net::Async::WebSearch;
2 # ABSTRACT: IO::Async multi-provider web search aggregator
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use parent 'IO::Async::Notifier';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Future ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Future::Utils qw( fmap_void );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use HTTP::Request::Common qw( GET );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Net::Async::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use Net::Async::WebSearch::Provider ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Net::Async::WebSearch::Result ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 our $VERSION = '0.002';
18  
19 # Reciprocal Rank Fusion constant (Cormack et al.)
20 6 our $RRF_K = 60;
21  
22 sub _init {
23 21 ▶ my ( $self, $args ) = @_;
Subroutine _init: called CC=8 SCAR=21.6
24 21 $self->SUPER::_init($args);
25 21 $self->{providers} = [];
26 21 $self->{http} = delete $args->{http};
27 21 ▶ $self->{rrf_k} = delete $args->{rrf_k} // $RRF_K;
Condition33%1/3
delete $args->{"rrf_k"} // $Net::Async::WebSearch::RRF_K
l!l&&r!l&&!r
0210
MC/DC0%0/2
delete $args->{"rrf_k"} // $Net::Async::WebSearch::RRF_K
delete $args->{"rrf_k"} $Net::Async::WebSearch::RRF_K
Truth table33%1/3
A // B
A= delete $args->{"rrf_k"}B= $Net::Async::WebSearch::RRF_K
ABresult
1X1
011
000
28 21 ▶ $self->{default_limit} = delete $args->{default_limit} // 10;
Condition50%1/2
delete $args->{"default_limit"} // 10
l!l
021
MC/DC0%0/1
delete $args->{"default_limit"} // 10
delete $args->{"default_limit"}
Truth table50%1/2
A
A= delete $args->{"default_limit"}
Aresult
11
00
29 21 ▶ $self->{per_provider_limit} = delete $args->{per_provider_limit} // 10;
Condition50%1/2
delete $args->{"per_provider_limit"} // 10
l!l
021
MC/DC0%0/1
delete $args->{"per_provider_limit"} // 10
delete $args->{"per_provider_limit"}
Truth table50%1/2
A
A= delete $args->{"per_provider_limit"}
Aresult
11
00
30 21 ▶ $self->{fetch_concurrency} = delete $args->{fetch_concurrency} // 100;
Condition50%1/2
delete $args->{"fetch_concurrency"} // 100
l!l
021
MC/DC0%0/1
delete $args->{"fetch_concurrency"} // 100
delete $args->{"fetch_concurrency"}
Truth table50%1/2
A
A= delete $args->{"fetch_concurrency"}
Aresult
11
00
31 $self->{fetch_concurrency_per_target_ip}
32 21 ▶ = delete $args->{fetch_concurrency_per_target_ip} // 5;
Condition50%1/2
delete $args->{"fetch_concurrency_per_target_ip"} // 5
l!l
021
MC/DC0%0/1
delete $args->{"fetch_concurrency_per_target_ip"} // 5
delete $args->{"fetch_concurrency_per_target_ip"}
Truth table50%1/2
A
A= delete $args->{"fetch_concurrency_per_target_ip"}
Aresult
11
00
33 21 $self->{fetch_timeout} = delete $args->{fetch_timeout};
34 21 $self->{fetch_max_bytes} = delete $args->{fetch_max_bytes};
35 21 $self->{fetch_user_agent} = delete $args->{fetch_user_agent};
36 21 ▶ if ( my $provs = delete $args->{providers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $provs = delete $args->{"providers"}) 9 12 21
37 9 $self->add_provider($_) for @$provs;
38 }
39 21 return;
40 }
41  
42 sub configure_unknown {
43 0 ▶ my ( $self, %args ) = @_;
Subroutine configure_unknown: not called CC=3 SCAR=24.8
Pod documented
44 0 for my $k (qw(
45 http rrf_k default_limit per_provider_limit providers
46 fetch_concurrency fetch_concurrency_per_target_ip
47 fetch_timeout fetch_max_bytes fetch_user_agent
48 )) {
49 0 delete $args{$k};
50 }
51 0 ▶ return unless %args;
Branch: 1 branch
BranchTrueFalseTotal
unless %args 0 0 0
52 0 croak "Unknown configuration keys: ".join(',', sort keys %args);
53 }
54  
55 1 ▶sub providers { @{ $_[0]->{providers} } }
Subroutine providers: called CC=1 SCAR=0.0
Pod documented
56 26 ▶sub rrf_k { @_ > 1 ? ($_[0]->{rrf_k} = $_[1]) : $_[0]->{rrf_k} }
Subroutine rrf_k: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 26 26
57 25 ▶sub default_limit { @_ > 1 ? ($_[0]->{default_limit} = $_[1]) : $_[0]->{default_limit} }
Subroutine default_limit: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 25 25
58 30 ▶sub per_provider_limit{ @_ > 1 ? ($_[0]->{per_provider_limit} = $_[1]) : $_[0]->{per_provider_limit} }
Subroutine per_provider_limit: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 30 30
59  
60 sub add_provider {
61 28 ▶ my ( $self, $p ) = @_;
Subroutine add_provider: called CC=5 SCAR=16.4
Pod documented
62 28 ▶ croak "provider must be a Net::Async::WebSearch::Provider"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p and $p->isa("Net::Async::WebSearch::Provider") 0 28 28
Condition33%1/3
ref $p and $p->isa("Net::Async::WebSearch::Provider")
!ll&&!rl&&r
0028
MC/DC0%0/2
ref $p and $p->isa("Net::Async::WebSearch::Provider")
ref $p $p->isa("Net::Async::WebSearch::Provider")
Truth table33%1/3
A and B
A= ref $pB= $p->isa("Net::Async::WebSearch::Provider")
ABresult
0X0
100
111
63 unless ref $p && $p->isa('Net::Async::WebSearch::Provider');
64 28 my %taken = map { $_->name => 1 } @{ $self->{providers} };
65 28 ▶ if ( $taken{ $p->name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taken{$p->name}) 1 27 28
66 1 my $base = $p->name;
67 1 my $n = 2;
68 1 ▶ $n++ while $taken{ "$base#$n" };
Branch: 1 branch
BranchTrueFalseTotal
$taken{"$base#$n"} and do { ++$n } 0 1 1
69 1 $p->name( "$base#$n" );
70 }
71 28 push @{ $self->{providers} }, $p;
72 28 return $p;
73 }
74  
75 sub provider {
76 0 ▶ my ( $self, $name ) = @_;
Subroutine provider: not called CC=3 SCAR=24.8
Pod documented
77 0 for my $p ( @{ $self->{providers} } ) {
78 0 ▶ return $p if $p->name eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $p->name eq $name 0 0 0
79 }
80 0 return;
81 }
82  
83 sub providers_matching {
84 2 ▶ my ( $self, $sel ) = @_;
Subroutine providers_matching: called CC=1 SCAR=0.0
Pod documented
85 2 return grep { $_->matches($sel) } @{ $self->{providers} };
86 }
87  
88 sub http {
89 55 ▶ my ( $self ) = @_;
Subroutine http: called CC=3 SCAR=11.0
Pod documented
90 55 ▶ return $self->{http} if $self->{http};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"http"} 53 2 55
91 my $http = Net::Async::HTTP->new(
92 user_agent => 'Net-Async-WebSearch/'.$VERSION,
93 2 ▶ max_connections_per_host => $self->{fetch_concurrency_per_target_ip} // 5,
Condition50%1/2
$self->{"fetch_concurrency_per_target_ip"} // 5
l!l
20
MC/DC0%0/1
$self->{"fetch_concurrency_per_target_ip"} // 5
$self->{"fetch_concurrency_per_target_ip"}
Truth table50%1/2
A
A= $self->{"fetch_concurrency_per_target_ip"}
Aresult
11
00
94 max_in_flight => 0,
95 );
96 2 $self->add_child($http);
97 2 return $self->{http} = $http;
98 }
99  
100 sub _on_added_to_loop {
101 0 ▶ my ( $self, $loop ) = @_;
Subroutine _on_added_to_loop: not called CC=2 SCAR=17.9
102 0 ▶ $self->SUPER::_on_added_to_loop($loop) if $self->can('SUPER::_on_added_to_loop');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("SUPER::_on_added_to_loop") 0 0 0
103 0 $self->http;
104 }
105  
106 #----------------------------------------------------------------------
107 # Provider selection
108 #----------------------------------------------------------------------
109  
110 sub _select_providers {
111 30 ▶ my ( $self, %args ) = @_;
Subroutine _select_providers: called CC=9 SCAR=22.0
112 30 my $only = $args{only};
113 30 my $exclude = $args{exclude};
114 30 my @sel;
115 30 for my $p ( @{ $self->{providers} } ) {
116 64 ▶ next unless $p->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $p->enabled 1 63 64
117 63 ▶ if ( $only && @$only ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($only and @$only) 34 29 63
Condition66%2/3
$only and @$only
!ll&&!rl&&r
29034
MC/DC50%1/2
$only and @$only
$only @$only
Truth table66%2/3
A and B
A= $onlyB= @$only
ABresult
0X0
100
111
118 34 ▶ next unless grep { $p->matches($_) } @$only;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$p->matches($_);} @$only 15 19 34
119 }
120 48 ▶ if ( $exclude && @$exclude ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude and @$exclude) 18 30 48
Condition66%2/3
$exclude and @$exclude
!ll&&!rl&&r
30018
MC/DC50%1/2
$exclude and @$exclude
$exclude @$exclude
Truth table66%2/3
A and B
A= $excludeB= @$exclude
ABresult
0X0
100
111
121 18 ▶ next if grep { $p->matches($_) } @$exclude;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$p->matches($_);} @$exclude 7 11 18
122 }
123 41 push @sel, $p;
124 }
125 30 return @sel;
126 }
127  
128 #----------------------------------------------------------------------
129 # URL normalization for dedup
130 #----------------------------------------------------------------------
131  
132 sub _normalize_url {
133 79 ▶ my ( $self, $url ) = @_;
Subroutine _normalize_url: called CC=4 SCAR=14.6
134 79 ▶ return '' unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 79 79
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
0079
MC/DC0%0/2
defined $url and length $url
defined $url length $url
Truth table33%1/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
135 79 ▶ my $u = eval { URI->new($url)->canonical } or return lc $url;
Branch: 1 branch
BranchTrueFalseTotal
my $u = eval { do { "URI"->new($url)->canonical } } or return lc $url 0 79 79
136 79 my $s = $u->as_string;
137 79 $s =~ s{#.*$}{};
138 79 $s =~ s{/+$}{};
139 79 return lc $s;
140 }
141  
142 #----------------------------------------------------------------------
143 # Fetch (optional page-body retrieval after search)
144 #----------------------------------------------------------------------
145  
146 sub _fetch_one {
147 12 ▶ my ( $self, $result, %args ) = @_;
Subroutine _fetch_one: called CC=7 SCAR=20.4
148 12 my $url = $result->url;
149 12 ▶ return Future->done unless defined $url && length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 12 12
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $url and length $url
defined $url length $url
Truth table33%1/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
150 12 my $req = GET($url);
151 $req->header( 'User-Agent' => $args{user_agent} // $self->{fetch_user_agent}
152 12 ▶ // 'Net-Async-WebSearch/'.$VERSION );
Condition80%4/5
($args{"user_agent"} // $self->{"fetch_user_agent"}) // 'Net-Async-WebSearch/' . $Net::Async::WebSearch::VERSION
l!l
111
$args{"user_agent"} // $self->{"fetch_user_agent"}
l!l&&r!l&&!r
1011
MC/DC50%1/2
($args{"user_agent"} // $self->{"fetch_user_agent"}) // 'Net-Async-WebSearch/' . $Net::Async::WebSearch::VERSION
$args{"user_agent"} $self->{"fetch_user_agent"}
Truth table66%2/3
A // B
A= $args{"user_agent"}B= $self->{"fetch_user_agent"}
ABresult
1X1
011
000
153 12 ▶ $req->header( 'Accept' => $args{accept} // '*/*' );
Condition50%1/2
$args{"accept"} // "*/*"
l!l
012
MC/DC0%0/1
$args{"accept"} // "*/*"
$args{"accept"}
Truth table50%1/2
A
A= $args{"accept"}
Aresult
11
00
154  
155 12 my %req_args = ( request => $req );
156 12 ▶ if ( defined $args{timeout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"timeout"}) 0 12 12
157 0 $req_args{timeout} = $args{timeout};
158 }
159 # NB: max_bytes is enforced on the decoded body below — Net::Async::HTTP
160 # does not cap the on-the-wire body length itself.
161  
162 return $self->http->do_request(%req_args)->then(sub {
163 11 ▶ my ( $resp ) = @_;
Subroutine __ANON__: called CC=10 SCAR=24.1
164 11 my $ct = $resp->header('Content-Type');
165 11 ▶ my $charset = ( $ct && $ct =~ /charset=([^\s;]+)/ ) ? lc $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ct && $ct =~ /charset=([^\s;]+)/ ? : 11 0 11
Condition33%1/3
$ct and $ct =~ /charset=([^\s;]+)/
!ll&&!rl&&r
0011
MC/DC0%0/2
$ct and $ct =~ /charset=([^\s;]+)/
$ct $ct =~ /charset=([^\s;]+)/
Truth table33%1/3
A and B
A= $ctB= $ct =~ /charset=([^\s;]+)/
ABresult
0X0
100
111
166 11 my $body = eval { $resp->decoded_content };
167 11 ▶ $body = $resp->content if !defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 11 11
168 11 ▶ if ( defined $args{max_bytes} && defined $body && length($body) > $args{max_bytes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"max_bytes"} and defined $body and length $body > $args{"max_bytes"}) 1 10 11
Condition66%4/6
defined $args{"max_bytes"} and defined $body and length $body > $args{"max_bytes"}
!ll&&!rl&&r
1001
defined $args{"max_bytes"} and defined $body
!ll&&!rl&&r
1001
MC/DC33%1/3
defined $args{"max_bytes"} and defined $body and length $body > $args{"max_bytes"}
defined $args{"max_bytes"} defined $body length $body > $args{"max_bytes"}
Truth table50%2/4
A and B and C
A= defined $args{"max_bytes"}B= defined $bodyC= length $body > $args{"max_bytes"}
ABCresult
0XX0
10X0
1100
1111
169 1 $body = substr($body, 0, $args{max_bytes});
170 }
171 $result->fetched({
172 11 ▶ ok => $resp->is_success ? 1 : 0,
Branch: 3 branches
BranchTrueFalseTotal
$resp->is_success ? : 10 1 11
$resp->request ? : 11 0 11
$resp->is_success ? : 10 1 11
173 status => $resp->code,
174 status_line => $resp->status_line,
175 final_url => ( $resp->request ? $resp->request->uri.'' : $url ),
176 content_type => $ct,
177 charset => $charset,
178 body => $body,
179 error => $resp->is_success ? undef : $resp->status_line,
180 });
181 11 Future->done;
182 })->else(sub {
183 1 ▶ my ( $err ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
184 1 $result->fetched({
185 ok => 0,
186 status => undef,
187 status_line => undef,
188 final_url => $url,
189 content_type => undef,
190 charset => undef,
191 body => undef,
192 error => "$err",
193 });
194 1 Future->done;
195 12 });
196 }
197  
198 sub _fetch_results {
199 6 ▶ my ( $self, $results_ref, %args ) = @_;
Subroutine _fetch_results: called CC=10 SCAR=25.6
200 6 ▶ my $n = $args{fetch} or return Future->done($results_ref);
Branch: 1 branch
BranchTrueFalseTotal
my $n = $args{"fetch"} or return "Future"->done($results_ref) 0 6 6
201 6 ▶ return Future->done($results_ref) unless $results_ref && @$results_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $results_ref and @$results_ref 0 6 6
Condition33%1/3
$results_ref and @$results_ref
!ll&&!rl&&r
006
MC/DC0%0/2
$results_ref and @$results_ref
$results_ref @$results_ref
Truth table33%1/3
A and B
A= $results_refB= @$results_ref
ABresult
0X0
100
111
202 6 ▶ my $cap = $n < @$results_ref ? $n : scalar @$results_ref;
Branch: 1 branch
BranchTrueFalseTotal
$n < @$results_ref ? : 5 1 6
203 6 my @targets = @{$results_ref}[ 0 .. $cap - 1 ];
204 6 ▶ my $conc = $args{fetch_concurrency} // $self->{fetch_concurrency} // 100;
Condition40%2/5
($args{"fetch_concurrency"} // $self->{"fetch_concurrency"}) // 100
l!l
60
$args{"fetch_concurrency"} // $self->{"fetch_concurrency"}
l!l&&r!l&&!r
060
MC/DC0%0/2
($args{"fetch_concurrency"} // $self->{"fetch_concurrency"}) // 100
$args{"fetch_concurrency"} $self->{"fetch_concurrency"}
Truth table33%1/3
A // B
A= $args{"fetch_concurrency"}B= $self->{"fetch_concurrency"}
ABresult
1X1
011
000
205 my %fargs = (
206 timeout => $args{fetch_timeout} // $self->{fetch_timeout},
207 max_bytes => $args{fetch_max_bytes} // $self->{fetch_max_bytes},
208 user_agent => $args{fetch_user_agent} // $self->{fetch_user_agent},
209 accept => $args{fetch_accept},
210 6 ▶ );
Condition55%5/9
$args{"fetch_timeout"} // $self->{"fetch_timeout"}
l!l&&r!l&&!r
006
$args{"fetch_max_bytes"} // $self->{"fetch_max_bytes"}
l!l&&r!l&&!r
105
$args{"fetch_user_agent"} // $self->{"fetch_user_agent"}
l!l&&r!l&&!r
105
MC/DC0%0/2
$args{"fetch_timeout"} // $self->{"fetch_timeout"}
$args{"fetch_timeout"} $self->{"fetch_timeout"}
MC/DC50%1/2
$args{"fetch_max_bytes"} // $self->{"fetch_max_bytes"}
$args{"fetch_max_bytes"} $self->{"fetch_max_bytes"}
MC/DC50%1/2
$args{"fetch_user_agent"} // $self->{"fetch_user_agent"}
$args{"fetch_user_agent"} $self->{"fetch_user_agent"}
Truth table33%1/3
A // B
A= $args{"fetch_timeout"}B= $self->{"fetch_timeout"}
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"fetch_max_bytes"}B= $self->{"fetch_max_bytes"}
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"fetch_user_agent"}B= $self->{"fetch_user_agent"}
ABresult
1X1
011
000
211 6 my $on_fetch = $args{on_fetch};
212 return fmap_void(
213 sub {
214 10 ▶ my $r = shift;
Subroutine __ANON__: called CC=1 SCAR=0.1
215 $self->_fetch_one($r, %fargs)->on_done(sub {
216 10 ▶ $on_fetch->($r) if $on_fetch;
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $on_fetch 0 10 10
217 10 });
218 },
219 foreach => \@targets,
220 concurrent => $conc,
221 6 ▶ )->then(sub { Future->done($results_ref) });
Subroutine __ANON__: called CC=1 SCAR=0.0
222 }
223  
224 #----------------------------------------------------------------------
225 # Core dispatch
226 #----------------------------------------------------------------------
227  
228 sub _dispatch {
229 30 ▶ my ( $self, %args ) = @_;
Subroutine _dispatch: called CC=9 SCAR=22.6
230 30 my $query = $args{query};
231 30 ▶ croak "search requires 'query'" unless defined $query && length $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query and length $query 0 30 30
Condition33%1/3
defined $query and length $query
!ll&&!rl&&r
0030
MC/DC0%0/2
defined $query and length $query
defined $query length $query
Truth table33%1/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
232  
233 30 my @providers = $self->_select_providers( only => $args{only}, exclude => $args{exclude} );
234 30 ▶ return ( [], [] ) unless @providers;
Branch: 1 branch
BranchTrueFalseTotal
unless @providers 0 30 30
235  
236 my %base = (
237 limit => $args{per_provider_limit} // $self->per_provider_limit,
238 language => $args{language},
239 region => $args{region},
240 safesearch => $args{safesearch},
241 30 ▶ );
Condition33%1/3
$args{"per_provider_limit"} // $self->per_provider_limit
l!l&&r!l&&!r
0300
MC/DC0%0/2
$args{"per_provider_limit"} // $self->per_provider_limit
$args{"per_provider_limit"} $self->per_provider_limit
Truth table33%1/3
A // B
A= $args{"per_provider_limit"}B= $self->per_provider_limit
ABresult
1X1
011
000
242  
243 30 ▶ my $popts = $args{provider_opts} || {};
Condition100%2/2
$args{'provider_opts'} || {}
l!l
129
MC/DC100%1/1
$args{'provider_opts'} || {}
$args{'provider_opts'}
Truth table100%2/2
A
A= $args{'provider_opts'}
Aresult
11
00
244 30 my @futures;
245 30 for my $p (@providers) {
246 41 my %merged = %base;
247 # Apply provider_opts in insertion order: class-leaf / tag first,
248 # exact name last so it wins.
249 41 for my $sel ( sort { ($a eq $p->name) <=> ($b eq $p->name) } keys %$popts ) {
250 6 ▶ next unless $p->matches($sel);
Branch: 1 branch
BranchTrueFalseTotal
unless $p->matches($sel) 2 4 6
251 4 %merged = ( %merged, %{ $popts->{$sel} } );
252 }
253 41 push @futures, {
254 provider => $p,
255 future => $p->search( $self->http, $query, \%merged ),
256 };
257 }
258 30 return ( \@providers, \@futures );
259 }
260  
261 #----------------------------------------------------------------------
262 # Mode: collect
263 #----------------------------------------------------------------------
264  
265 sub search {
266 29 ▶ my ( $self, %args ) = @_;
Subroutine search: called CC=7 SCAR=19.6
Pod documented
267 29 ▶ $args{mode} ||= 'collect';
Condition100%2/2
$args{"mode"} ||= "collect"
l!l
326
MC/DC100%1/1
$args{"mode"} ||= "collect"
$args{"mode"}
Truth table100%2/2
A
A= $args{"mode"}
Aresult
11
00
268 29 ▶ return $self->search_stream(%args) if $args{mode} eq 'stream';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"mode"} eq "stream" 1 28 29
269 28 ▶ return $self->search_race(%args) if $args{mode} eq 'race';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"mode"} eq "race" 2 26 28
270  
271 26 my ( $provs, $futs ) = $self->_dispatch(%args);
272 26 ▶ return Future->done({ results => [], errors => [], stats => { providers => 0 } })
Branch: 1 branch
BranchTrueFalseTotal
unless $futs and @$futs 0 26 26
Condition33%1/3
$futs and @$futs
!ll&&!rl&&r
0026
MC/DC0%0/2
$futs and @$futs
$futs @$futs
Truth table33%1/3
A and B
A= $futsB= @$futs
ABresult
0X0
100
111
273 unless $futs && @$futs;
274  
275 26 ▶ my $limit = $args{limit} // $self->default_limit;
Condition66%2/3
$args{"limit"} // $self->default_limit
l!l&&r!l&&!r
3230
MC/DC0%0/2
$args{"limit"} // $self->default_limit
$args{"limit"} $self->default_limit
Truth table66%2/3
A // B
A= $args{"limit"}B= $self->default_limit
ABresult
1X1
011
000
276 26 my $k = $self->rrf_k;
277  
278 my @wrapped = map {
279 26 my $name = $_->{provider}->name;
280 $_->{future}->else(sub {
281 3 ▶ my @err = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
282 3 Future->done({ __error => 1, provider => $name, error => $err[0] });
283 35 });
284 } @$futs;
285  
286 return Future->needs_all(@wrapped)->then(sub {
287 26 ▶ my @per_provider = @_;
Subroutine __ANON__: called CC=12 SCAR=25.1
288 26 my @errors;
289 my %agg; # normalized_url => { result => $first, score => $n, providers => {p=>rank,...} }
290 26 for my $payload (@per_provider) {
291 35 ▶ if ( ref $payload eq 'HASH' && $payload->{__error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $payload eq "HASH" and $payload->{"__error"}) 3 32 35
Condition66%2/3
ref $payload eq "HASH" and $payload->{"__error"}
!ll&&!rl&&r
3203
MC/DC50%1/2
ref $payload eq "HASH" and $payload->{"__error"}
ref $payload eq "HASH" $payload->{"__error"}
Truth table66%2/3
A and B
A= ref $payload eq "HASH"B= $payload->{"__error"}
ABresult
0X0
100
111
292 3 push @errors, { provider => $payload->{provider}, error => $payload->{error} };
293 3 next;
294 }
295 32 ▶ for my $r ( @{ $payload || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$payload or [] 0 32 32
296 70 my $key = $self->_normalize_url( $r->url );
297 70 ▶ next unless length $key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $key 0 70 70
298 70 ▶ my $slot = $agg{$key} ||= { result => $r, score => 0, providers => {} };
Condition100%2/2
$agg{$key} ||= {"result", $r, "score", 0, "providers", {}}
l!l
763
MC/DC100%1/1
$agg{$key} ||= {"result", $r, "score", 0, "providers", {}}
$agg{$key}
Truth table100%2/2
A
A= $agg{$key}
Aresult
11
00
299 70 $slot->{providers}{ $r->provider } = $r->rank;
300 70 $slot->{score} += 1 / ( $k + $r->rank );
301 # Prefer a result that has a snippet, if the first had none.
302 70 ▶ if ( !$slot->{result}->snippet && $r->snippet ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $slot->{"result"}->snippet and $r->snippet) 0 70 70
Condition66%2/3
not $slot->{"result"}->snippet and $r->snippet
!ll&&!rl&&r
60100
MC/DC0%0/2
not $slot->{"result"}->snippet and $r->snippet
not $slot->{"result"}->snippet $r->snippet
Truth table66%2/3
not(A) and B
A= not $slot->{"result"}->snippetB= $r->snippet
ABresult
0X0
100
111
303 0 $slot->{result} = $r;
304 }
305 }
306 }
307 my @merged =
308 map {
309 63 my $s = $_;
310 63 $s->{result}->score( $s->{score} );
311 63 $s->{result}->extra->{providers} = { %{ $s->{providers} } };
312 63 $s->{result};
313 }
314 26 sort { $b->{score} <=> $a->{score} }
315 values %agg;
316  
317 26 ▶ @merged = splice @merged, 0, $limit if @merged > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if @merged > $limit 1 25 26
318  
319 26 my $final = {
320 results => \@merged,
321 errors => \@errors,
322 stats => {
323 providers => scalar @$provs,
324 providers_ok => ( scalar @$provs ) - scalar @errors,
325 providers_error => scalar @errors,
326 merged => scalar @merged,
327 },
328 };
329  
330 26 ▶ if ( $args{fetch} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"fetch"}) 5 21 26
331 return $self->_fetch_results(\@merged, %args)->then(sub {
332 5 ▶ $final->{stats}{fetched} = scalar grep { $_->fetched } @merged;
Subroutine __ANON__: called CC=1 SCAR=0.0
333 5 Future->done($final);
334 5 });
335 }
336 21 return Future->done($final);
337 26 });
338 }
339  
340 #----------------------------------------------------------------------
341 # Mode: stream — fire on_result per result as soon as each provider arrives
342 #----------------------------------------------------------------------
343  
344 sub search_stream {
345 2 ▶ my ( $self, %args ) = @_;
Subroutine search_stream: called CC=9 SCAR=23.3
Pod documented
346 2 ▶ my $cb = $args{on_result} or croak "stream mode requires 'on_result' coderef";
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $args{"on_result"} or croak("stream mode requires 'on_result' coderef") 0 2 2
347 2 my $on_provider_done = $args{on_provider_done};
348 2 my $on_provider_error = $args{on_provider_error};
349 2 my $on_fetch = $args{on_fetch};
350  
351 2 my ( $provs, $futs ) = $self->_dispatch(%args);
352 2 ▶ return Future->done({ results => [], errors => [], stats => { providers => 0 } })
Branch: 1 branch
BranchTrueFalseTotal
unless $futs and @$futs 0 2 2
Condition33%1/3
$futs and @$futs
!ll&&!rl&&r
002
MC/DC0%0/2
$futs and @$futs
$futs @$futs
Truth table33%1/3
A and B
A= $futsB= @$futs
ABresult
0X0
100
111
353 unless $futs && @$futs;
354  
355 2 my @all;
356 my @errors;
357 2 my $seen_key = {};
358  
359 2 ▶ my $fetch_cap = $args{fetch} // 0;
Condition100%2/2
$args{"fetch"} // 0
l!l
11
MC/DC100%1/1
$args{"fetch"} // 0
$args{"fetch"}
Truth table100%2/2
A
A= $args{"fetch"}
Aresult
11
00
360 2 my $fetch_started = 0;
361 2 my @fetch_futures;
362 my %fargs = (
363 timeout => $args{fetch_timeout} // $self->{fetch_timeout},
364 max_bytes => $args{fetch_max_bytes} // $self->{fetch_max_bytes},
365 user_agent => $args{fetch_user_agent} // $self->{fetch_user_agent},
366 accept => $args{fetch_accept},
367 2 ▶ );
Condition33%3/9
$args{"fetch_timeout"} // $self->{"fetch_timeout"}
l!l&&r!l&&!r
002
$args{"fetch_max_bytes"} // $self->{"fetch_max_bytes"}
l!l&&r!l&&!r
002
$args{"fetch_user_agent"} // $self->{"fetch_user_agent"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$args{"fetch_timeout"} // $self->{"fetch_timeout"}
$args{"fetch_timeout"} $self->{"fetch_timeout"}
MC/DC0%0/2
$args{"fetch_max_bytes"} // $self->{"fetch_max_bytes"}
$args{"fetch_max_bytes"} $self->{"fetch_max_bytes"}
MC/DC0%0/2
$args{"fetch_user_agent"} // $self->{"fetch_user_agent"}
$args{"fetch_user_agent"} $self->{"fetch_user_agent"}
Truth table33%1/3
A // B
A= $args{"fetch_timeout"}B= $self->{"fetch_timeout"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args{"fetch_max_bytes"}B= $self->{"fetch_max_bytes"}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $args{"fetch_user_agent"}B= $self->{"fetch_user_agent"}
ABresult
1X1
011
000
368  
369 2 my @wrapped;
370 2 for my $entry (@$futs) {
371 3 my $name = $entry->{provider}->name;
372 my $f = $entry->{future}->then(sub {
373 3 ▶ my ( $results ) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.6
374 3 for my $r (@$results) {
375 9 my $key = $self->_normalize_url( $r->url );
376 9 ▶ next unless length $key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $key 0 9 9
377 9 ▶ next if $seen_key->{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_key->{$key}++ 2 7 9
378 7 push @all, $r;
379 7 $cb->($r);
380 7 ▶ if ( $fetch_cap && $fetch_started < $fetch_cap ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fetch_cap and $fetch_started < $fetch_cap) 2 5 7
Condition100%3/3
$fetch_cap and $fetch_started < $fetch_cap
!ll&&!rl&&r
412
MC/DC100%2/2
$fetch_cap and $fetch_started < $fetch_cap
$fetch_cap $fetch_started < $fetch_cap
Truth table100%3/3
A and B
A= $fetch_capB= $fetch_started < $fetch_cap
ABresult
0X0
100
111
381 2 $fetch_started++;
382 push @fetch_futures, $self->_fetch_one($r, %fargs)->on_done(sub {
383 2 ▶ $on_fetch->($r) if $on_fetch;
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $on_fetch 2 0 2
384 2 });
385 }
386 }
387 3 ▶ $on_provider_done->( $name, $results ) if $on_provider_done;
Branch: 1 branch
BranchTrueFalseTotal
if $on_provider_done 0 3 3
388 3 Future->done;
389 })->else(sub {
390 0 ▶ my ( $err ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
391 0 push @errors, { provider => $name, error => $err };
392 0 ▶ $on_provider_error->( $name, $err ) if $on_provider_error;
Branch: 1 branch
BranchTrueFalseTotal
if $on_provider_error 0 0 0
393 0 Future->done;
394 3 });
395 3 push @wrapped, $f;
396 }
397  
398 return Future->needs_all(@wrapped)->then(sub {
399 # Wait for any pending fetches the stream kicked off.
400 return @fetch_futures
401 2 ▶ ? Future->needs_all(@fetch_futures)
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@fetch_futures ? : 1 1 2
402 : Future->done;
403 })->then(sub {
404 Future->done({
405 results => \@all,
406 errors => \@errors,
407 stats => {
408 providers => scalar @$provs,
409 providers_ok => ( scalar @$provs ) - scalar @errors,
410 providers_error => scalar @errors,
411 emitted => scalar @all,
412 2 ▶ ( $fetch_cap ? ( fetched => scalar grep { $_->fetched } @all ) : () ),
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$fetch_cap ? : 1 1 2
413 },
414 });
415 2 });
416 }
417  
418 #----------------------------------------------------------------------
419 # Mode: race — resolve with whichever provider comes back first (non-error)
420 #----------------------------------------------------------------------
421  
422 sub search_race {
423 2 ▶ my ( $self, %args ) = @_;
Subroutine search_race: called CC=5 SCAR=16.6
Pod documented
424 2 my ( $provs, $futs ) = $self->_dispatch(%args);
425 2 ▶ return Future->done({ results => [], errors => [], stats => { providers => 0 } })
Branch: 1 branch
BranchTrueFalseTotal
unless $futs and @$futs 0 2 2
Condition33%1/3
$futs and @$futs
!ll&&!rl&&r
002
MC/DC0%0/2
$futs and @$futs
$futs @$futs
Truth table33%1/3
A and B
A= $futsB= @$futs
ABresult
0X0
100
111
426 unless $futs && @$futs;
427  
428 2 ▶ my $limit = $args{limit} // $self->default_limit;
Condition33%1/3
$args{"limit"} // $self->default_limit
l!l&&r!l&&!r
020
MC/DC0%0/2
$args{"limit"} // $self->default_limit
$args{"limit"} $self->default_limit
Truth table33%1/3
A // B
A= $args{"limit"}B= $self->default_limit
ABresult
1X1
011
000
429  
430 # Wrap each so we can distinguish "first success" from "first completion".
431 2 my $winner = Future->new;
432 2 my @errors;
433 my @remaining = map {
434 2 my $name = $_->{provider}->name;
435 3 my $f = $_->{future};
436 $f->on_done(sub {
437 3 ▶ my ( $results ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
438 3 ▶ return if $winner->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $winner->is_ready 1 2 3
439 2 my @top = @$results;
440 2 ▶ @top = splice @top, 0, $limit if @top > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if @top > $limit 0 2 2
441 2 $winner->done({
442 provider => $name,
443 results => \@top,
444 errors => \@errors,
445 stats => {
446 providers => scalar @$provs,
447 winning => $name,
448 },
449 });
450 3 });
451 $f->on_fail(sub {
452 0 ▶ my ( $err ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
453 0 push @errors, { provider => $name, error => $err };
454 3 });
455 3 $f;
456 } @$futs;
457  
458 # If everyone fails, resolve with errors.
459 Future->wait_all(@remaining)->on_done(sub {
460 2 ▶ return if $winner->is_ready;
Subroutine __ANON__: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
if $winner->is_ready 2 0 2
461 0 $winner->done({
462 provider => undef,
463 results => [],
464 errors => \@errors,
465 stats => {
466 providers => scalar @$provs,
467 providers_error => scalar @errors,
468 },
469 });
470 2 });
471  
472 2 ▶ return $winner unless $args{fetch};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"fetch"} 1 1 2
473 return $winner->then(sub {
474 1 ▶ my ( $out ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
475 1 ▶ return Future->done($out) unless @{ $out->{results} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$out->{"results"};} 0 1 1
476 $self->_fetch_results( $out->{results}, %args )->then(sub {
477 1 ▶ $out->{stats}{fetched} = scalar grep { $_->fetched } @{ $out->{results} };
Subroutine __ANON__: called CC=1 SCAR=0.0
478 1 Future->done($out);
479 1 });
480 1 });
481 }
482  
483 6 1;
484  
485 __END__
« WebSearch/Provider/Reddit/OAuth.pm ↑ Summary WebSearch/Provider.pm »