DDG/Meta/ZeroClickInfoSpice.pm

filter: Statementstmt 80.5% 128 / 159 Branchbran 61.1% 33 / 54 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 82.1% 23 / 28 Podpod 0.0% 0 / 5 totaltotal 73.3% 184 / 251 CC 34 SCAR 19.4
CC
34
Cov
75%
__ANON__
30.0
__ANON__
12.4
create_rewrite
6.6
CRAP 7.0
« DDG/Meta/ZeroClickInfo.pm ↑ Summary DDG/Meta/ShareDir.pm »
1 package DDG::Meta::ZeroClickInfoSpice;
2 12 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: Functions for generating a L<DDG::ZeroClickInfo::Spice> factory
4 12 $DDG::Meta::ZeroClickInfoSpice::VERSION = '1017';
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use DDG::ZeroClickInfo::Spice;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use DDG::ZeroClickInfo::Spice::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use DDG::Rewrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use URI::Encode qw(uri_encode uri_decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 ▶sub zeroclickinfospice_attributes {qw(
Subroutine zeroclickinfospice_attributes: called CC=1 SCAR=0.0
Pod undocumented
16 call
17 call_type
18 caller
19 from
20 proxy_cache_valid
21 proxy_ssl_session_reuse
22 to
23 wrap_jsonp_callback
24 wrap_string_callback
25 headers
26 post_body
27 is_cached
28 is_unsafe
29 ttl
30 error_fallback
31 alt_to
32 upstream_timeouts
33 )}
34  
35 12 my %applied;
36  
37 sub apply_keywords {
38 11 ▶ my ( $class, $target ) = @_;
Subroutine apply_keywords: called CC=2 SCAR=7.5
Pod undocumented
39
40 11 ▶ return if exists $applied{$target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $applied{$target} 0 11 11
41 11 $applied{$target} = undef;
42  
43 11 my ($callback, $path, $answer_type) = @{params_from_target($target)};
44  
45 11 my %zcispice_params = (
46 caller => $target,
47 call_type => 'include',
48 call => $path,
49 wrap_jsonp_callback => 0,
50 wrap_string_callback => 0,
51 accept_header => 0,
52 upstream_timeouts => +{},
53 );
54  
55 11 my $stash = Package::Stash->new($target);
56  
57 $stash->add_symbol('&call',sub {
58 0 ▶ my %params = %zcispice_params;
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 delete $params{'from'};
60 0 delete $params{'to'};
61 0 delete $params{'wrap_jsonp_callback'};
62 0 delete $params{'wrap_string_callback'};
63 0 delete $params{'accept_header'};
64 0 delete $params{'proxy_cache_valid'};
65 0 delete $params{'proxy_ssl_session_reuse'};
66 0 delete $params{'upstream_timeouts'};
67 0 return DDG::ZeroClickInfo::Spice->new(
68 %params,
69 );
70 11 });
71  
72 $stash->add_symbol('&spice_new',sub {
73 11 ▶ shift;
Subroutine __ANON__: called CC=12 SCAR=25.2
74 11 my @call;
75 11 my %params = %zcispice_params;
76 11 my $data;
77 11 delete $params{'from'};
78 11 delete $params{'to'};
79 11 for (@_) {
80 16 ▶ if (ref $_ eq 'HASH') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 1 15 16
elsif (ref $_ eq 'DDG::ZeroClickInfo::Spice::Data') { } 3 12 15
elsif (ref $_ eq 'DDG::ZeroClickInfo::Spice') { } 0 12 12
elsif (not defined $_) { } 0 12 12
81 1 for my $k (keys %{$_}) {
82 1 $params{$k} = $_->{$k};
83 };
84 } elsif (ref $_ eq 'DDG::ZeroClickInfo::Spice::Data') {
85 3 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) { } 2 1 3
86 2 $data->add_data($_);
87 } else {
88 1 $data = $_;
89 }
90 } elsif (ref $_ eq 'DDG::ZeroClickInfo::Spice') {
91 0 return $_;
92 } elsif (!defined $_) {
93 # do nothing
94 } else {
95 12 push @call, $_;
96 }
97 }
98 11 ▶ $params{'call_data'} = $data->data if $data;
Branch: 1 branch
BranchTrueFalseTotal
if $data 1 10 11
99 11 ▶ if (@call) {
Branch: 1 branch
BranchTrueFalseTotal
if (@call) 11 0 11
100 11 ▶ if ($params{'call_type'} eq 'include') {
Branch: 2 branches
BranchTrueFalseTotal
if ($params{'call_type'} eq 'include') { } 10 1 11
elsif (scalar @call == 1) { } 1 0 1
101 10 $params{'call'} = $target->path.join('/',map { uri_encode($_,1) } @call);
102 } elsif (scalar @call == 1) {
103 1 $params{'call'} = uri_encode($call[0]);
104 } else {
105 0 croak "DDG::ZeroClickInfo::Spice can't handle more then one value in return list on non include call_type";
106 }
107 }
108 11 DDG::ZeroClickInfo::Spice->new(%params)
109 11 });
110  
111 $stash->add_symbol('&spice',sub {
112 9 ▶ if (ref $_[0] eq 'HASH') {
Subroutine __ANON__: called CC=4 SCAR=15.2
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 9 9
113 0 for (keys %{$_[0]}) {
114 0 $zcispice_params{check_zeroclickinfospice_key($_)} = $_[0]->{$_};
115 }
116 } else {
117 9 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 9 18
118 9 my $key = shift;
119 9 my $value = shift;
120 9 $zcispice_params{check_zeroclickinfospice_key($key)} = $value;
121 }
122 }
123 11 });
124  
125 11 ▶ $stash->add_symbol('&callback',sub { $callback });
Subroutine __ANON__: called CC=1 SCAR=0.0
126  
127 11 ▶ $stash->add_symbol('&path',sub { $path });
Subroutine __ANON__: called CC=1 SCAR=0.0
128  
129 11 my $spice_js;
130  
131 $stash->add_symbol('&data',sub {
132 3 ▶ unshift @_, %{$_[0]} if ref $_[0] eq 'HASH';
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 3 3
133 3 my ( %data ) = @_;
134 3 return DDG::ZeroClickInfo::Spice::Data->new( data => \%data );
135 11 });
136  
137 $stash->add_symbol('&spice_js',sub {
138 0 ▶ return $spice_js if defined $spice_js;
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
if defined $spice_js 0 0 0
139 0 my ( $self ) = @_;
140 0 $spice_js = "";
141 0 ▶ if ($target->can('module_share_dir') && (my $spice_js_file = $target->can('share')->('spice.js'))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->can("module_share_dir") and my $spice_js_file = $target->can("share")->("spice.js")) 0 0 0
Condition0%0/3
$target->can("module_share_dir") and my $spice_js_file = $target->can("share")->("spice.js")
!ll&&!rl&&r
000
MC/DC0%0/2
$target->can("module_share_dir") and my $spice_js_file = $target->can("share")->("spice.js")
$target->can("module_share_dir") my $spice_js_file = $target->can("share")->("spice.js")
Truth table0%0/3
A and B
A= $target->can("module_share_dir")B= my $spice_js_file = $target->can("share")->("spice.js")
ABresult
0X0
100
111
142 0 $spice_js .= io($spice_js_file)->slurp;
143 0 $spice_js .= "\n";
144 }
145 0 ▶ if ($target->spice_call_type eq 'self') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->spice_call_type eq "self") 0 0 0
146 0 $spice_js .= $target->callback."();";
147 }
148 0 return $spice_js;
149 11 });
150  
151 11 my $rewrite;
152 $stash->add_symbol('&has_rewrite',sub {
153 2 ▶ defined $zcispice_params{'to'};
Subroutine __ANON__: called CC=1 SCAR=0.0
154 11 });
155  
156 $stash->add_symbol('&rewrite',sub {
157 1 ▶ unless (defined $rewrite) {
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rewrite) 1 0 1
158 1 ▶ if ($target->has_rewrite) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->has_rewrite) { } 1 0 1
159 1 $rewrite = create_rewrite($callback, $path, \%zcispice_params);
160 }
161 else {
162 0 $rewrite = '';
163 }
164 }
165 1 return $rewrite;
166 11 });
167  
168 # make these accessbile, e.g. for duckpan
169 $stash->add_symbol('&alt_rewrites', sub {
170 2 ▶ my %rewrites;
Subroutine __ANON__: called CC=4 SCAR=13.9
171 # check if we have alternate end points to add
172 2 ▶ if(my $alt_to = $zcispice_params{alt_to}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $alt_to = $zcispice_params{"alt_to"}) 1 1 2
173 1 my ($base_target) = $target =~ /^(.+::)\w+$/;
174 1 ▶ while(my ($to, $params) = each %$alt_to){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$alt_to) 2 1 3
175 2 check_zeroclickinfospice_key($_) for keys %$params;
176 2 my $target = "$base_target$to";
177 2 my ($callback, $path) = @{params_from_target($target)};
178 2 $rewrites{$to} = create_rewrite($callback, $path, $params);
179 }
180 }
181  
182 2 return \%rewrites;
183 11 });
184  
185 $stash->add_symbol('&get_nginx_conf',sub {
186 2 ▶ my $nginx_conf_func = $stash->get_symbol('&nginx_conf');
Subroutine __ANON__: called CC=4 SCAR=14.0
187 2 ▶ return $nginx_conf_func->(@_) if $nginx_conf_func;
Branch: 1 branch
BranchTrueFalseTotal
if $nginx_conf_func 1 1 2
188
189 # (20151208 zt) just in case downstream can't handle undef ;-/
190 1 my $conf = '';
191 1 ▶ if($target->has_rewrite){
Branch: 1 branch
BranchTrueFalseTotal
if ($target->has_rewrite) 1 0 1
192 1 $conf = $target->rewrite->nginx_conf;
193 }
194  
195 # check if we have alternate end points to add
196 1 for my $r (values %{$target->alt_rewrites}){
197 0 $conf .= $r->nginx_conf;
198 }
199  
200 1 return $conf;
201 11 });
202  
203 ### SHOULD GET DEPRECATED vvvv ###
204 11 ▶ $stash->add_symbol('&spice_from',sub { $zcispice_params{'from'} });
Subroutine __ANON__: not called CC=1 SCAR=1.2
205 11 ▶ $stash->add_symbol('&spice_to',sub { $zcispice_params{'to'} });
Subroutine __ANON__: not called CC=1 SCAR=1.2
206 11 ▶ $stash->add_symbol('&spice_call_type',sub { $zcispice_params{'call_type'} });
Subroutine __ANON__: not called CC=1 SCAR=1.2
207 ### ^^^^ ###
208  
209 }
210  
211 sub check_zeroclickinfospice_key {
212 11 ▶ my $key = shift;
Subroutine check_zeroclickinfospice_key: called CC=2 SCAR=7.4
Pod undocumented
213 11 ▶ if (grep { $key eq $_ } zeroclickinfospice_attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$key eq $_;} zeroclickinfospice_attributes()) { } 11 0 11
214 11 return $key;
215 } else {
216 0 croak $key." is not supported on DDG::ZeroClickInfo::Spice";
217 }
218 }
219  
220 sub params_from_target {
221 15 ▶ my $target = shift;
Subroutine params_from_target: called CC=1 SCAR=0.0
Pod undocumented
222  
223 15 my @parts = split('::',$target);
224 15 my $callback = join('_',map { s/([a-z])([A-Z])/$1_$2/g; lc; } @parts);
225 15 shift @parts;
226 15 my $path = '/js/'.join('/',map { s/([a-z])([A-Z])/$1_$2/g; lc; } @parts).'/';
227 15 shift @parts;
228 15 my $answer_type = lc(join(' ',@parts));
229  
230 15 return [$callback, $path, $answer_type];
231 }
232  
233 sub create_rewrite {
234 3 ▶ my ($callback, $path, $params) = @_;
Subroutine create_rewrite: called CC=5 SCAR=18.9
Pod undocumented
235  
236 return DDG::Rewrite->new(
237 to => $params->{to},
238 defined $params->{from} ? ( from => $params->{from}) : (),
239 defined $params->{proxy_cache_valid} ? ( proxy_cache_valid => $params->{proxy_cache_valid} ) : (),
240 defined $params->{proxy_ssl_session_reuse} ? ( proxy_ssl_session_reuse => $params->{proxy_ssl_session_reuse} ) : (),
241 defined $params->{post_body} ? ( post_body => $params->{post_body} ) : (),
242 callback => $callback,
243 path => $path,
244 wrap_jsonp_callback => $params->{wrap_jsonp_callback},
245 wrap_string_callback => $params->{wrap_string_callback},
246 headers => $params->{headers},
247 error_fallback => $params->{error_fallback},
248 upstream_timeouts => $params->{upstream_timeouts},
249 3 ▶ );
Branch: 4 branches
BranchTrueFalseTotal
defined $params->{'from'} ? : 0 3 3
defined $params->{'proxy_cache_valid'} ? : 0 3 3
defined $params->{'proxy_ssl_session_reuse'} ? : 0 3 3
defined $params->{'post_body'} ? : 0 3 3
250 }
251  
252 12 1;
253  
254 __END__
« DDG/Meta/ZeroClickInfo.pm ↑ Summary DDG/Meta/ShareDir.pm »