WebSearch/Provider/Serper.pm

filter: Statementstmt 46.4% 26 / 56 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 61.5% 8 / 13 Podpod 100% 3 / 3 totaltotal 33.9% 37 / 109 CC 17 SCAR 36.8
CC
17
Cov
30%
__ANON__
90.0
search
42.0
_init
20.0
CRAP 39.8
« WebSearch/Provider/SearxNG.pm ↑ Summary WebSearch/Provider/Reddit.pm »
1 package Net::Async::WebSearch::Provider::Serper;
2 1 our $VERSION = '0.002';
3 # ABSTRACT: Serper.dev Google Search API 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( encode_json decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Net::Async::WebSearch::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub _init {
15 0 ▶ my ( $self ) = @_;
Subroutine _init: not called CC=4 SCAR=30.0
16 0 ▶ croak "Serper provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 0 0 0
17 0 ▶ $self->{endpoint} ||= 'https://google.serper.dev/search';
Condition0%0/2
$self->{"endpoint"} ||= "https://google.serper.dev/search"
l!l
00
MC/DC0%0/1
$self->{"endpoint"} ||= "https://google.serper.dev/search"
$self->{"endpoint"}
Truth table0%0/2
A
A= $self->{"endpoint"}
Aresult
11
00
18 0 ▶ $self->{name} ||= 'serper';
Condition0%0/2
$self->{"name"} ||= "serper"
l!l
00
MC/DC0%0/1
$self->{"name"} ||= "serper"
$self->{"name"}
Truth table0%0/2
A
A= $self->{"name"}
Aresult
11
00
19 }
20  
21 0 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: not called CC=1 SCAR=6.9
Pod documented
22 0 ▶sub api_key { $_[0]->{api_key} }
Subroutine api_key: not called CC=1 SCAR=6.9
Pod documented
23  
24 sub search {
25 0 ▶ my ( $self, $http, $query, $opts ) = @_;
Subroutine search: not called CC=6 SCAR=37.4
Pod documented
26 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
27 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
28  
29 0 my %body = ( q => $query, num => $limit );
30 0 ▶ $body{gl} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 0 0 0
31 0 ▶ $body{hl} = $opts->{language} if defined $opts->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"language"} 0 0 0
32 0 ▶ $body{tbs} = $opts->{tbs} if defined $opts->{tbs};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"tbs"} 0 0 0
33  
34 0 my $req = HTTP::Request->new(
35 POST => $self->endpoint,
36 [
37 'X-API-KEY' => $self->api_key,
38 'Content-Type' => 'application/json',
39 'Accept' => 'application/json',
40 'User-Agent' => $self->user_agent_string,
41 ],
42 encode_json(\%body),
43 );
44  
45 return $http->do_request( request => $req )->then(sub {
46 0 ▶ my ( $resp ) = @_;
Subroutine __ANON__: not called CC=9 SCAR=45.0
47 0 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
48 0 return Future->fail(
49 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
50 );
51 }
52 0 my $data = eval { decode_json( $resp->decoded_content ) };
53 0 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
54 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
55 }
56 0 my @out;
57 0 my $rank = 0;
58 0 ▶ for my $r ( @{ $data->{organic} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$data->{"organic"} or [] 0 0 0
59 0 $rank++;
60 push @out, Net::Async::WebSearch::Result->new(
61 url => $r->{link},
62 title => $r->{title},
63 snippet => $r->{snippet},
64 provider => $self->name,
65 rank => $r->{position} // $rank,
66 published_at => $r->{date},
67 raw => $r,
68 extra => {
69 ( defined $r->{sitelinks} ? ( sitelinks => $r->{sitelinks} ) : () ),
70 0 ▶ ( defined $r->{imageUrl} ? ( imageUrl => $r->{imageUrl} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
defined $r->{'sitelinks'} ? : 0 0 0
defined $r->{'imageUrl'} ? : 0 0 0
Condition0%0/3
$r->{"position"} // $rank
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{"position"} // $rank
$r->{"position"} $rank
Truth table0%0/3
A // B
A= $r->{"position"}B= $rank
ABresult
1X1
011
000
71 },
72 );
73 0 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 0 0
74 }
75 0 return Future->done(\@out);
76 0 });
77 }
78  
79 1 1;
80  
81 __END__
« WebSearch/Provider/SearxNG.pm ↑ Summary WebSearch/Provider/Reddit.pm »