WebSearch/Provider/Brave.pm

filter: Statementstmt 96.8% 62 / 64 Branchbran 65.0% 13 / 20 Conditioncond 53.3% 8 / 15 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 82.1% 101 / 123 CC 19 SCAR 18.6
CC
19
Cov
84%
__ANON__
12.6
search
6.4
_init
4.4
CRAP 6.4
« WebSearch/Provider/SearxNG.pm ↑ Summary WebSearch/Provider/Serper.pm »
1 package Net::Async::WebSearch::Provider::Brave;
2 2 our $VERSION = '0.003';
3 # ABSTRACT: Brave 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( 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=4 SCAR=14.9
16 1 my ( $self ) = @_;
17 1 ▶ croak "Brave provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 0 1 1
18 1 ▶ $self->{endpoint} ||= 'https://api.search.brave.com/res/v1/web/search';
Condition50%1/2
$self->{"endpoint"} ||= "https://api.search.brave.com/res/v1/web/search"
l!l
01
MC/DC0%0/1
$self->{"endpoint"} ||= "https://api.search.brave.com/res/v1/web/search"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
Aresult
11
00
19 1 ▶ $self->{name} ||= 'brave';
Condition50%1/2
$self->{"name"} ||= "brave"
l!l
01
MC/DC0%0/1
$self->{"name"} ||= "brave"
$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=6 SCAR=18.6
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 );
31 1 my %q = ( q => $query, count => $limit );
32 1 ▶ $q{country} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 0 1 1
33 1 ▶ $q{search_lang}= $opts->{language} if defined $opts->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"language"} 0 1 1
34 1 ▶ $q{safesearch} = $opts->{safesearch} if defined $opts->{safesearch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"safesearch"} 0 1 1
35 1 $uri->query_form(%q);
36  
37 1 my $req = GET( $uri->as_string );
38 1 $req->header( 'User-Agent' => $self->user_agent_string );
39 1 $req->header( 'Accept' => 'application/json' );
40 1 $req->header( 'X-Subscription-Token' => $self->api_key );
41  
42 1 ▶ return $http->do_request( request => $req )->then(sub {
Subroutine __ANON__: called CC=11 SCAR=25.3
43 1 my ( $resp ) = @_;
44 1 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 1 1
45 0 return Future->fail(
46 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
47 );
48 }
49 1 my $data = eval { decode_json( $resp->decoded_content ) };
50 1 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 1 1
51 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
52 }
53 1 my @out;
54 1 my $rank = 0;
55 1 ▶ for my $r ( @{ ($data->{web} || {})->{results} || [] } ) {
Condition50%2/4
+($data->{'web'} || {})->{'results'} || []
l!l
10
$data->{'web'} || {}
l!l
10
MC/DC0%0/1
+($data->{'web'} || {})->{'results'} || []
$data->{'web'}
Truth table50%1/2
A
A= $data->{'web'}
Aresult
11
00
56 2 $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 2 ▶ ( defined $r->{subtype} ? ( subtype => $r->{subtype} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
defined $r->{'age'} ? : 1 1 2
defined $r->{'profile'} ? : 1 1 2
defined $r->{'subtype'} ? : 1 1 2
Condition66%2/3
$r->{"page_age"} // $r->{"age"}
l!l&&r!l&&!r
101
MC/DC50%1/2
$r->{"page_age"} // $r->{"age"}
$r->{"page_age"} $r->{"age"}
Truth table66%2/3
A // B
A= $r->{"page_age"}B= $r->{"age"}
ABresult
1X1
011
000
72 },
73 );
74 2 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 2 2
75 }
76 1 return Future->done(\@out);
77 1 });
78 }
79  
80 2 1;
81  
82 __END__
« WebSearch/Provider/SearxNG.pm ↑ Summary WebSearch/Provider/Serper.pm »