WebSearch/Provider/Exa.pm

filter: Statementstmt 96.2% 51 / 53 Branchbran 57.1% 8 / 14 Conditioncond 50.0% 5 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 81.6% 80 / 98 CC 14 SCAR 16.4
CC
14
Cov
83%
__ANON__
10.7
_init
4.4
search
3.2
CRAP 5.1
« WebSearch/Provider/Tavily.pm ↑ Summary WebSearch/Provider/Mojeek.pm »
1 package Net::Async::WebSearch::Provider::Exa;
2 2 our $VERSION = '0.003';
3 # ABSTRACT: Exa neural 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 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 "Exa provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 0 1 1
17 1 ▶ $self->{endpoint} ||= 'https://api.exa.ai/search';
Condition50%1/2
$self->{"endpoint"} ||= "https://api.exa.ai/search"
l!l
01
MC/DC0%0/1
$self->{"endpoint"} ||= "https://api.exa.ai/search"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
Aresult
11
00
18 1 ▶ $self->{name} ||= 'exa';
Condition50%1/2
$self->{"name"} ||= "exa"
l!l
01
MC/DC0%0/1
$self->{"name"} ||= "exa"
$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=3 SCAR=11.5
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 # Exa has no native language/region/safesearch parameters, so those opts are
30 # silently ignored. Snippet text only comes back when `contents.text` is
31 # requested — without it every hit has an empty snippet.
32 1 my %body = (
33 query => $query,
34 numResults => $limit,
35 contents => { text => JSON->true },
36 );
37  
38 1 my $req = HTTP::Request->new(
39 POST => $self->endpoint,
40 [
41 'x-api-key' => $self->api_key,
42 'Content-Type' => 'application/json',
43 'Accept' => 'application/json',
44 'User-Agent' => $self->user_agent_string,
45 ],
46 encode_json(\%body),
47 );
48  
49 1 ▶ return $http->do_request( request => $req )->then(sub {
Subroutine __ANON__: called CC=9 SCAR=23.7
50 1 my ( $resp ) = @_;
51 1 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 1 1
52 0 return Future->fail(
53 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
54 );
55 }
56 1 my $data = eval { decode_json( $resp->decoded_content ) };
57 1 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 1 1
58 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
59 }
60 1 my @out;
61 1 my $rank = 0;
62 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
63 2 $rank++;
64 push @out, Net::Async::WebSearch::Result->new(
65 url => $r->{url},
66 title => $r->{title},
67 snippet => $r->{text},
68 provider => $self->name,
69 rank => $rank,
70 published_at => $r->{publishedDate},
71 raw => $r,
72 extra => {
73 ( defined $r->{author} ? ( author => $r->{author} ) : () ),
74 ( defined $r->{id} ? ( id => $r->{id} ) : () ),
75 2 ▶ ( defined $r->{score} ? ( score => $r->{score} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
defined $r->{'author'} ? : 1 1 2
defined $r->{'id'} ? : 2 0 2
defined $r->{'score'} ? : 2 0 2
76 },
77 );
78 2 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 2 2
79 }
80 1 return Future->done(\@out);
81 1 });
82 }
83  
84 2 1;
85  
86 __END__
« WebSearch/Provider/Tavily.pm ↑ Summary WebSearch/Provider/Mojeek.pm »