WebSearch/Provider/Yandex.pm

filter: Statementstmt 93.7% 75 / 80 Branchbran 54.1% 13 / 24 Conditioncond 42.1% 8 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 75.9% 117 / 154 CC 23 SCAR 20.3
CC
23
Cov
78%
__ANON__
14.2
search
9.7
_init
7.1
CRAP 7.6
« WebSearch/Provider/Google.pm ↑ Summary WebSearch.pm »
1 package Net::Async::WebSearch::Provider::Yandex;
2 2 our $VERSION = '0.003';
3 # ABSTRACT: Yandex Search API (XML) 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 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 XML::LibXML ();
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=6 SCAR=19.6
16 1 my ( $self ) = @_;
17 1 ▶ croak "Yandex provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 0 1 1
18 croak "Yandex provider requires 'folderid' (Yandex Cloud folder)"
19 1 ▶ unless $self->{folderid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"folderid"} 0 1 1
20 1 ▶ $self->{endpoint} ||= 'https://yandex.com/search/xml';
Condition50%1/2
$self->{"endpoint"} ||= "https://yandex.com/search/xml"
l!l
01
MC/DC0%0/1
$self->{"endpoint"} ||= "https://yandex.com/search/xml"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
Aresult
11
00
21 1 ▶ $self->{name} ||= 'yandex';
Condition50%1/2
$self->{"name"} ||= "yandex"
l!l
01
MC/DC0%0/1
$self->{"name"} ||= "yandex"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
Aresult
11
00
22 1 ▶ $self->{l10n} ||= 'en'; # en | ru | tr | be | kk | uk
Condition50%1/2
$self->{"l10n"} ||= "en"
l!l
01
MC/DC0%0/1
$self->{"l10n"} ||= "en"
$self->{"l10n"}
Truth table50%1/2
A
A= $self->{"l10n"}
Aresult
11
00
23 }
24  
25 1 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
26 1 ▶sub api_key { $_[0]->{api_key} }
Subroutine api_key: called CC=1 SCAR=0.0
Pod documented
27 1 ▶sub folderid { $_[0]->{folderid} }
Subroutine folderid: called CC=1 SCAR=0.0
Pod documented
28 1 ▶sub l10n { $_[0]->{l10n} }
Subroutine l10n: called CC=1 SCAR=0.0
Pod documented
29  
30 1 ▶sub search {
Subroutine search: called CC=8 SCAR=22.7
Pod documented
31 1 my ( $self, $http, $query, $opts ) = @_;
32 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
33 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
34  
35 1 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 1 ▶ // sprintf('attr=d.mode=deep.groups-on-page=%d.docs-in-group=1', $limit),
Condition42%3/7
$opts->{"l10n"} // $self->l10n
l!l&&r!l&&!r
010
$opts->{"sortby"} // "rlv"
l!l
01
$opts->{"groupby"} // sprintf("attr=d.mode=deep.groups-on-page=%d.docs-in-group=1", $limit)
l!l
01
MC/DC0%0/2
$opts->{"l10n"} // $self->l10n
$opts->{"l10n"} $self->l10n
MC/DC0%0/1
$opts->{"sortby"} // "rlv"
$opts->{"sortby"}
MC/DC0%0/1
$opts->{"groupby"} // sprintf("attr=d.mode=deep.groups-on-page=%d.docs-in-group=1", $limit)
$opts->{"groupby"}
Truth table33%1/3
A // B
A= $opts->{"l10n"}B= $self->l10n
ABresult
1X1
011
000
Truth table50%1/2
A
A= $opts->{"sortby"}
Aresult
11
00
Truth table50%1/2
A
A= $opts->{"groupby"}
Aresult
11
00
44 );
45 1 ▶ $q{filter} = $opts->{safesearch} if defined $opts->{safesearch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"safesearch"} 0 1 1
46 1 ▶ $q{lr} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 0 1 1
47 1 $uri->query_form(%q);
48  
49 1 my $req = GET( $uri->as_string );
50 1 $req->header( 'User-Agent' => $self->user_agent_string );
51 1 $req->header( 'Accept' => 'application/xml' );
52  
53 1 ▶ return $http->do_request( request => $req )->then(sub {
Subroutine __ANON__: called CC=11 SCAR=26.5
54 1 my ( $resp ) = @_;
55 1 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 1 1
56 0 return Future->fail(
57 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
58 );
59 }
60 1 my $doc = eval { XML::LibXML->new->parse_string( $resp->decoded_content ) };
61 1 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 1 1
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 1 ▶ if ( my ($err) = $doc->findnodes('/yandexsearch/response/error') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($err) = $doc->findnodes("/yandexsearch/response/error")) 0 1 1
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 1 my @out;
73 1 my $rank = 0;
74 1 for my $doc_node ( $doc->findnodes('/yandexsearch/response/results/grouping/group/doc') ) {
75 2 my ($url_node) = $doc_node->findnodes('./url');
76 2 my ($title_node) = $doc_node->findnodes('./title');
77 2 my @passages = $doc_node->findnodes('./passages/passage');
78 2 my @headline = $doc_node->findnodes('./headline');
79 2 ▶ my $url = $url_node ? $url_node->textContent : next;
Branch: 1 branch
BranchTrueFalseTotal
$url_node ? : 2 0 2
80 2 ▶ my $title = $title_node ? $title_node->textContent : '';
Branch: 1 branch
BranchTrueFalseTotal
$title_node ? : 2 0 2
81 2 my $snippet;
82 2 ▶ if (@passages) {
Branch: 2 branches
BranchTrueFalseTotal
if (@passages) { } 1 1 2
elsif (@headline) { } 1 0 1
83 1 $snippet = join ' … ', map { $_->textContent } @passages;
84 } elsif (@headline) {
85 1 $snippet = $headline[0]->textContent;
86 }
87 2 $rank++;
88 2 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 2 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 2 2
96 }
97 1 return Future->done(\@out);
98 1 });
99 }
100  
101 2 1;
102  
103 __END__
« WebSearch/Provider/Google.pm ↑ Summary WebSearch.pm »