| 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
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
exists $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map} and defined $Lim::RPC::URIMaps::_MAP_CACHE_CODE{$map}
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}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | Lim::DEBUG and $self->{logger}->debug('Regexp compilation of map "', $map, '" failed: ', $@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 119 | 0 | foreach my $predata_variable (split(/,/o, $predata)) { | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($predata_variable =~ /^([^=]+)=(.+)$/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 127 | 0 | $@ = 'Predata is not valid'; | |||||||||||||||||||||||||||
| 128 | 0 | return; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 12 | ▶ | if (scalar @variables) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 12 | $code = 'my ('; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 12 | $n = 1; | |||||||||||||||||||||||||||
| 137 | 12 | ▶ | while ($n <= scalar @variables) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 12 | ▶ | $code .= '$v'.$n.($n != scalar @variables ? ',' : ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 12 | $n++; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 12 | $code .= ')=('; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 12 | $n = 1; | |||||||||||||||||||||||||||
| 145 | 12 | ▶ | while ($n <= scalar @variables) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 12 | ▶ | $code .= '$'.$n.($n != scalar @variables ? ',' : ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | Lim::DEBUG and $self->{logger}->debug('Code generation of map "', $map, '" failed: ', $@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | Lim::DEBUG and $self->{logger}->debug('Verify code of map "', $map, '" failed: ', $@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||