WebSearch/Provider/Brave.pm

filter: Statementstmt 45.3% 29 / 64 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 64.2% 9 / 14 Podpod 100% 3 / 3 totaltotal 33.3% 41 / 123 CC 19 SCAR 40.3
CC
19
Cov
29%
__ANON__
132.0
search
42.0
_init
20.0
CRAP 56.2
« WebSearch/Provider/Yandex.pm ↑ Summary WebSearch/Provider/SearxNG.pm »
1 package Net::Async::WebSearch::Provider::Brave;
2 1 our $VERSION = '0.002';
3 # ABSTRACT: Brave 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( 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=4 SCAR=30.0
17 0 ▶ croak "Brave provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 0 0 0
18 0 ▶ $self->{endpoint} ||= 'https://api.search.brave.com/res/v1/web/search';
Condition0%0/2
$self->{"endpoint"} ||= "https://api.search.brave.com/res/v1/web/search"
l!l
00
MC/DC0%0/1
$self->{"endpoint"} ||= "https://api.search.brave.com/res/v1/web/search"
$self->{"endpoint"}
Truth table0%0/2
A
A= $self->{"endpoint"}
Aresult
11
00
19 0 ▶ $self->{name} ||= 'brave';
Condition0%0/2
$self->{"name"} ||= "brave"
l!l
00
MC/DC0%0/1
$self->{"name"} ||= "brave"
$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=6 SCAR=37.4
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 );
31 0 my %q = ( q => $query, count => $limit );
32 0 ▶ $q{country} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 0 0 0
33 0 ▶ $q{search_lang}= $opts->{language} if defined $opts->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"language"} 0 0 0
34 0 ▶ $q{safesearch} = $opts->{safesearch} if defined $opts->{safesearch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"safesearch"} 0 0 0
35 0 $uri->query_form(%q);
36  
37 0 my $req = GET( $uri->as_string );
38 0 $req->header( 'User-Agent' => $self->user_agent_string );
39 0 $req->header( 'Accept' => 'application/json' );
40 0 $req->header( 'X-Subscription-Token' => $self->api_key );
41  
42 return $http->do_request( request => $req )->then(sub {
43 0 ▶ my ( $resp ) = @_;
Subroutine __ANON__: not called CC=11 SCAR=48.8
44 0 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
45 0 return Future->fail(
46 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
47 );
48 }
49 0 my $data = eval { decode_json( $resp->decoded_content ) };
50 0 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
51 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
52 }
53 0 my @out;
54 0 my $rank = 0;
55 0 ▶ for my $r ( @{ ($data->{web} || {})->{results} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
+($data->{"web"} || {})->{"results"} or [] 0 0 0
Condition0%0/2
$data->{'web'} || {}
l!l
00
MC/DC0%0/1
$data->{'web'} || {}
$data->{'web'}
Truth table0%0/2
A
A= $data->{'web'}
Aresult
11
00
56 0 $rank++;
57 push @out, Net::Async::WebSearch::Result->new(
58 url => $r->{url},
59 title => $r->{title},
60 snippet => $r->{description},
61 provider => $self->name,
62 rank => $rank,
63 # Brave gives both age (human-readable like "3 days ago") and
64 # page_age (ISO 8601). Prefer the ISO one.
65 published_at => $r->{page_age} // $r->{age},
66 language => $r->{language},
67 raw => $r,
68 extra => {
69 ( defined $r->{age} ? ( age => $r->{age} ) : () ),
70 ( defined $r->{profile} ? ( profile => $r->{profile} ) : () ),
71 0 ▶ ( defined $r->{subtype} ? ( subtype => $r->{subtype} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
defined $r->{'age'} ? : 0 0 0
defined $r->{'profile'} ? : 0 0 0
defined $r->{'subtype'} ? : 0 0 0
Condition0%0/3
$r->{"page_age"} // $r->{"age"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{"page_age"} // $r->{"age"}
$r->{"page_age"} $r->{"age"}
Truth table0%0/3
A // B
A= $r->{"page_age"}B= $r->{"age"}
ABresult
1X1
011
000
72 },
73 );
74 0 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 0 0
75 }
76 0 return Future->done(\@out);
77 0 });
78 }
79  
80 1 1;
81  
82 __END__
« WebSearch/Provider/Yandex.pm ↑ Summary WebSearch/Provider/SearxNG.pm »