WebSearch/Provider/SearxNG.pm

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