WebSearch/Provider/Yandex.pm

filter: Statementstmt 36.2% 29 / 80 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 56.2% 9 / 16 Podpod 100% 5 / 5 totaltotal 27.5% 43 / 156 CC 23 SCAR 41.0
CC
23
Cov
23%
__ANON__
132.0
search
72.0
_init
42.0
CRAP 60.4
« WebSearch/Provider/Google.pm ↑ Summary WebSearch/Provider/Brave.pm »
1 package Net::Async::WebSearch::Provider::Yandex;
2 1 our $VERSION = '0.002';
3 # ABSTRACT: Yandex Search API (XML) 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 URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Request::Common qw( GET );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use XML::LibXML ();
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=6 SCAR=37.4
17 0 ▶ croak "Yandex provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 0 0 0
18 croak "Yandex provider requires 'folderid' (Yandex Cloud folder)"
19 0 ▶ unless $self->{folderid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"folderid"} 0 0 0
20 0 ▶ $self->{endpoint} ||= 'https://yandex.com/search/xml';
Condition0%0/2
$self->{"endpoint"} ||= "https://yandex.com/search/xml"
l!l
00
MC/DC0%0/1
$self->{"endpoint"} ||= "https://yandex.com/search/xml"
$self->{"endpoint"}
Truth table0%0/2
A
A= $self->{"endpoint"}
Aresult
11
00
21 0 ▶ $self->{name} ||= 'yandex';
Condition0%0/2
$self->{"name"} ||= "yandex"
l!l
00
MC/DC0%0/1
$self->{"name"} ||= "yandex"
$self->{"name"}
Truth table0%0/2
A
A= $self->{"name"}
Aresult
11
00
22 0 ▶ $self->{l10n} ||= 'en'; # en | ru | tr | be | kk | uk
Condition0%0/2
$self->{"l10n"} ||= "en"
l!l
00
MC/DC0%0/1
$self->{"l10n"} ||= "en"
$self->{"l10n"}
Truth table0%0/2
A
A= $self->{"l10n"}
Aresult
11
00
23 }
24  
25 0 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: not called CC=1 SCAR=6.9
Pod documented
26 0 ▶sub api_key { $_[0]->{api_key} }
Subroutine api_key: not called CC=1 SCAR=6.9
Pod documented
27 0 ▶sub folderid { $_[0]->{folderid} }
Subroutine folderid: not called CC=1 SCAR=6.9
Pod documented
28 0 ▶sub l10n { $_[0]->{l10n} }
Subroutine l10n: not called CC=1 SCAR=6.9
Pod documented
29  
30 sub search {
31 0 ▶ my ( $self, $http, $query, $opts ) = @_;
Subroutine search: not called CC=8 SCAR=42.8
Pod documented
32 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
33 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
34  
35 0 my $uri = URI->new( $self->endpoint );
36 my %q = (
37 folderid => $self->folderid,
38 apikey => $self->api_key,
39 query => $query,
40 l10n => $opts->{l10n} // $self->l10n,
41 sortby => $opts->{sortby} // 'rlv',
42 groupby => $opts->{groupby}
43 0 ▶ // sprintf('attr=d.mode=deep.groups-on-page=%d.docs-in-group=1', $limit),
Condition0%0/8
$opts->{"l10n"} // $self->l10n
l!l&&r!l&&!r
000
$opts->{"sortby"} // "rlv"
l!l
00
$opts->{"groupby"} // sprintf("attr=d.mode=deep.groups-on-page=%d.docs-in-group=1", $limit)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"l10n"} // $self->l10n
$opts->{"l10n"} $self->l10n
MC/DC0%0/1
$opts->{"sortby"} // "rlv"
$opts->{"sortby"}
MC/DC0%0/2
$opts->{"groupby"} // sprintf("attr=d.mode=deep.groups-on-page=%d.docs-in-group=1", $limit)
$opts->{"groupby"} sprintf("attr=d.mode=deep.groups-on-page=%d.docs-in-group=1", $limit)
Truth table0%0/3
A // B
A= $opts->{"l10n"}B= $self->l10n
ABresult
1X1
011
000
Truth table0%0/2
A
A= $opts->{"sortby"}
Aresult
11
00
Truth table0%0/3
A // B
A= $opts->{"groupby"}B= sprintf("attr=d.mode=deep.groups-on-page=%d.docs-in-group=1", $limit)
ABresult
1X1
011
000
44 );
45 0 ▶ $q{filter} = $opts->{safesearch} if defined $opts->{safesearch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"safesearch"} 0 0 0
46 0 ▶ $q{lr} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 0 0 0
47 0 $uri->query_form(%q);
48  
49 0 my $req = GET( $uri->as_string );
50 0 $req->header( 'User-Agent' => $self->user_agent_string );
51 0 $req->header( 'Accept' => 'application/xml' );
52  
53 return $http->do_request( request => $req )->then(sub {
54 0 ▶ my ( $resp ) = @_;
Subroutine __ANON__: not called CC=11 SCAR=48.8
55 0 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
56 0 return Future->fail(
57 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
58 );
59 }
60 0 my $doc = eval { XML::LibXML->new->parse_string( $resp->decoded_content ) };
61 0 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
62 0 return Future->fail( $self->name.": XML parse: $e", 'websearch', $self->name );
63 }
64 # Yandex signals API-level errors inside the XML envelope.
65 0 ▶ if ( my ($err) = $doc->findnodes('/yandexsearch/response/error') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($err) = $doc->findnodes("/yandexsearch/response/error")) 0 0 0
66 0 ▶ my $code = $err->getAttribute('code') // '';
Condition0%0/2
$err->getAttribute("code") // ""
l!l
00
MC/DC0%0/1
$err->getAttribute("code") // ""
$err->getAttribute("code")
Truth table0%0/2
A
A= $err->getAttribute("code")
Aresult
11
00
67 0 my $msg = $err->textContent;
68 0 return Future->fail(
69 $self->name.": API error $code: $msg", 'websearch', $self->name,
70 );
71 }
72 0 my @out;
73 0 my $rank = 0;
74 0 for my $doc_node ( $doc->findnodes('/yandexsearch/response/results/grouping/group/doc') ) {
75 0 my ($url_node) = $doc_node->findnodes('./url');
76 0 my ($title_node) = $doc_node->findnodes('./title');
77 0 my @passages = $doc_node->findnodes('./passages/passage');
78 0 my @headline = $doc_node->findnodes('./headline');
79 0 ▶ my $url = $url_node ? $url_node->textContent : next;
Branch: 1 branch
BranchTrueFalseTotal
$url_node ? : 0 0 0
80 0 ▶ my $title = $title_node ? $title_node->textContent : '';
Branch: 1 branch
BranchTrueFalseTotal
$title_node ? : 0 0 0
81 0 my $snippet;
82 0 ▶ if (@passages) {
Branch: 2 branches
BranchTrueFalseTotal
if (@passages) { } 0 0 0
elsif (@headline) { } 0 0 0
83 0 $snippet = join ' … ', map { $_->textContent } @passages;
84 } elsif (@headline) {
85 0 $snippet = $headline[0]->textContent;
86 }
87 0 $rank++;
88 0 push @out, Net::Async::WebSearch::Result->new(
89 url => $url,
90 title => $title,
91 snippet => $snippet,
92 provider => $self->name,
93 rank => $rank,
94 );
95 0 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 0 0
96 }
97 0 return Future->done(\@out);
98 0 });
99 }
100  
101 1 1;
102  
103 __END__
« WebSearch/Provider/Google.pm ↑ Summary WebSearch/Provider/Brave.pm »