WebSearch/Provider/SearxNG.pm

filter: Statementstmt 43.9% 29 / 66 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 64.2% 9 / 14 Podpod 100% 3 / 3 totaltotal 34.1% 41 / 120 CC 19 SCAR 39.5
CC
19
Cov
29%
search
90.0
__ANON__
90.0
_init
12.0
CRAP 52.2
« WebSearch/Provider/Brave.pm ↑ Summary WebSearch/Provider/Serper.pm »
1 package Net::Async::WebSearch::Provider::SearxNG;
2 1 our $VERSION = '0.002';
3 # ABSTRACT: SearxNG/Searx JSON endpoint provider
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use parent 'Net::Async::WebSearch::Provider';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Request::Common qw( GET );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::Async::WebSearch::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub _init {
16 0 ▶ my ( $self ) = @_;
Subroutine _init: not called CC=3 SCAR=24.8
17 0 ▶ croak "SearxNG provider requires 'endpoint'" unless $self->{endpoint};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"endpoint"} 0 0 0
18 0 $self->{endpoint} =~ s{/+$}{};
19 0 ▶ $self->{name} ||= 'searxng';
Condition0%0/2
$self->{"name"} ||= "searxng"
l!l
00
MC/DC0%0/1
$self->{"name"} ||= "searxng"
$self->{"name"}
Truth table0%0/2
A
A= $self->{"name"}
Aresult
11
00
20 }
21  
22 0 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: not called CC=1 SCAR=6.9
Pod documented
23 0 ▶sub api_key { $_[0]->{api_key} }
Subroutine api_key: not called CC=1 SCAR=6.9
Pod documented
24  
25 sub search {
26 0 ▶ my ( $self, $http, $query, $opts ) = @_;
Subroutine search: not called CC=9 SCAR=45.0
Pod documented
27 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
28 0 ▶ my $limit = $opts->{limit} || 10;
Condition0%0/2
$opts->{'limit'} || 10
l!l
00
MC/DC0%0/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table0%0/2
A
A= $opts->{'limit'}
Aresult
11
00
29  
30 0 my $uri = URI->new( $self->endpoint . '/search' );
31 0 my %q = (
32 q => $query,
33 format => 'json',
34 );
35 0 ▶ $q{language} = $opts->{language} if defined $opts->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"language"} 0 0 0
36 0 ▶ $q{safesearch} = $opts->{safesearch} if defined $opts->{safesearch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"safesearch"} 0 0 0
37 0 ▶ $q{categories} = $opts->{categories} if defined $opts->{categories};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"categories"} 0 0 0
38 0 ▶ $q{engines} = $opts->{engines} if defined $opts->{engines};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"engines"} 0 0 0
39 0 ▶ $q{pageno} = $opts->{pageno} if defined $opts->{pageno};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"pageno"} 0 0 0
40 0 $uri->query_form(%q);
41  
42 0 my $req = GET( $uri->as_string );
43 0 $req->header( 'User-Agent' => $self->user_agent_string );
44 0 $req->header( 'Accept' => 'application/json' );
45 0 ▶ $req->header( 'Authorization' => 'Bearer ' . $self->api_key ) if defined $self->api_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->api_key 0 0 0
46  
47 return $http->do_request( request => $req )->then(sub {
48 0 ▶ my ( $resp ) = @_;
Subroutine __ANON__: not called CC=9 SCAR=45.0
49 0 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
50 0 return Future->fail(
51 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
52 );
53 }
54 0 my $data = eval { decode_json( $resp->decoded_content ) };
55 0 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
56 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
57 }
58 0 my @out;
59 0 my $rank = 0;
60 0 ▶ for my $r ( @{ $data->{results} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$data->{"results"} or [] 0 0 0
61 0 $rank++;
62 push @out, Net::Async::WebSearch::Result->new(
63 url => $r->{url},
64 title => $r->{title},
65 snippet => $r->{content},
66 provider => $self->name,
67 rank => $rank,
68 published_at => $r->{publishedDate},
69 raw => $r,
70 extra => {
71 ( defined $r->{engine} ? ( engine => $r->{engine} ) : () ),
72 ( defined $r->{category} ? ( category => $r->{category} ) : () ),
73 0 ▶ ( defined $r->{thumbnail} ? ( thumbnail => $r->{thumbnail} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
defined $r->{'engine'} ? : 0 0 0
defined $r->{'category'} ? : 0 0 0
defined $r->{'thumbnail'} ? : 0 0 0
74 },
75 );
76 0 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 0 0
77 }
78 0 return Future->done(\@out);
79 0 });
80 }
81  
82 1 1;
83  
84 __END__
« WebSearch/Provider/Brave.pm ↑ Summary WebSearch/Provider/Serper.pm »