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