DDG/Request.pm

filter: Statementstmt 90.3% 94 / 104 Branchbran 100% 20 / 20 Conditioncond 81.8% 9 / 11 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 78.9% 15 / 19 Podpod 50.0% 2 / 4 totaltotal 88.3% 144 / 163 CC 20 SCAR 13.9
CC
20
Cov
91%
generate_remainder
8.0
_build_matched_trigger
6.0
_build_triggers
5.0
CRAP 4.0
« DDG/ZeroClickInfo/Fathead.pm ↑ Summary DDG/ZeroClickInfo/Spice/Data.pm »
1 package DDG::Request;
2 10 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: A request to DuckDuckGo itself, so the query itself and parameter around the query defining him
4 10 $DDG::Request::VERSION = '1017';
5 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 #
10 # QUERY
11 #
12 ###############################
13  
14  
15 10 has query_raw => (
16 is => 'ro',
17 required => 1,
18 );
19  
20 10 my $whitespaces = qr{\s+};
21 10 my $whitespaces_matches = qr{($whitespaces)};
22 10 my $whitespaces_dashes = qr{[\s\-]+};
23 10 my $non_alphanumeric_ascii = qr{[\x00-\x1f\x21-\x2f\x3a-\x40\x5b-\x60\x7b-\x81\x{a7}]+};
24  
25  
26 10 has query_raw_parts => (
27 is => 'ro',
28 lazy => 1,
29 builder => '_build_query_raw_parts',
30 );
31 sub _build_query_raw_parts {
32 [
33 54 ▶ split(/$whitespaces_matches/,shift->query_raw)
Subroutine _build_query_raw_parts: called CC=1 SCAR=0.0
34 ]
35 }
36  
37  
38 10 has query_parts => (
39 is => 'ro',
40 lazy => 1,
41 builder => '_build_query_parts',
42 );
43 sub _build_query_parts {
44 18 ▶ my $x;
Subroutine _build_query_parts: called CC=1 SCAR=0.0
45 [
46 52 grep { ( $x += length ) < 500 } # 500 matches the internal query max
47 100 grep { ! /$whitespaces/ }
48 107 grep { length }
49 18 @{shift->query_raw_parts}
50 ]
51 }
52  
53  
54 10 has query_parts_lc => (
55 is => 'ro',
56 lazy => 1,
57 builder => '_build_query_parts_lc',
58 );
59 sub _build_query_parts_lc {
60 [
61 0 map { lc }
62 0 ▶ @{shift->query_parts}
Subroutine _build_query_parts_lc: not called CC=1 SCAR=6.9
63 ]
64 }
65  
66  
67 10 has triggers => (
68 is => 'ro',
69 lazy => 1,
70 builder => '_build_triggers',
71 );
72 sub _build_triggers {
73 49 ▶ my ( $self ) = @_;
Subroutine _build_triggers: called CC=5 SCAR=16.1
74 49 my @parts = @{$self->query_raw_parts};
75 49 ▶ return {} if not scalar @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @parts 1 48 49
76 48 ▶ my $x = $parts[0] eq '' ? 2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$parts[0] eq '' ? : 14 34 48
77 48 my %triggers;
78 48 for ($x..(scalar @parts-1)) {
79 261 ▶ unless ($_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ % 2) 149 112 261
80 149 $triggers{$_} = $self->generate_triggers($parts[$_]);
81 }
82 }
83 48 return \%triggers;
84 }
85  
86  
87 sub generate_triggers {
88 149 ▶ my $part = lc $_[1];
Subroutine generate_triggers: called CC=5 SCAR=16.1
Pod documented
89 149 my %parts;
90 149 ++$parts{$part};
91 149 ▶ $part =~ s/^!//go && ++$parts{$part};
Branch: 1 branch
BranchTrueFalseTotal
$part =~ s/^!//go and ++$parts{$part} 8 141 149
92 149 ▶ $part =~ s/\?$//go && ++$parts{$part};
Branch: 1 branch
BranchTrueFalseTotal
$part =~ s/\?$//go and ++$parts{$part} 4 145 149
93 # Look for non-word characters, except single quotes, e.g. can't, John's
94 149 ▶ if ($part =~ /[^\w']/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /[^\w']/o) 10 139 149
95 # The split could be part of the if but it would leave single quotes
96 # in the resulting terms.
97 10 my @boundary_words = split /\W+/o, $part ;
98 10 ++$parts{$_} for @boundary_words;
99 10 ++$parts{join('', @boundary_words)};
100 10 ++$parts{join(' ', @boundary_words)};
101 }
102 149 return [keys %parts];
103 }
104  
105  
106 10 has remainder => (
107 is => 'rwp',
108 lazy => 1
109 );
110  
111 sub generate_remainder {
112 24 ▶ my ( $self, $from_pos, $to_pos ) = @_;
Subroutine generate_remainder: called CC=8 SCAR=20.8
Pod documented
113 24 ▶ $to_pos = $from_pos unless defined $to_pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to_pos 14 10 24
114 24 my @query_raw_parts = @{$self->query_raw_parts};
115 24 my $max = scalar @query_raw_parts-1;
116 24 my $remainder = '';
117 24 ▶ if ( $to_pos < $max && ( $from_pos == 0 || ( $from_pos == 2 && $query_raw_parts[0] eq '' ) ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($to_pos < $max and $from_pos == 0 || $from_pos == 2 && $query_raw_parts[0] eq '') { } 10 14 24
elsif ($max % 2 ? $to_pos == $max - 1 : $to_pos == $max) { } 11 3 14
Condition100%9/9
$to_pos < $max and $from_pos == 0 || $from_pos == 2 && $query_raw_parts[0] eq ""
!ll&&!rl&&r
9510
$from_pos == 0 or $from_pos == 2 and $query_raw_parts[0] eq ""
l!l&&r!l&&!r
735
$from_pos == 2 and $query_raw_parts[0] eq ""
!ll&&!rl&&r
413
MC/DC100%4/4
$to_pos < $max and $from_pos == 0 || $from_pos == 2 && $query_raw_parts[0] eq ""
$to_pos < $max $from_pos == 0 $from_pos == 2 $query_raw_parts[0] eq ""
Truth table100%5/5
A and B or C and D
A= $to_pos < $maxB= $from_pos == 0C= $from_pos == 2D= $query_raw_parts[0] eq ""
ABCDresult
0XXX0
100X0
10100
11XX1
10111
118 10 $remainder = join('',@query_raw_parts[$to_pos+1..$max]);
119 10 $remainder =~ s/^\s//;
120 ▶ } elsif ( $max % 2 ? $to_pos == $max-1 : $to_pos == $max ) {
Branch: 1 branch
BranchTrueFalseTotal
$max % 2 ? : 3 11 14
121 11 $remainder = join('',@query_raw_parts[0..$from_pos-1]);
122 11 $remainder =~ s/\s$//;
123 } else {
124 3 my $left_remainder = join('',@query_raw_parts[0..$from_pos-1]);
125 3 my $right_remainder = join('',@query_raw_parts[$to_pos+1..$max]);
126 3 $left_remainder =~ s/\s$//;
127 3 $right_remainder =~ s/^\s//;
128 3 $remainder = $left_remainder.' '.$right_remainder;
129 }
130 24 $self->_set_remainder($remainder);
131 24 return $remainder;
132 }
133  
134  
135 10 has matched_trigger => (
136 is => 'ro',
137 lazy => 1,
138 builder => 1
139 );
140  
141 sub _build_matched_trigger {
142 0 ▶ my $self = shift;
Subroutine _build_matched_trigger: not called CC=2 SCAR=17.9
143  
144 0 ▶ my $r = $self->remainder || '';
Condition0%0/2
$self->remainder || ''
l!l
00
MC/DC0%0/1
$self->remainder || ''
$self->remainder
Truth table0%0/2
A
A= $self->remainder
Aresult
11
00
145 0 my $qr = $self->query_raw;
146 0 $qr =~ s/\s*\Q$r\E\s*//i;
147 0 return $qr;
148 }
149  
150  
151 10 has query => (
152 is => 'ro',
153 lazy => 1,
154 builder => '_build_query',
155 );
156 sub _build_query {
157 12 ▶ join(' ',@{shift->query_parts})
Subroutine _build_query: called CC=1 SCAR=0.0
158 }
159  
160  
161 10 has query_lc => (
162 is => 'ro',
163 lazy => 1,
164 builder => '_build_query_lc',
165 );
166 sub _build_query_lc {
167 lc(shift->query)
168 11 ▶}
Subroutine _build_query_lc: called CC=1 SCAR=0.0
169  
170  
171 10 has query_nowhitespace => (
172 is => 'ro',
173 lazy => 1,
174 builder => '_build_query_nowhitespace',
175 );
176 sub _build_query_nowhitespace {
177 11 ▶ for (shift->query) {
Subroutine _build_query_nowhitespace: called CC=2 SCAR=6.9
178 11 s/$whitespaces//g;
179 11 return $_;
180 }
181 }
182  
183  
184 10 has query_nowhitespace_nodash => (
185 is => 'ro',
186 lazy => 1,
187 builder => '_build_query_nowhitespace_nodash',
188 );
189 sub _build_query_nowhitespace_nodash {
190 11 ▶ for (shift->query) {
Subroutine _build_query_nowhitespace_nodash: called CC=2 SCAR=6.9
191 11 s/$whitespaces_dashes//g;
192 11 return $_;
193 }
194 }
195  
196  
197 10 has query_clean => (
198 is => 'ro',
199 lazy => 1,
200 builder => '_build_query_clean',
201 );
202 sub _build_query_clean {
203 11 ▶ for (shift->query_lc) {
Subroutine _build_query_clean: called CC=2 SCAR=6.9
204 11 s/$non_alphanumeric_ascii//g;
205 11 s/$whitespaces/ /g;
206 11 return $_;
207 }
208 }
209  
210  
211 10 has words => (
212 is => 'ro',
213 lazy => 1,
214 builder => '_build_words',
215 );
216 sub _build_words {
217 [
218 11 ▶ grep { length }
Subroutine _build_words: called CC=1 SCAR=0.0
219 split(/$whitespaces/,shift->query_clean)
220 ]
221 }
222  
223  
224 10 has wordcount => (
225 is => 'ro',
226 lazy => 1,
227 builder => '_build_wordcount',
228 );
229 11 ▶sub _build_wordcount { scalar @{shift->words} }
Subroutine _build_wordcount: called CC=1 SCAR=0.0
230  
231  
232 10 has seen_plugins => (
233 is => 'rw',
234 lazy => 1,
235 builder => '_build_seen_plugins',
236 );
237 41 ▶sub _build_seen_plugins {[]}
Subroutine _build_seen_plugins: called CC=1 SCAR=0.0
238  
239 #
240 # LANGUAGE / LOCATION / IP
241 #
242 ###############################
243  
244 # DDG::Language TODO
245 10 has language => (
246 #isa => 'DDG::Language',
247 is => 'ro',
248 predicate => 'has_language',
249 );
250 0 ▶sub lang { shift->language }
Subroutine lang: not called CC=1 SCAR=6.9
Pod undocumented
251  
252 10 has location => (
253 #isa => 'DDG::Location',
254 is => 'ro',
255 predicate => 'has_location',
256 );
257 0 ▶sub loc { shift->location }
Subroutine loc: not called CC=1 SCAR=6.9
Pod undocumented
258  
259 10 1;
260  
261 __END__
« DDG/ZeroClickInfo/Fathead.pm ↑ Summary DDG/ZeroClickInfo/Spice/Data.pm »