blib/lib/Tie/DuckDuckGo.pm

filter: Statementstmt 73.2% 52 / 71 Branchbran 50.0% 11 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 15 / 21 Podpod 0.0% 0 / 2 totaltotal 67.2% 78 / 116 CC 13 SCAR 13.0
CC
13
Cov
68%
do_search
8.5
FETCH
6.0
SPLICE
6.0
CRAP 3.7
↑ Summary
1 package Tie::DuckDuckGo;
2  
3 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.02';
9  
10 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # when tying, call the constructor with data type name
16 1 ▶sub TIEARRAY { shift->new('array', @_) }
Subroutine TIEARRAY: called CC=1 SCAR=0.0
17 1 ▶sub TIEHASH { shift->new('hash', @_) }
Subroutine TIEHASH: called CC=1 SCAR=0.0
18 1 ▶sub TIESCALAR { shift->new('scalar', @_) }
Subroutine TIESCALAR: called CC=1 SCAR=0.0
19  
20 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
21 3 my ($class, $type, $query) = @_;
22  
23 # keep track of tied data type
24 3 my $self = bless({type => $type}, $class);
25  
26 # for now i'm using a key _tie_ddg,
27 # but maybe this can be simplified
28 3 ▶ if ($type eq 'array') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'array') { } 1 2 3
elsif ($type eq 'hash') { } 1 1 2
29 1 $self->do_search('_tie_ddg', $query);
30 }
31 elsif ($type eq 'hash') {
32 1 $self->do_search($query, $query);
33 }
34 else {
35 ## $scalar = 1
36 1 $self->do_search('_tie_ddg', $query, 1);
37 }
38  
39 3 $self;
40 }
41  
42 3 ▶sub do_search {
Subroutine do_search: called CC=6 SCAR=21.4
Pod undocumented
43 3 my ($self, $store_as, $query, $scalar) = @_;
44 3 ▶ return unless defined $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query 0 3 3
45  
46 # do search
47 3 my $uri = URI->new('https://duckduckgo.com/html/');
48 3 $uri->query_form({ q => $query });
49  
50 3 my $resp = HTTP::Tiny->new->get($uri->as_string);
51 3 my $tree = HTML::TreeBuilder::XPath->new_from_content($resp->{content});
52  
53 # build results
54 3 my @results = $tree->findnodes(
55 './/div[@id="content"]//div[@id="links"]
56 //div[@class =~ /\bresults_links\b/]
57 //div[@class =~ /\blinks_main\b/]'
58 );
59  
60 3 my @save;
61 3 for my $result (@results) {
62 0 ▶ my ($link) = $result->findnodes('.//a[@class="large"]') or next;
Branch: 1 branch
BranchTrueFalseTotal
my($link) = $result->findnodes(".//a[\@class=\"large\"]") or next 0 0 0
63 0 my ($snippet) = $result->findvalues('.//div[@class="snippet"]');
64  
65 0 push @save,
66 { url => $link->attr('href'),
67 title => $link->as_text,
68 snippet => $snippet,
69 };
70 0 ▶ last if $scalar; # only save one result for scalar type
Branch: 1 branch
BranchTrueFalseTotal
if $scalar 0 0 0
71 }
72  
73 3 ▶ $self->{data}{$store_as} = $scalar ? [$save[0]] : \@save;
Branch: 1 branch
BranchTrueFalseTotal
$scalar ? : 1 2 3
74 }
75  
76 1 ▶sub FETCH {
Subroutine FETCH: called CC=4 SCAR=17.9
77 1 my ($self, $index) = @_;
78 1 ▶ $index = 0 unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 1 0 1
79  
80 # if we're accessing a hash, we may have not done the search already
81 1 ▶ if ($self->{type} eq 'hash') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "hash") 0 1 1
82 0 ▶ $self->do_search($index, $index) unless exists $self->{data}{$index};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$index} 0 0 0
83  
84 0 return $self->{data}{$index};
85 }
86  
87 1 $self->{data}{_tie_ddg}[$index];
88 }
89  
90 2 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
91 2 my $self = shift;
92 2 scalar @{ $self->{data}{_tie_ddg} };
93 }
94  
95 0 ▶sub SHIFT { shift @{shift->{data}{_tie_ddg}} }
Subroutine SHIFT: not called CC=1 SCAR=6.9
96 0 ▶sub POP { pop @{shift->{data}{_tie_ddg}} }
Subroutine POP: not called CC=1 SCAR=6.9
97  
98 0 ▶sub SPLICE {
Subroutine SPLICE: not called CC=2 SCAR=17.9
99 0 my ($self, $offset, $limit) = @_;
100  
101 # oops, I don't think we want to write anything to the list
102 0 ▶ if (@_ > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 3) 0 0 0
103 0 Carp::carp q,Can't replace search results with list!,;
104 0 return;
105 }
106  
107 0 splice(@{$self->{data}{_tie_ddg}}, $offset, $limit);
108 }
109  
110 2 ▶sub EXISTS {
Subroutine EXISTS: called CC=2 SCAR=6.9
111 2 my ($self, $index) = @_;
112 2 ▶ return exists($self->{data}{$index}) if $self->{type} eq 'hash';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"type"} eq "hash" 1 1 2
113 1 exists $self->{data}{_tie_ddg}[$index];
114 }
115  
116 0 ▶sub STORE { Carp::carp q,Can't store search results to hash!, }
Subroutine STORE: not called CC=1 SCAR=6.9
117 0 ▶sub PUSH { Carp::carp q,Can't push search results to array!, }
Subroutine PUSH: not called CC=1 SCAR=6.9
118 0 ▶sub UNSHIFT { Carp::carp q,Can't unshift search results to array!, }
Subroutine UNSHIFT: not called CC=1 SCAR=6.9
119  
120 1 1;
121 __END__
↑ Summary