WebSearch/Provider/Google.pm

filter: Statementstmt 42.0% 29 / 69 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 60.0% 9 / 15 Podpod 100% 4 / 4 totaltotal 28.7% 42 / 146 CC 25 SCAR 45.5
CC
25
Cov
25%
__ANON__
210.0
search
72.0
_init
30.0
CRAP 94.4
↑ Summary WebSearch/Provider/Yandex.pm »
1 package Net::Async::WebSearch::Provider::Google;
2 1 our $VERSION = '0.002';
3 # ABSTRACT: Google Programmable Search (CSE) JSON API 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 JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Request::Common qw( GET );
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=5 SCAR=34.0
17 0 ▶ croak "Google provider requires 'api_key'" unless $self->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"api_key"} 0 0 0
18 0 ▶ croak "Google provider requires 'cx' (Programmable Search engine id)" unless $self->{cx};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cx"} 0 0 0
19 0 ▶ $self->{endpoint} ||= 'https://www.googleapis.com/customsearch/v1';
Condition0%0/2
$self->{"endpoint"} ||= "https://www.googleapis.com/customsearch/v1"
l!l
00
MC/DC0%0/1
$self->{"endpoint"} ||= "https://www.googleapis.com/customsearch/v1"
$self->{"endpoint"}
Truth table0%0/2
A
A= $self->{"endpoint"}
Aresult
11
00
20 0 ▶ $self->{name} ||= 'google';
Condition0%0/2
$self->{"name"} ||= "google"
l!l
00
MC/DC0%0/1
$self->{"name"} ||= "google"
$self->{"name"}
Truth table0%0/2
A
A= $self->{"name"}
Aresult
11
00
21 }
22  
23 0 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: not called CC=1 SCAR=6.9
Pod documented
24 0 ▶sub api_key { $_[0]->{api_key} }
Subroutine api_key: not called CC=1 SCAR=6.9
Pod documented
25 0 ▶sub cx { $_[0]->{cx} }
Subroutine cx: not called CC=1 SCAR=6.9
Pod documented
26  
27 sub search {
28 0 ▶ my ( $self, $http, $query, $opts ) = @_;
Subroutine search: not called CC=8 SCAR=42.8
Pod documented
29 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
30 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
31  
32 0 my $uri = URI->new( $self->endpoint );
33 0 ▶ my %q = (
Branch: 1 branch
BranchTrueFalseTotal
$limit > 10 ? : 0 0 0
34 q => $query,
35 key => $self->api_key,
36 cx => $self->cx,
37 num => ( $limit > 10 ? 10 : $limit ), # CSE hard-caps num to 10
38 );
39 0 ▶ $q{gl} = $opts->{region} if defined $opts->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"region"} 0 0 0
40 0 ▶ $q{hl} = $opts->{language} if defined $opts->{language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"language"} 0 0 0
41 0 ▶ $q{safe} = $opts->{safesearch} if defined $opts->{safesearch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"safesearch"} 0 0 0
42 0 ▶ $q{dateRestrict} = $opts->{date_restrict} if defined $opts->{date_restrict};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"date_restrict"} 0 0 0
43 0 $uri->query_form(%q);
44  
45 0 my $req = GET( $uri->as_string );
46 0 $req->header( 'User-Agent' => $self->user_agent_string );
47 0 $req->header( 'Accept' => 'application/json' );
48  
49 return $http->do_request( request => $req )->then(sub {
50 0 ▶ my ( $resp ) = @_;
Subroutine __ANON__: not called CC=14 SCAR=53.5
51 0 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
52 0 return Future->fail(
53 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
54 );
55 }
56 0 my $data = eval { decode_json( $resp->decoded_content ) };
57 0 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
58 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
59 }
60 0 my @out;
61 0 my $rank = 0;
62 0 ▶ for my $r ( @{ $data->{items} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$data->{"items"} or [] 0 0 0
63 0 $rank++;
64 0 ▶ my $pagemap = $r->{pagemap} || {};
Condition0%0/2
$r->{'pagemap'} || {}
l!l
00
MC/DC0%0/1
$r->{'pagemap'} || {}
$r->{'pagemap'}
Truth table0%0/2
A
A= $r->{'pagemap'}
Aresult
11
00
65 0 ▶ my ($metatags) = @{ $pagemap->{metatags} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$pagemap->{"metatags"} or [] 0 0 0
66 push @out, Net::Async::WebSearch::Result->new(
67 url => $r->{link},
68 title => $r->{title},
69 snippet => $r->{snippet},
70 provider => $self->name,
71 rank => $rank,
72 published_at => (
73 $metatags && ( $metatags->{'article:published_time'}
74 // $metatags->{'og:article:published_time'}
75 // $metatags->{'date'} )
76 ),
77 raw => $r,
78 extra => {
79 ( defined $r->{displayLink} ? ( displayLink => $r->{displayLink} ) : () ),
80 ( defined $r->{mime} ? ( mime => $r->{mime} ) : () ),
81 0 ▶ ( defined $r->{fileFormat} ? ( fileFormat => $r->{fileFormat} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
defined $r->{'displayLink'} ? : 0 0 0
defined $r->{'mime'} ? : 0 0 0
defined $r->{'fileFormat'} ? : 0 0 0
Condition0%0/9
$metatags && (($metatags->{'article:published_time'} // $metatags->{'og:article:published_time'}) // $metatags->{'date'})
!ll&&!rl&&r
000
($metatags->{"article:published_time"} // $metatags->{"og:article:published_time"}) // $metatags->{"date"}
l!l&&r!l&&!r
000
$metatags->{"article:published_time"} // $metatags->{"og:article:published_time"}
l!l&&r!l&&!r
000
MC/DC0%0/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 table0%0/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
82 },
83 );
84 0 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 0 0
85 }
86 0 return Future->done(\@out);
87 0 });
88 }
89  
90 1 1;
91  
92 __END__
↑ Summary WebSearch/Provider/Yandex.pm »