WebSearch/Provider/Marginalia.pm

filter: Statementstmt 96.6% 58 / 60 Branchbran 71.4% 10 / 14 Conditioncond 60.0% 6 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 86.6% 91 / 105 CC 14 SCAR 14.2
CC
14
Cov
88%
__ANON__
8.0
search
5.1
_init
4.1
CRAP 4.1
« WebSearch/Provider/DuckDuckGo.pm ↑ Summary WebSearch/Provider.pm »
1 package Net::Async::WebSearch::Provider::Marginalia;
2 2 our $VERSION = '0.003';
3 # ABSTRACT: Marginalia 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 Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Request::Common qw( GET );
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 3 ▶sub _init {
Subroutine _init: called CC=4 SCAR=14.2
15 3 my ( $self ) = @_;
16 # Marginalia ships a public test key, so — unlike the paid providers — an
17 # api_key is not required; fall back to it when none was supplied.
18 3 ▶ $self->{api_key} = 'public' unless defined $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"api_key"} 2 1 3
19 3 ▶ $self->{endpoint} ||= 'https://api2.marginalia-search.com/search';
Condition50%1/2
$self->{"endpoint"} ||= "https://api2.marginalia-search.com/search"
l!l
03
MC/DC0%0/1
$self->{"endpoint"} ||= "https://api2.marginalia-search.com/search"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
Aresult
11
00
20 3 ▶ $self->{name} ||= 'marginalia';
Condition50%1/2
$self->{"name"} ||= "marginalia"
l!l
03
MC/DC0%0/1
$self->{"name"} ||= "marginalia"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
Aresult
11
00
21 }
22  
23 5 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
24 6 ▶sub api_key { $_[0]->{api_key} }
Subroutine api_key: called CC=1 SCAR=0.0
Pod documented
25  
26 5 ▶sub search {
Subroutine search: called CC=5 SCAR=16.4
Pod documented
27 5 my ( $self, $http, $query, $opts ) = @_;
28 5 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
50
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
29 5 ▶ my $limit = $opts->{limit} || 10;
Condition100%2/2
$opts->{'limit'} || 10
l!l
23
MC/DC100%1/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table100%2/2
A
A= $opts->{'limit'}
Aresult
11
00
30  
31 5 my $uri = URI->new( $self->endpoint );
32 5 my %q = ( query => $query, count => $limit );
33 # Marginalia has no language/region knobs — those opts are silently ignored.
34 # safesearch maps to the nsfw filter: safesearch on → nsfw suppressed (0),
35 # safesearch off → nsfw allowed (1).
36 5 ▶ $q{nsfw} = $opts->{safesearch} ? 0 : 1 if defined $opts->{safesearch};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts->{"safesearch"} 2 3 5
$opts->{'safesearch'} ? : 1 1 2
37 5 $uri->query_form(%q);
38  
39 5 my $req = GET( $uri->as_string );
40 5 $req->header( 'User-Agent' => $self->user_agent_string );
41 5 $req->header( 'Accept' => 'application/json' );
42 5 $req->header( 'API-Key' => $self->api_key );
43  
44 5 ▶ return $http->do_request( request => $req )->then(sub {
Subroutine __ANON__: called CC=7 SCAR=20.7
45 5 my ( $resp ) = @_;
46 5 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 5 5
47 0 return Future->fail(
48 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
49 );
50 }
51 5 my $data = eval { decode_json( $resp->decoded_content ) };
52 5 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 5 5
53 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
54 }
55 # The corpus license (CC-BY-NC-SA) is a top-level field; record it per hit.
56 5 my $license = $data->{license};
57 5 my @out;
58 5 my $rank = 0;
59 5 ▶ for my $r ( @{ $data->{results} || [] } ) {
Condition50%1/2
$data->{'results'} || []
l!l
50
MC/DC0%0/1
$data->{'results'} || []
$data->{'results'}
Truth table50%1/2
A
A= $data->{'results'}
Aresult
11
00
60 10 $rank++;
61 push @out, Net::Async::WebSearch::Result->new(
62 url => $r->{url},
63 title => $r->{title},
64 snippet => $r->{description},
65 10 ▶ provider => $self->name,
Branch: 1 branch
BranchTrueFalseTotal
defined $license ? : 10 0 10
66 rank => $rank,
67 raw => $r,
68 extra => {
69 ( defined $license ? ( license => $license ) : () ),
70 },
71 );
72 10 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 10 10
73 }
74 5 return Future->done(\@out);
75 5 });
76 }
77  
78 2 1;
79  
80 __END__
« WebSearch/Provider/DuckDuckGo.pm ↑ Summary WebSearch/Provider.pm »