WebSearch/Provider/Mojeek.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 81.2% 13 / 16 Conditioncond 58.3% 7 / 12 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 87.9% 102 / 116 CC 16 SCAR 15.2
CC
16
Cov
90%
__ANON__
8.6
search
6.1
_init
4.1
CRAP 4.6
« WebSearch/Provider/Exa.pm ↑ Summary WebSearch/Provider/DuckDuckGo.pm »
1 package Net::Async::WebSearch::Provider::Mojeek;
2 2 our $VERSION = '0.003';
3 # ABSTRACT: Mojeek 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 6 ▶sub _init {
Subroutine _init: called CC=4 SCAR=14.2
16 6 my ( $self ) = @_;
17 # Mojeek runs its own index (not a Google reseller) and has no public/shared
18 # key, so — like Brave — an api_key is mandatory.
19 6 ▶ croak "Mojeek provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 1 5 6
20 5 ▶ $self->{endpoint} ||= 'https://api.mojeek.com/search';
Condition50%1/2
$self->{"endpoint"} ||= "https://api.mojeek.com/search"
l!l
05
MC/DC0%0/1
$self->{"endpoint"} ||= "https://api.mojeek.com/search"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
Aresult
11
00
21 5 ▶ $self->{name} ||= 'mojeek';
Condition50%1/2
$self->{"name"} ||= "mojeek"
l!l
05
MC/DC0%0/1
$self->{"name"} ||= "mojeek"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
Aresult
11
00
22 }
23  
24 5 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
25 6 ▶sub api_key { $_[0]->{api_key} }
Subroutine api_key: called CC=1 SCAR=0.0
Pod documented
26  
27 5 ▶sub search {
Subroutine search: called CC=6 SCAR=18.1
Pod documented
28 5 my ( $self, $http, $query, $opts ) = @_;
29 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
30 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
31  
32 # Two Mojeek quirks live in this request: the key travels as the `api_key`
33 # *query parameter* (not a header, unlike Brave), and `fmt=json` is mandatory
34 # — without it Mojeek answers XML, which the JSON decode below cannot parse.
35 5 my $uri = URI->new( $self->endpoint );
36 5 my %q = (
37 q => $query,
38 t => $limit,
39 fmt => 'json',
40 api_key => $self->api_key,
41 );
42 5 ▶ $q{lb} = $opts->{language} if defined $opts->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"language"} 1 4 5
43 5 ▶ $q{rb} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 1 4 5
44 5 ▶ $q{safe} = $opts->{safesearch} if defined $opts->{safesearch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"safesearch"} 1 4 5
45 5 $uri->query_form(%q);
46  
47 5 my $req = GET( $uri->as_string );
48 5 $req->header( 'User-Agent' => $self->user_agent_string );
49 5 $req->header( 'Accept' => 'application/json' );
50  
51 5 ▶ return $http->do_request( request => $req )->then(sub {
Subroutine __ANON__: called CC=8 SCAR=21.5
52 5 my ( $resp ) = @_;
53 5 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 1 4 5
54 1 return Future->fail(
55 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
56 );
57 }
58 4 my $data = eval { decode_json( $resp->decoded_content ) };
59 4 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 4 4
60 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
61 }
62 # Mojeek nests everything under a `response` container: the hit list is
63 # response.results (NOT a top-level `results`), alongside response.head
64 # (query metadata) and response.status (OK/ERROR). Reach through it before
65 # walking the results.
66 4 ▶ my $response = $data->{response} || {};
Condition50%1/2
$data->{'response'} || {}
l!l
40
MC/DC0%0/1
$data->{'response'} || {}
$data->{'response'}
Truth table50%1/2
A
A= $data->{'response'}
Aresult
11
00
67 4 my $status = $response->{status};
68 4 my @out;
69 4 my $rank = 0;
70 4 ▶ for my $r ( @{ $response->{results} || [] } ) {
Condition50%1/2
$response->{'results'} || []
l!l
40
MC/DC0%0/1
$response->{'results'} || []
$response->{'results'}
Truth table50%1/2
A
A= $response->{'results'}
Aresult
11
00
71 8 $rank++;
72 push @out, Net::Async::WebSearch::Result->new(
73 url => $r->{url},
74 title => $r->{title},
75 snippet => $r->{desc},
76 8 ▶ provider => $self->name,
Branch: 1 branch
BranchTrueFalseTotal
defined $status ? : 8 0 8
77 rank => $rank,
78 raw => $r,
79 extra => {
80 ( defined $status ? ( status => $status ) : () ),
81 },
82 );
83 8 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 8 8
84 }
85 4 return Future->done(\@out);
86 5 });
87 }
88  
89 2 1;
90  
91 __END__
« WebSearch/Provider/Exa.pm ↑ Summary WebSearch/Provider/DuckDuckGo.pm »