DDG/Meta/RequestHandler.pm

filter: Statementstmt 86.8% 99 / 114 Branchbran 63.6% 28 / 44 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 87.5% 14 / 16 Podpod 100% 3 / 3 totaltotal 80.7% 147 / 182 SCAR 35.7
CC
29
Cov
80%
__ANON__
18.8
__ANON__
12.0
__ANON__
6.0
CRAP 35.6
« DDG/Rewrite.pm ↑ Summary DDG/Location.pm »
1 package DDG::Meta::RequestHandler;
2 11 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: Functions for a request handler
4 11 $DDG::Meta::RequestHandler::VERSION = '1016';
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use DDG::Location;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use DDG::Language;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 require Moo::Role;
12  
13  
14 11 my @request_scalar_attributes = qw(
15  
16 query_raw
17 query_nowhitespace
18 query_nowhitespace_nodash
19 query
20 query_lc
21 query_clean
22 wordcount
23  
24 );
25  
26 11 my @request_array_attributes = qw(
27  
28 words
29 query_parts
30 query_raw_parts
31  
32 );
33  
34 11 my $default_handler = 'query_raw';
35  
36  
37 11 my %applied;
38  
39 sub apply_keywords {
40 35 ▶ my ( $class, $target, $result_handler, $role ) = @_;
Subroutine apply_keywords: called CC=2 SCAR=7.4
Pod documented
41  
42 35 ▶ return if exists $applied{$target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $applied{$target} 0 35 35
43 35 $applied{$target} = undef;
44
45 35 my $stash = Package::Stash->new($target);
46  
47 35 $class->reset_request_symbols($stash);
48  
49  
50 $stash->add_symbol('&handle',sub {
51 27 ▶ my $handler = shift;
Subroutine __ANON__: called CC=14 SCAR=29.3
52 27 my $code;
53 27 ▶ if (ref $handler eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq 'CODE') { } 6 21 27
54 6 $code = $handler;
55 6 $handler = $default_handler;
56 } else {
57 21 $code = shift;
58 21 ▶ croak "We need a CODEREF for the handler" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 21 21
59 }
60 27 ▶ croak "Please define triggers before you define a handler" unless $target->has_triggers;
Branch: 1 branch
BranchTrueFalseTotal
unless $target->has_triggers 1 26 27
61  
62 26 ▶ if (grep { $_ eq $handler } @request_scalar_attributes) {
Branch: 6 branches
BranchTrueFalseTotal
if (grep {$_ eq $handler;} @request_scalar_attributes) { } 7 19 26
elsif (grep {$_ eq $handler;} @request_array_attributes) { } 3 16 19
elsif ($handler eq 'request') { } 0 16 16
elsif ($handler eq 'remainder' or $handler eq 'remainder_lc') { } 11 5 16
elsif ($handler eq 'matches') { } 5 0 5
elsif ($handler eq 'all') { } 0 0 0
63 $stash->add_symbol('&handle_request_matches',sub {
64 10 ▶ my ( $self, $request ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
65 10 $class->request_symbols($stash,$request);
66 10 my @result;
67 10 my $default = $request->$handler;
68 10 for ($default) {
69 10 @result = $code->($_);
70 }
71 10 $class->reset_request_symbols($stash);
72 10 ▶ return @result ? $result_handler->($self,@result) : ();
Branch: 1 branch
BranchTrueFalseTotal
@result ? : 10 0 10
73 7 });
74 57 } elsif (grep { $_ eq $handler } @request_array_attributes) {
75 $stash->add_symbol('&handle_request_matches',sub {
76 7 ▶ my ( $self, $request ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
77 7 $class->request_symbols($stash,$request);
78 7 my @result;
79 7 my $default = $request->$default_handler;
80 7 for ($default) {
81 7 @result = $code->(@{$request->$handler});
82 }
83 7 $class->reset_request_symbols($stash);
84 7 ▶ return @result ? $result_handler->($self,@result) : ();
Branch: 1 branch
BranchTrueFalseTotal
@result ? : 7 0 7
85 3 });
86 # LEGACY vvvv (got replaced with $req feature)
87 } elsif ($handler eq 'request') {
88 $stash->add_symbol('&handle_request_matches',sub {
89 0 ▶ my ( $self, $request ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
90 0 $class->request_symbols($stash,$request);
91 0 my @result;
92 0 for ($request) {
93 0 @result = $code->($_);
94 }
95 0 $class->reset_request_symbols($stash);
96 0 ▶ return @result ? $result_handler->($self,@result) : ();
Branch: 1 branch
BranchTrueFalseTotal
@result ? : 0 0 0
97 0 });
98 # ^^^^
99 ▶ } elsif ($handler eq 'remainder' || $handler eq 'remainder_lc') {
Condition66%2/3
$handler eq "remainder" or $handler eq "remainder_lc"
l!l&&r!l&&!r
1105
MC/DC50%1/2
$handler eq "remainder" or $handler eq "remainder_lc"
$handler eq "remainder" $handler eq "remainder_lc"
Truth table66%2/3
A or B
A= $handler eq "remainder"B= $handler eq "remainder_lc"
ABresult
1X1
011
000
100 11 ▶ croak "You must be using words matching for remainder handler" unless $target->triggers_block_type eq 'Words';
Branch: 1 branch
BranchTrueFalseTotal
unless $target->triggers_block_type eq "Words" 1 10 11
101 $stash->add_symbol('&handle_request_matches',sub {
102 24 ▶ my ( $self, $request, $from_pos, $to_pos ) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.0
103 24 $class->request_symbols($stash,$request);
104 24 my $remainder = $request->generate_remainder($from_pos,$to_pos);
105 24 my @result;
106 24 ▶ for ($handler eq 'remainder' ? $remainder : lc($remainder)) {
Branch: 1 branch
BranchTrueFalseTotal
$handler eq 'remainder' ? : 24 0 24
107 24 @result = $code->($_);
108 }
109 24 $class->reset_request_symbols($stash);
110 24 ▶ return @result ? $result_handler->($self, @result) : ();
Branch: 1 branch
BranchTrueFalseTotal
@result ? : 24 0 24
111 10 });
112 } elsif ($handler eq 'matches') {
113 5 ▶ croak "You must be using regexps matching for matches handler" unless $target->triggers_block_type eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless $target->triggers_block_type eq "Regexp" 1 4 5
114 $stash->add_symbol('&handle_request_matches',sub {
115 4 ▶ my ( $self, $request, @matches ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
116 4 $class->request_symbols($stash,$request);
117 4 my @result;
118 4 my $default = $request->query_raw;
119 4 for ($default) {
120 4 @result = $code->(@matches);
121 }
122 4 $class->reset_request_symbols($stash);
123 4 ▶ return @result ? $result_handler->($self,@result) : ();
Branch: 1 branch
BranchTrueFalseTotal
@result ? : 4 0 4
124 4 });
125 } elsif ($handler eq 'all') {
126 $stash->add_symbol('&handle_request_matches',sub {
127 0 ▶ my ( $self, $request, @matches_or_pos ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
128 0 $class->request_symbols($stash,$request);
129 0 my @result = $code->($self,$request,\@matches_or_pos);
130 0 $class->reset_request_symbols($stash);
131 0 ▶ return @result ? $result_handler->($self,@result) : ();
Branch: 1 branch
BranchTrueFalseTotal
@result ? : 0 0 0
132 0 });
133 } else {
134 0 croak "I dont know how to handle ".$handler."!";
135 }
136  
137 #
138 # apply role
139 #
140  
141 24 ▶ Moo::Role->apply_role_to_package($target,$role) if $role;
Branch: 1 branch
BranchTrueFalseTotal
if $role 24 0 24
142 35 });
143 }
144  
145  
146 sub request_symbols {
147 45 ▶ my ( $class, $stash, $request ) = @_;
Subroutine request_symbols: called CC=3 SCAR=11.0
Pod documented
148 45 $stash->add_symbol('$req',\$request);
149  
150 45 $stash->add_symbol('$has_loc',$request->has_location);
151 45 ▶ if ($request->has_location) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->has_location) { } 17 28 45
152 17 $stash->add_symbol('$loc',\$request->location);
153 } else {
154 28 $stash->add_symbol('$loc',\DDG::Location->new);
155 }
156  
157 45 $stash->add_symbol('$has_lang',$request->has_language);
158 45 ▶ if ($request->has_language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->has_language) { } 16 29 45
159 16 $stash->add_symbol('$lang',\$request->language);
160 } else {
161 29 $stash->add_symbol('$lang',\DDG::Language->new);
162 }
163  
164 # $stash->add_symbol('$has_reg',$request->has_region);
165 # if ($request->has_region) {
166 # $stash->add_symbol('$reg',\$request->region);
167 # } else {
168 # $stash->add_symbol('$reg','');
169 # }
170  
171 }
172  
173  
174 sub reset_request_symbols {
175 80 ▶ my ( $class, $stash ) = @_;
Subroutine reset_request_symbols: called CC=1 SCAR=0.0
Pod documented
176 80 $stash->add_symbol('$req',undef);
177  
178 80 $stash->add_symbol('$has_loc',undef);
179 80 $stash->add_symbol('$loc',undef);
180  
181 80 $stash->add_symbol('$has_lang',undef);
182 80 $stash->add_symbol('$lang',undef);
183  
184 # $stash->add_symbol('$has_reg',undef);
185 # $stash->add_symbol('$reg',undef);
186 }
187  
188 11 1;
189  
190 __END__
« DDG/Rewrite.pm ↑ Summary DDG/Location.pm »