WebSearch/Provider/Google.pm

filter: Statementstmt 98.8% 85 / 86 Branchbran 69.4% 25 / 36 Conditioncond 65.2% 15 / 23 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 19 / 19 Podpod 100% 4 / 4 totaltotal 85.4% 153 / 179 CC 30 SCAR 20.7
CC
30
Cov
86%
__ANON__
17.3
__ANON__
7.4
_init
5.7
CRAP 7.9
« WebSearch/Provider/Reddit.pm ↑ Summary WebSearch/Provider/Yandex.pm »
1 package Net::Async::WebSearch::Provider::Google;
2 3 our $VERSION = '0.003';
3 # ABSTRACT: Google Programmable Search (CSE) JSON API provider
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Net::Async::WebSearch::Provider';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Future::Utils qw( repeat );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use HTTP::Request::Common qw( GET );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Net::Async::WebSearch::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 ▶sub _init {
Subroutine _init: called CC=5 SCAR=17.5
17 8 my ( $self ) = @_;
18 8 ▶ croak "Google provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 0 8 8
19 8 ▶ croak "Google provider requires 'cx' (Programmable Search engine id)" unless $self->{cx};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cx"} 0 8 8
20 8 ▶ $self->{endpoint} ||= 'https://www.googleapis.com/customsearch/v1';
Condition50%1/2
$self->{"endpoint"} ||= "https://www.googleapis.com/customsearch/v1"
l!l
08
MC/DC0%0/1
$self->{"endpoint"} ||= "https://www.googleapis.com/customsearch/v1"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
Aresult
11
00
21 8 ▶ $self->{name} ||= 'google';
Condition50%1/2
$self->{"name"} ||= "google"
l!l
08
MC/DC0%0/1
$self->{"name"} ||= "google"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
Aresult
11
00
22 }
23  
24 24 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
25 24 ▶sub api_key { $_[0]->{api_key} }
Subroutine api_key: called CC=1 SCAR=0.0
Pod documented
26 24 ▶sub cx { $_[0]->{cx} }
Subroutine cx: called CC=1 SCAR=0.0
Pod documented
27  
28 8 ▶sub search {
Subroutine search: called CC=3 SCAR=11.1
Pod documented
29 8 my ( $self, $http, $query, $opts ) = @_;
30 8 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
80
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
31 8 ▶ my $limit = $opts->{limit} || 10;
Condition100%2/2
$opts->{'limit'} || 10
l!l
71
MC/DC100%1/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table100%2/2
A
A= $opts->{'limit'}
Aresult
11
00
32  
33 # CSE serves at most 10 results per call and never past the 100th hit
34 # overall, so satisfy limit > 10 by paging over the 1-based `start` index
35 # (start=1,11,21,…) until we have `limit` results or the API runs dry.
36 8 my @out;
37 8 my $rank = 0;
38 8 my $exhausted = 0;
39  
40 24 ▶ my $paging = repeat {
Subroutine __ANON__: called CC=7 SCAR=20.0
41 24 my $start = @out + 1; # CSE `start` is 1-based
42 24 my $num = $limit - @out;
43 24 ▶ $num = 10 if $num > 10; # CSE hard-caps num to 10
Branch: 1 branch
BranchTrueFalseTotal
if $num > 10 19 5 24
44 24 ▶ $num = 100 - @out if @out + $num > 100; # never page past hit #100
Branch: 1 branch
BranchTrueFalseTotal
if @out + $num > 100 0 24 24
45  
46 24 my $uri = URI->new( $self->endpoint );
47 24 my %q = (
48 q => $query,
49 key => $self->api_key,
50 cx => $self->cx,
51 num => $num,
52 start => $start,
53 );
54 24 ▶ $q{gl} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 0 24 24
55 24 ▶ $q{hl} = $opts->{language} if defined $opts->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"language"} 0 24 24
56 24 ▶ $q{safe} = $opts->{safesearch} if defined $opts->{safesearch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"safesearch"} 0 24 24
57 24 ▶ $q{dateRestrict} = $opts->{date_restrict} if defined $opts->{date_restrict};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"date_restrict"} 0 24 24
58 24 $uri->query_form(%q);
59  
60 24 my $req = GET( $uri->as_string );
61 24 $req->header( 'User-Agent' => $self->user_agent_string );
62 24 $req->header( 'Accept' => 'application/json' );
63  
64 24 ▶ $http->do_request( request => $req )->then(sub {
Subroutine __ANON__: called CC=15 SCAR=28.5
65 24 my ( $resp ) = @_;
66 24 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 1 23 24
67 1 return Future->fail(
68 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
69 );
70 }
71 23 my $data = eval { decode_json( $resp->decoded_content ) };
72 23 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 23 23
73 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
74 }
75 23 ▶ my $items = $data->{items} || [];
Condition100%2/2
$data->{'items'} || []
l!l
221
MC/DC100%1/1
$data->{'items'} || []
$data->{'items'}
Truth table100%2/2
A
A= $data->{'items'}
Aresult
11
00
76 23 ▶ $exhausted = 1 unless @$items; # no more results upstream
Branch: 1 branch
BranchTrueFalseTotal
unless @$items 1 22 23
77 23 for my $r ( @$items ) {
78 190 $rank++;
79 190 ▶ my $pagemap = $r->{pagemap} || {};
Condition100%2/2
$r->{'pagemap'} || {}
l!l
1189
MC/DC100%1/1
$r->{'pagemap'} || {}
$r->{'pagemap'}
Truth table100%2/2
A
A= $r->{'pagemap'}
Aresult
11
00
80 190 ▶ my ($metatags) = @{ $pagemap->{metatags} || [] };
Condition100%2/2
$pagemap->{'metatags'} || []
l!l
1189
MC/DC100%1/1
$pagemap->{'metatags'} || []
$pagemap->{'metatags'}
Truth table100%2/2
A
A= $pagemap->{'metatags'}
Aresult
11
00
81 push @out, Net::Async::WebSearch::Result->new(
82 url => $r->{link},
83 title => $r->{title},
84 snippet => $r->{snippet},
85 provider => $self->name,
86 rank => $rank,
87 published_at => (
88 $metatags && ( $metatags->{'article:published_time'}
89 // $metatags->{'og:article:published_time'}
90 // $metatags->{'date'} )
91 ),
92 raw => $r,
93 extra => {
94 ( defined $r->{displayLink} ? ( displayLink => $r->{displayLink} ) : () ),
95 ( defined $r->{mime} ? ( mime => $r->{mime} ) : () ),
96 190 ▶ ( defined $r->{fileFormat} ? ( fileFormat => $r->{fileFormat} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
defined $r->{'displayLink'} ? : 190 0 190
defined $r->{'mime'} ? : 0 190 190
defined $r->{'fileFormat'} ? : 0 190 190
Condition44%4/9
$metatags && (($metatags->{'article:published_time'} // $metatags->{'og:article:published_time'}) // $metatags->{'date'})
!ll&&!rl&&r
18901
($metatags->{"article:published_time"} // $metatags->{"og:article:published_time"}) // $metatags->{"date"}
l!l&&r!l&&!r
100
$metatags->{"article:published_time"} // $metatags->{"og:article:published_time"}
l!l&&r!l&&!r
100
MC/DC25%1/4
$metatags && (($metatags->{'article:published_time'} // $metatags->{'og:article:published_time'}) // $metatags->{'date'})
$metatags $metatags->{"article:published_time"} $metatags->{"og:article:published_time"} $metatags->{"date"}
Truth table40%2/5
A && B // C // D
A= $metatagsB= $metatags->{"article:published_time"}C= $metatags->{"og:article:published_time"}D= $metatags->{"date"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
97 },
98 );
99 190 ▶ last if @out >= $limit; # honour limit exactly
Branch: 1 branch
BranchTrueFalseTotal
if @out >= $limit 5 185 190
100 }
101 23 return Future->done;
102 24 });
103 24 ▶ } until => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
104 24 my ( $trial ) = @_;
105 24 ▶ return 1 if $trial->failure; # stop and propagate on error
Branch: 1 branch
BranchTrueFalseTotal
if $trial->failure 1 23 24
106 23 ▶ return 1 if @out >= $limit; # limit satisfied
Branch: 1 branch
BranchTrueFalseTotal
if @out >= $limit 5 18 23
107 18 ▶ return 1 if @out >= 100; # CSE result ceiling reached
Branch: 1 branch
BranchTrueFalseTotal
if @out >= 100 1 17 18
108 17 return $exhausted; # nothing left to page
109 8 };
110  
111 8 ▶ return $paging->then(sub { Future->done( \@out ) });
Subroutine __ANON__: called CC=1 SCAR=0.0
112 }
113  
114 3 1;
115  
116 __END__
« WebSearch/Provider/Reddit.pm ↑ Summary WebSearch/Provider/Yandex.pm »