| 1 | # | ||||||||||||||||||||||||||||||||
| 2 | # This file is part of Dancer-Plugin-Params-Normalization | ||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2011 by Damien "dams" Krotkine. | ||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||
| 9 | package Dancer::Plugin::Params::Normalization; | ||||||||||||||||||||||||||||||||
| 10 | { | ||||||||||||||||||||||||||||||||
| 11 | 11 | $Dancer::Plugin::Params::Normalization::VERSION = '0.52'; | |||||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | # ABSTRACT: A plugin for normalizing query parameters in Dancer | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 11 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 11 | my $conf = plugin_setting; | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | # method that does nothing. It's optimized to nothing at compile time | ||||||||||||||||||||||||||||||||
| 22 | 11 | ▶ | my $void = sub(){}; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | # set the params_filter | ||||||||||||||||||||||||||||||||
| 25 | 11 | my $params_filter = sub () { 1; }; | |||||||||||||||||||||||||||||||
| 26 | 11 | ▶ | if (defined $conf->{params_filter}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | 1 | my $re = $conf->{params_filter}; | |||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | $params_filter = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | 2 | return scalar($_[0] =~ /$re/) }; | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | # method that loops on a hashref and apply a given method on its keys | ||||||||||||||||||||||||||||||||
| 33 | 14 | ▶ | my $apply_on_keys = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.1
| |||||||||||||||||||||||||||||||||
| 34 | 14 | my ($h, $func) = @_; | |||||||||||||||||||||||||||||||
| 35 | 14 | my $new_h = {}; | |||||||||||||||||||||||||||||||
| 36 | 14 | ▶ | while (my ($k, $v) = each (%$h)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 8 | ▶ | my $new_k = $params_filter->($k) ? $func->($k) : $k; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | 8 | ▶ | exists $new_h->{$new_k} && ! ($conf->{no_conflict_warn} || 0) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
exists $new_h->{$new_k} and not $conf->{"no_conflict_warn"} || 0
$conf->{'no_conflict_warn'} || 0
MC/DC0%0/2
exists $new_h->{$new_k} and not $conf->{"no_conflict_warn"} || 0
exists $new_h->{$new_k}
$conf->{'no_conflict_warn'}
Truth table33%1/3
A and not(B)
A= exists $new_h->{$new_k}B= $conf->{'no_conflict_warn'}
| |||||||||||||||||||||||||||||||||
| 39 | and warn "paramater names conflict while doing normalization of parameters '$k' : it produces '$new_k', which alreay exists."; | ||||||||||||||||||||||||||||||||
| 40 | 8 | $new_h->{$new_k} = $v; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | 14 | return $new_h; | |||||||||||||||||||||||||||||||
| 43 | 11 | }; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | # default normalization method is passthrough (do nothing) | ||||||||||||||||||||||||||||||||
| 47 | 11 | my $normalization_fonction = $void; | |||||||||||||||||||||||||||||||
| 48 | 11 | ▶ | if (defined $conf->{method} && $conf->{method} ne 'passthrough') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $conf->{"method"} and $conf->{"method"} ne "passthrough"
MC/DC50%1/2
defined $conf->{"method"} and $conf->{"method"} ne "passthrough"
defined $conf->{"method"}
$conf->{"method"} ne "passthrough"
Truth table66%2/3
A and B
A= defined $conf->{"method"}B= $conf->{"method"} ne "passthrough"
| |||||||||||||||||||||||||||||||||
| 49 | 9 | my $method; | |||||||||||||||||||||||||||||||
| 50 | 9 | ▶ | if ($conf->{method} eq 'lowercase') { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 51 | 10 | ▶ | $method = sub { my ($h) = @_; $apply_on_keys->($h, sub { lc($_[0]) } ) }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 52 | } elsif ($conf->{method} eq 'uppercase') { | ||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | $method = sub { my ($h) = @_; $apply_on_keys->($h, sub { uc($_[0]) } ) }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 54 | } elsif ($conf->{method} eq 'ucfirst') { | ||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | $method = sub { my ($h) = @_; $apply_on_keys->($h, sub { ucfirst($_[0]) } ) }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||||||
| 57 | 3 | my $class = $conf->{method}; | |||||||||||||||||||||||||||||||
| 58 | 3 | my $class_name = $class; | |||||||||||||||||||||||||||||||
| 59 | 3 | $class_name =~ s!::|'!/!g; | |||||||||||||||||||||||||||||||
| 60 | 3 | $class_name .= '.pm'; | |||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | if ( ! $class->can('new') ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | 1 | eval { require $class_name }; | |||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | $@ and die "error while requiring custom normalization class '$class' : $@"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | 3 | my $abstract_classname = __PACKAGE__ . '::Abstract'; | |||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | $class->isa(__PACKAGE__ . '::Abstract') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | or die "custom normalization class '$class' doesn't inherit from '$abstract_classname'"; | ||||||||||||||||||||||||||||||||
| 68 | 3 | my $instance = $class->new(); | |||||||||||||||||||||||||||||||
| 69 | # using a custom normalization is incompatible with params filters | ||||||||||||||||||||||||||||||||
| 70 | defined $conf->{params_filter} | ||||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | and die "your configuration contains a 'params_filter' fields, and a custom 'method' normalization class name. The two fields are incompatible"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | # todo : use *method = \&{$class->normalize} or somethin' | ||||||||||||||||||||||||||||||||
| 73 | 10 | ▶ | $method = sub { $instance->normalize($_[0]) }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 9 | my $params_types = $conf->{params_types}; | |||||||||||||||||||||||||||||||
| 77 | # default value | ||||||||||||||||||||||||||||||||
| 78 | 9 | ▶ | defined $params_types | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | or $params_types = [ qw(query body) ]; | ||||||||||||||||||||||||||||||||
| 80 | 9 | ▶ | ref $params_types eq 'ARRAY' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | or die "configuration field 'params_types' should be an array"; | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 9 | my %params_types = map { $_ => 1 } @$params_types; | |||||||||||||||||||||||||||||||
| 84 | 9 | my $params_type_query = delete $params_types{query}; | |||||||||||||||||||||||||||||||
| 85 | 9 | my $params_type_body = delete $params_types{body}; | |||||||||||||||||||||||||||||||
| 86 | 9 | my $params_type_route = delete $params_types{route}; | |||||||||||||||||||||||||||||||
| 87 | 9 | ▶ | keys %params_types | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | and die "your configuration contains '" . join(', ', keys %params_types) . | ||||||||||||||||||||||||||||||||
| 89 | "' as 'params_types' field(s), but only these are allowed : 'query', 'body', 'route'"; | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 13 | ▶ | $normalization_fonction = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 92 | my ($new_query_params, | ||||||||||||||||||||||||||||||||
| 93 | $new_body_params, | ||||||||||||||||||||||||||||||||
| 94 | 13 | $new_route_params) = map { scalar(params($_)) } qw(query body route); | |||||||||||||||||||||||||||||||
| 95 | 13 | ▶ | $params_type_query and $new_query_params = $method->($new_query_params); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 13 | ▶ | $params_type_body and $new_body_params = $method->($new_body_params); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | 13 | ▶ | $params_type_route and $new_route_params = $method->($new_route_params); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 13 | request->{params} = {}; | |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 13 | request->_set_query_params($new_query_params); | |||||||||||||||||||||||||||||||
| 102 | 13 | request->_set_body_params($new_body_params); | |||||||||||||||||||||||||||||||
| 103 | 13 | request->_set_route_params($new_route_params); | |||||||||||||||||||||||||||||||
| 104 | 9 | }; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 11 | ▶ | if (defined $conf->{general_rule}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | $conf->{general_rule} =~ /^always$|^ondemand$/ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 109 | or die 'configuration field general_rule must be one of : always, ondemand'; | ||||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | if ($conf->{general_rule} eq 'ondemand') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 1 | ▶ | register normalize => sub{ $normalization_fonction->() }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 112 | } else { | ||||||||||||||||||||||||||||||||
| 113 | 0 | hook before => $normalization_fonction; | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||||||
| 116 | 10 | hook before => $normalization_fonction; | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 11 | register_plugin; | |||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 11 | 1; | |||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||||||