line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DDG::Meta::RequestHandler; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:DDG'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Functions for a request handler |
4
|
|
|
|
|
|
|
$DDG::Meta::RequestHandler::VERSION = '1017'; |
5
|
11
|
|
|
11
|
|
81
|
use strict; |
|
11
|
|
|
|
|
24
|
|
|
11
|
|
|
|
|
252
|
|
6
|
11
|
|
|
11
|
|
45
|
use warnings; |
|
11
|
|
|
|
|
19
|
|
|
11
|
|
|
|
|
199
|
|
7
|
11
|
|
|
11
|
|
46
|
use Carp; |
|
11
|
|
|
|
|
32
|
|
|
11
|
|
|
|
|
462
|
|
8
|
11
|
|
|
11
|
|
763
|
use Package::Stash; |
|
11
|
|
|
|
|
15281
|
|
|
11
|
|
|
|
|
226
|
|
9
|
11
|
|
|
11
|
|
1351
|
use DDG::Location; |
|
11
|
|
|
|
|
29
|
|
|
11
|
|
|
|
|
281
|
|
10
|
11
|
|
|
11
|
|
1712
|
use DDG::Language; |
|
11
|
|
|
|
|
25
|
|
|
11
|
|
|
|
|
8908
|
|
11
|
|
|
|
|
|
|
require Moo::Role; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my @request_array_attributes = qw( |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
words |
29
|
|
|
|
|
|
|
query_parts |
30
|
|
|
|
|
|
|
query_raw_parts |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $default_handler = 'query_raw'; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my %applied; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub apply_keywords { |
40
|
35
|
|
|
35
|
1
|
113
|
my ( $class, $target, $result_handler, $role ) = @_; |
41
|
|
|
|
|
|
|
|
42
|
35
|
50
|
|
|
|
137
|
return if exists $applied{$target}; |
43
|
35
|
|
|
|
|
81
|
$applied{$target} = undef; |
44
|
|
|
|
|
|
|
|
45
|
35
|
|
|
|
|
185
|
my $stash = Package::Stash->new($target); |
46
|
|
|
|
|
|
|
|
47
|
35
|
|
|
|
|
134
|
$class->reset_request_symbols($stash); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$stash->add_symbol('&handle',sub { |
51
|
27
|
|
|
27
|
|
175
|
my $handler = shift; |
|
|
|
|
26
|
|
|
|
|
|
|
|
3
|
|
|
|
52
|
27
|
|
|
|
|
53
|
my $code; |
53
|
27
|
100
|
|
|
|
74
|
if (ref $handler eq 'CODE') { |
54
|
6
|
|
|
|
|
14
|
$code = $handler; |
55
|
6
|
|
|
|
|
14
|
$handler = $default_handler; |
56
|
|
|
|
|
|
|
} else { |
57
|
21
|
|
|
|
|
36
|
$code = shift; |
58
|
21
|
50
|
|
|
|
65
|
croak "We need a CODEREF for the handler" unless ref $code eq 'CODE'; |
59
|
|
|
|
|
|
|
} |
60
|
27
|
100
|
|
|
|
85
|
croak "Please define triggers before you define a handler" unless $target->has_triggers; |
61
|
|
|
|
|
|
|
|
62
|
26
|
100
|
66
|
|
|
59
|
if (grep { $_ eq $handler } @request_scalar_attributes) { |
|
182
|
100
|
|
|
|
333
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
63
|
|
|
|
|
|
|
$stash->add_symbol('&handle_request_matches',sub { |
64
|
10
|
|
|
10
|
|
27
|
my ( $self, $request ) = @_; |
|
|
|
|
7
|
|
|
|
|
|
|
|
3
|
|
|
|
65
|
10
|
|
|
|
|
62
|
$class->request_symbols($stash,$request); |
66
|
10
|
|
|
|
|
29
|
my @result; |
67
|
10
|
|
|
|
|
51
|
my $default = $request->$handler; |
68
|
10
|
|
|
|
|
27
|
for ($default) { |
69
|
10
|
|
|
|
|
43
|
@result = $code->($_); |
70
|
|
|
|
|
|
|
} |
71
|
10
|
|
|
|
|
104
|
$class->reset_request_symbols($stash); |
72
|
10
|
50
|
|
|
|
57
|
return @result ? $result_handler->($self,@result) : (); |
73
|
7
|
|
|
|
|
74
|
}); |
74
|
57
|
|
|
|
|
182
|
} elsif (grep { $_ eq $handler } @request_array_attributes) { |
75
|
|
|
|
|
|
|
$stash->add_symbol('&handle_request_matches',sub { |
76
|
7
|
|
|
7
|
|
15
|
my ( $self, $request ) = @_; |
77
|
7
|
|
|
|
|
24
|
$class->request_symbols($stash,$request); |
78
|
7
|
|
|
|
|
16
|
my @result; |
79
|
7
|
|
|
|
|
19
|
my $default = $request->$default_handler; |
80
|
7
|
|
|
|
|
16
|
for ($default) { |
81
|
7
|
|
|
|
|
12
|
@result = $code->(@{$request->$handler}); |
|
7
|
|
|
|
|
104
|
|
82
|
|
|
|
|
|
|
} |
83
|
7
|
|
|
|
|
63
|
$class->reset_request_symbols($stash); |
84
|
7
|
50
|
|
|
|
29
|
return @result ? $result_handler->($self,@result) : (); |
85
|
3
|
|
|
|
|
32
|
}); |
86
|
|
|
|
|
|
|
# LEGACY vvvv (got replaced with $req feature) |
87
|
|
|
|
|
|
|
} elsif ($handler eq 'request') { |
88
|
|
|
|
|
|
|
$stash->add_symbol('&handle_request_matches',sub { |
89
|
0
|
|
|
0
|
|
0
|
my ( $self, $request ) = @_; |
90
|
0
|
|
|
|
|
0
|
$class->request_symbols($stash,$request); |
91
|
0
|
|
|
|
|
0
|
my @result; |
92
|
0
|
|
|
|
|
0
|
for ($request) { |
93
|
0
|
|
|
|
|
0
|
@result = $code->($_); |
94
|
|
|
|
|
|
|
} |
95
|
0
|
|
|
|
|
0
|
$class->reset_request_symbols($stash); |
96
|
0
|
0
|
|
|
|
0
|
return @result ? $result_handler->($self,@result) : (); |
97
|
0
|
|
|
|
|
0
|
}); |
98
|
|
|
|
|
|
|
# ^^^^ |
99
|
|
|
|
|
|
|
} elsif ($handler eq 'remainder' || $handler eq 'remainder_lc') { |
100
|
11
|
100
|
|
|
|
35
|
croak "You must be using words matching for remainder handler" unless $target->triggers_block_type eq 'Words'; |
101
|
|
|
|
|
|
|
$stash->add_symbol('&handle_request_matches',sub { |
102
|
24
|
|
|
24
|
|
55
|
my ( $self, $request, $from_pos, $to_pos ) = @_; |
103
|
24
|
|
|
|
|
79
|
$class->request_symbols($stash,$request); |
104
|
24
|
|
|
|
|
91
|
my $remainder = $request->generate_remainder($from_pos,$to_pos); |
105
|
24
|
|
|
|
|
39
|
my @result; |
106
|
24
|
50
|
|
|
|
71
|
for ($handler eq 'remainder' ? $remainder : lc($remainder)) { |
107
|
24
|
|
|
|
|
72
|
@result = $code->($_); |
108
|
|
|
|
|
|
|
} |
109
|
24
|
|
|
|
|
145
|
$class->reset_request_symbols($stash); |
110
|
24
|
50
|
|
|
|
95
|
return @result ? $result_handler->($self, @result) : (); |
111
|
10
|
|
|
|
|
109
|
}); |
112
|
|
|
|
|
|
|
} elsif ($handler eq 'matches') { |
113
|
5
|
100
|
|
|
|
16
|
croak "You must be using regexps matching for matches handler" unless $target->triggers_block_type eq 'Regexp'; |
114
|
|
|
|
|
|
|
$stash->add_symbol('&handle_request_matches',sub { |
115
|
4
|
|
|
4
|
|
13
|
my ( $self, $request, @matches ) = @_; |
116
|
4
|
|
|
|
|
16
|
$class->request_symbols($stash,$request); |
117
|
4
|
|
|
|
|
8
|
my @result; |
118
|
4
|
|
|
|
|
15
|
my $default = $request->query_raw; |
119
|
4
|
|
|
|
|
8
|
for ($default) { |
120
|
4
|
|
|
|
|
16
|
@result = $code->(@matches); |
121
|
|
|
|
|
|
|
} |
122
|
4
|
|
|
|
|
47
|
$class->reset_request_symbols($stash); |
123
|
4
|
50
|
|
|
|
22
|
return @result ? $result_handler->($self,@result) : (); |
124
|
4
|
|
|
|
|
46
|
}); |
125
|
|
|
|
|
|
|
} elsif ($handler eq 'all') { |
126
|
|
|
|
|
|
|
$stash->add_symbol('&handle_request_matches',sub { |
127
|
0
|
|
|
0
|
|
0
|
my ( $self, $request, @matches_or_pos ) = @_; |
128
|
0
|
|
|
|
|
0
|
$class->request_symbols($stash,$request); |
129
|
0
|
|
|
|
|
0
|
my @result = $code->($self,$request,\@matches_or_pos); |
130
|
0
|
|
|
|
|
0
|
$class->reset_request_symbols($stash); |
131
|
0
|
0
|
|
|
|
0
|
return @result ? $result_handler->($self,@result) : (); |
132
|
0
|
|
|
|
|
0
|
}); |
133
|
|
|
|
|
|
|
} else { |
134
|
0
|
|
|
|
|
0
|
croak "I dont know how to handle ".$handler."!"; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# |
138
|
|
|
|
|
|
|
# apply role |
139
|
|
|
|
|
|
|
# |
140
|
|
|
|
|
|
|
|
141
|
24
|
50
|
|
|
|
140
|
Moo::Role->apply_role_to_package($target,$role) if $role; |
142
|
35
|
|
|
|
|
2914
|
}); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub request_symbols { |
147
|
45
|
|
|
45
|
1
|
95
|
my ( $class, $stash, $request ) = @_; |
148
|
45
|
|
|
|
|
292
|
$stash->add_symbol('$req',\$request); |
149
|
|
|
|
|
|
|
|
150
|
45
|
|
|
|
|
305
|
$stash->add_symbol('$has_loc',$request->has_location); |
151
|
45
|
100
|
|
|
|
197
|
if ($request->has_location) { |
152
|
17
|
|
|
|
|
70
|
$stash->add_symbol('$loc',\$request->location); |
153
|
|
|
|
|
|
|
} else { |
154
|
28
|
|
|
|
|
415
|
$stash->add_symbol('$loc',\DDG::Location->new); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
45
|
|
|
|
|
213
|
$stash->add_symbol('$has_lang',$request->has_language); |
158
|
45
|
100
|
|
|
|
138
|
if ($request->has_language) { |
159
|
16
|
|
|
|
|
77
|
$stash->add_symbol('$lang',\$request->language); |
160
|
|
|
|
|
|
|
} else { |
161
|
29
|
|
|
|
|
399
|
$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
|
|
|
80
|
1
|
165
|
my ( $class, $stash ) = @_; |
176
|
80
|
|
|
|
|
438
|
$stash->add_symbol('$req',undef); |
177
|
|
|
|
|
|
|
|
178
|
80
|
|
|
|
|
349
|
$stash->add_symbol('$has_loc',undef); |
179
|
80
|
|
|
|
|
381
|
$stash->add_symbol('$loc',undef); |
180
|
|
|
|
|
|
|
|
181
|
80
|
|
|
|
|
335
|
$stash->add_symbol('$has_lang',undef); |
182
|
80
|
|
|
|
|
326
|
$stash->add_symbol('$lang',undef); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# $stash->add_symbol('$has_reg',undef); |
185
|
|
|
|
|
|
|
# $stash->add_symbol('$reg',undef); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
1; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
__END__ |