Lim/RPC/URIMaps.pm

filter: Statementstmt 72.9% 70 / 96 Branchbran 43.7% 21 / 48 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 63.8% 106 / 166 SCAR 43.9
CC
31
Cov
63%
add
59.6
process
20.0
new
3.2
CRAP 81.0
« Lim/RPC/Protocol/XMLRPC.pm ↑ Summary Lim/Error.pm »
1 package Lim::RPC::URIMaps;
2  
3 10 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Log::Log4perl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =encoding utf8
12  
13 =head1 NAME
14  
15 ...
16  
17 =head1 VERSION
18  
19 See L<Lim> for version.
20  
21 =cut
22  
23 10 our $VERSION = $Lim::VERSION;
24 10 our %_MAP_CACHE_CODE;
25  
26 =head1 SYNOPSIS
27  
28 ...
29  
30 =head1 SUBROUTINES/METHODS
31  
32 =head2 new
33  
34 =cut
35  
36 sub new {
37 12 ▶ my $this = shift;
Subroutine new: called CC=3 SCAR=11.6
Pod documented
38 12 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
39 12 my $self = {
40 logger => Log::Log4perl->get_logger,
41 maps => []
42 };
43 12 bless $self, $class;
44
45 12 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("new ", __PACKAGE__, " ", $self) 12 0 12
46 12 $self;
47 }
48  
49 sub DESTROY {
50 12 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=2 SCAR=7.2
51 12 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("destroy ", __PACKAGE__, " ", $self) 12 0 12
52 }
53  
54 =head2 add
55  
56 =cut
57  
58 sub add {
59 20 ▶ my ($self, $map) = @_;
Subroutine add: called CC=25 SCAR=40.9
Pod documented
60 20 my (@regexps, @variables, $regexp, $n, $code, $call, $predata);
61  
62 #
63 # See if this is a redirect call and check if we have the map in cache
64 #
65  
66 20 ▶ if ($map =~ /^(\S+)\s+=>\s+(\w+)(?:\s+(\S+))?$/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($map =~ /^(\S+)\s+=>\s+(\w+)(?:\s+(\S+))?$/ou) { } 8 12 20
elsif ($map =~ /^(\S+)\s+(\S+)$/ou) { } 0 12 12
67 8 ($map, $call, $predata) = ($1, $2, $3);
68 }
69 elsif ($map =~ /^(\S+)\s+(\S+)$/o) {
70 0 ($map, $call, $predata) = ($1, '', $2);
71 }
72 else {
73 12 $call = '';
74 }
75
76 20 ▶ if (exists $_MAP_CACHE_CODE{$map} and defined $_MAP_CACHE_CODE{$map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map} and defined $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map}) 8 12 20
Condition66%2/3
exists $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map} and defined $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map}
!ll&&!rl&&r
1208
MC/DC50%1/2
exists $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map} and defined $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map}
exists $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map} defined $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map}
Truth table66%2/3
A and B
A= exists $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map}B= defined $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map}
ABresult
0X0
100
111
77 8 push(@{$self->{maps}}, $_MAP_CACHE_CODE{$map});
78 8 return $call;
79 }
80
81 #
82 # Validate and pull out parts of the map used to generate regexp and code
83 #
84
85 12 foreach my $map_part (split(/\//o, $map)) {
86 20 ▶ if ($map_part =~ /^\w+$/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($map_part =~ /^\w+$/ou) { } 8 12 20
elsif ($map_part =~ /^((?:\w+\.)*\w+)=(.+)$/ou) { } 12 0 12
87 8 push(@regexps, $map_part);
88 }
89 elsif ($map_part =~ /^((?:\w+\.)*\w+)=(.+)$/o) {
90 12 push(@variables, $1);
91 12 push(@regexps, '('.$2.')');
92 }
93 else {
94 0 ▶ Lim::DEBUG and $self->{logger}->debug('Validation of map "', $map, '" failed');
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Validation of map \"", $map, "\" failed") 0 0 0
95 0 $@ = 'Map is not valid';
96 0 return;
97 }
98 }
99
100 #
101 # Validate the regexp made from the map by compiling it with qr
102 #
103  
104 12 $regexp = '^'.join('\/', @regexps).'$';
105 12 eval {
106 12 my $dummy = qr/$regexp/;
107 };
108 12 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 12 12
109 0 ▶ Lim::DEBUG and $self->{logger}->debug('Regexp compilation of map "', $map, '" failed: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Regexp compilation of map \"", $map, "\" failed: ", $@) 0 0 0
110 0 return;
111 }
112
113 #
114 # Generate the code that checked given URI with generated regexp and adds
115 # data gotten by the regexp to the data structure defined by the map
116 #
117
118 12 ▶ if ($predata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($predata) 0 12 12
119 0 foreach my $predata_variable (split(/,/o, $predata)) {
120 0 ▶ if ($predata_variable =~ /^([^=]+)=(.+)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($predata_variable =~ /^([^=]+)=(.+)$/ou) { } 0 0 0
121 0 my ($variable, $value) = ($1, $2);
122  
123 0 $code .= '$data->{'.join('}->{', split(/\./o, $variable)).'} = \''.$value.'\'';
124 }
125 else {
126 0 ▶ Lim::DEBUG and $self->{logger}->debug('Predata of map "', $map, '" invalid');
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Predata of map \"", $map, "\" invalid") 0 0 0
127 0 $@ = 'Predata is not valid';
128 0 return;
129 }
130 }
131 }
132  
133 12 ▶ if (scalar @variables) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @variables) { } 12 0 12
134 12 $code = 'my (';
135
136 12 $n = 1;
137 12 ▶ while ($n <= scalar @variables) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= scalar @variables) 12 12 24
138 12 ▶ $code .= '$v'.$n.($n != scalar @variables ? ',' : '');
Branch: 1 branch
BranchTrueFalseTotal
$n != scalar @variables ? : 0 12 12
139 12 $n++;
140 }
141
142 12 $code .= ')=(';
143
144 12 $n = 1;
145 12 ▶ while ($n <= scalar @variables) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= scalar @variables) 12 12 24
146 12 ▶ $code .= '$'.$n.($n != scalar @variables ? ',' : '');
Branch: 1 branch
BranchTrueFalseTotal
$n != scalar @variables ? : 0 12 12
147 12 $n++;
148 }
149
150 12 $code .= ');';
151
152 12 $n = 1;
153 12 foreach my $variable (@variables) {
154 12 $code .= '$data->{'.join('}->{', split(/\./o, $variable)).'} = $v'.($n++).';';
155 }
156 }
157 else {
158 0 $code = '';
159 }
160  
161 #
162 # Create the subroutine from the generated code
163 #
164
165 12 eval '$code = sub { my ($uri, $data)=@_; if($uri =~ /'.$regexp.'/o) { '.$code.' return \''.$call.'\';} return; };';
166 12 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 12 12
167 0 ▶ Lim::DEBUG and $self->{logger}->debug('Code generation of map "', $map, '" failed: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Code generation of map \"", $map, "\" failed: ", $@) 0 0 0
168 0 return;
169 }
170
171 #
172 # Verify code by calling it in eval
173 #
174
175 12 eval {
176 12 $code->('', {});
177 };
178 12 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 12 12
179 0 ▶ Lim::DEBUG and $self->{logger}->debug('Verify code of map "', $map, '" failed: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Verify code of map \"", $map, "\" failed: ", $@) 0 0 0
180 0 return;
181 }
182
183 #
184 # Store the generated subroutine and return success
185 #
186  
187 12 $_MAP_CACHE_CODE{$map} = $code;
188 12 weaken($_MAP_CACHE_CODE{$map});
189 12 push(@{$self->{maps}}, $code);
190 12 return $call;
191 }
192  
193 =head2 process
194  
195 =cut
196  
197 sub process {
198 0 ▶ my ($self, $uri, $data) = @_;
Subroutine process: not called CC=4 SCAR=30.0
Pod documented
199
200 0 ▶ unless (ref($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data eq "HASH") 0 0 0
201 0 confess '$data parameter is not a hash';
202 }
203
204 0 foreach my $map (@{$self->{maps}}) {
205 0 ▶ if (defined (my $ret = $map->($uri, $data))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ret = &$map($uri, $data))) 0 0 0
206 0 return $ret;
207 }
208 }
209 0 return;
210 }
211  
212 =head1 AUTHOR
213  
214 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
215  
216 =head1 BUGS
217  
218 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
219  
220 =head1 SUPPORT
221  
222 You can find documentation for this module with the perldoc command.
223  
224 perldoc Lim
225  
226 You can also look for information at:
227  
228 =over 4
229  
230 =item * Lim issue tracker (report bugs here)
231  
232 L<https://github.com/jelu/lim/issues>
233  
234 =back
235  
236 =head1 ACKNOWLEDGEMENTS
237  
238 =head1 LICENSE AND COPYRIGHT
239  
240 Copyright 2013 Jerry Lundström.
241  
242 This program is free software; you can redistribute it and/or modify it
243 under the terms of either: the GNU General Public License as published
244 by the Free Software Foundation; or the Artistic License.
245  
246 See http://dev.perl.org/licenses/ for more information.
247  
248  
249 =cut
250  
251 10 1; # End of Lim::RPC::URIMaps
« Lim/RPC/Protocol/XMLRPC.pm ↑ Summary Lim/Error.pm »