WebSearch/Provider/Serper.pm

filter: Statementstmt 96.4% 54 / 56 Branchbran 55.5% 10 / 18 Conditioncond 46.1% 6 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 78.1% 86 / 110 CC 17 SCAR 17.7
CC
17
Cov
80%
__ANON__
11.2
search
6.8
_init
4.4
CRAP 5.8
« WebSearch/Provider/Brave.pm ↑ Summary WebSearch/Provider/Tavily.pm »
1 package Net::Async::WebSearch::Provider::Serper;
2 2 our $VERSION = '0.003';
3 # ABSTRACT: Serper.dev Google Search API 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( encode_json decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::Async::WebSearch::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶sub _init {
Subroutine _init: called CC=4 SCAR=14.9
15 1 my ( $self ) = @_;
16 1 ▶ croak "Serper provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 0 1 1
17 1 ▶ $self->{endpoint} ||= 'https://google.serper.dev/search';
Condition50%1/2
$self->{"endpoint"} ||= "https://google.serper.dev/search"
l!l
01
MC/DC0%0/1
$self->{"endpoint"} ||= "https://google.serper.dev/search"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
Aresult
11
00
18 1 ▶ $self->{name} ||= 'serper';
Condition50%1/2
$self->{"name"} ||= "serper"
l!l
01
MC/DC0%0/1
$self->{"name"} ||= "serper"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
Aresult
11
00
19 }
20  
21 1 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
22 1 ▶sub api_key { $_[0]->{api_key} }
Subroutine api_key: called CC=1 SCAR=0.0
Pod documented
23  
24 1 ▶sub search {
Subroutine search: called CC=6 SCAR=19.2
Pod documented
25 1 my ( $self, $http, $query, $opts ) = @_;
26 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
27 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
28  
29 1 my %body = ( q => $query, num => $limit );
30 1 ▶ $body{gl} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 0 1 1
31 1 ▶ $body{hl} = $opts->{language} if defined $opts->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"language"} 0 1 1
32 1 ▶ $body{tbs} = $opts->{tbs} if defined $opts->{tbs};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"tbs"} 0 1 1
33  
34 1 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 1 ▶ return $http->do_request( request => $req )->then(sub {
Subroutine __ANON__: called CC=9 SCAR=24.1
46 1 my ( $resp ) = @_;
47 1 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 1 1
48 0 return Future->fail(
49 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
50 );
51 }
52 1 my $data = eval { decode_json( $resp->decoded_content ) };
53 1 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 1 1
54 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
55 }
56 1 my @out;
57 1 my $rank = 0;
58 1 ▶ for my $r ( @{ $data->{organic} || [] } ) {
Condition50%1/2
$data->{'organic'} || []
l!l
10
MC/DC0%0/1
$data->{'organic'} || []
$data->{'organic'}
Truth table50%1/2
A
A= $data->{'organic'}
Aresult
11
00
59 2 $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 2 ▶ ( defined $r->{imageUrl} ? ( imageUrl => $r->{imageUrl} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
defined $r->{'sitelinks'} ? : 1 1 2
defined $r->{'imageUrl'} ? : 0 2 2
Condition33%1/3
$r->{"position"} // $rank
l!l&&r!l&&!r
200
MC/DC0%0/2
$r->{"position"} // $rank
$r->{"position"} $rank
Truth table33%1/3
A // B
A= $r->{"position"}B= $rank
ABresult
1X1
011
000
71 },
72 );
73 2 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 2 2
74 }
75 1 return Future->done(\@out);
76 1 });
77 }
78  
79 2 1;
80  
81 __END__
« WebSearch/Provider/Brave.pm ↑ Summary WebSearch/Provider/Tavily.pm »