Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

big cleanup, fix tests

  • Loading branch information...
commit 1dbbcb4b71c7eccbd8593885689db154792fadba 1 parent a6f734b
@dams authored
View
3  CHANGES → Changes
@@ -1,4 +1,5 @@
-
+{{$NEXT}}
+ * big cleanup, use dist zilla, fix tests.
Version 0.1
* Support for lowercase, uppercase and generic transformation (via a closure)
Version 0.2
View
13 MANIFEST
@@ -1,13 +0,0 @@
-CHANGES
-lib/Dancer/Plugin/Params/Normalization.pm
-Makefile.PL
-MANIFEST This list of files
-README
-t/01_base.t
-t/02_test_lowercase.t
-t/03_test_uppercase.t
-t/04-ucfirst.t
-t/05-filtering.t
-t/06-types.t
-t/07-ondemand.t
-t/TestUtils.pm
View
8 MANIFEST.SKIP
@@ -1,8 +0,0 @@
-t\/logs\/.*log
-\.git
-blib
-pm_to_blib
-\.old$
-^Makefile$
-MANIFEST.bak
-MANIFEST.SKIP
View
6 README
@@ -1,6 +0,0 @@
-Dancer::Plugin::Params::Normalization
-
-A Dancer plugin to normalize query parameters, like converting to lowercase,
-uppercase, or any other transformation.
-
-
View
192 README.pod
@@ -0,0 +1,192 @@
+=pod
+
+=head1 NAME
+
+Dancer::Plugin::Params::Normalization - A plugin for normalizing query parameters in Dancer.
+
+=head1 SYNOPSYS
+
+In configuration file :
+
+ plugins:
+ Params::Normalization:
+ method: lowercase
+
+In your Dancer App :
+
+ package MyWebService;
+
+ use Dancer;
+ use Dancer::Plugin::Params::Normalization;
+
+ get '/hello' => sub {
+ 'Hello ' . params->{name};
+ };
+
+Requests
+
+ # This will work, as NAME will be lowercased to name
+ curl http://mywebservice/test?NAME=John
+
+=head1 DESCRIPTION
+
+This plugin helps you normalize the query parameters in Dancer.
+
+=head1 CONFIGURATION
+
+The behaviour of this plugin is primarily setup in the configuration file, in
+your main config.yml or environment config file.
+
+ # Example 1 : always lowercase all parameters
+ plugins:
+ Params::Normalization:
+ method: lowercase
+
+ # Example 1 : always uppercase all parameters
+ plugins:
+ Params::Normalization:
+ method: uppercase
+
+ # Example 1 : on-demand uppercase parameters that starts with 'a'
+ plugins:
+ Params::Normalization:
+ general_rule: ondemand
+ method: uppercase
+ params_filter: ^[aA]
+
+Here is a list of configuration fields:
+
+=head2 general_rule
+
+This field specifies if the normalization should always happen, or on demand.
+
+Value can be of:
+
+=over
+
+=item always
+
+Parameters will be normalized behind the scene, automatically.
+
+=item ondemand
+
+Parameters are not normalized by default. The code in the route definition
+needs to call normalize_params to have the parameters normalized
+
+=back
+
+B<Default value>: C<always>
+
+=head2 method
+
+This field specifies what kind of normalization to do.
+
+Value can be of:
+
+=over
+
+=item lowercase
+
+parameters names are lowercased
+
+=item uppercase
+
+parameters names are uppercased
+
+=item ucfirst
+
+parameters names are ucfirst'ed
+
+=item Custom::Class::Name
+
+Used to execute a custom normalization method.
+
+The given class should inherit
+L<Dancer::Plugin::Params::Normalization::Abstract> and implement the method
+C<normalize>. this method takes in argument a hashref of the parameters, and
+returns a hashrefs of the normalized parameters. It can have an C<init> method
+if it requires initialization.
+
+Using a custom normalization is incompatible with C<params_filter> (see below).
+
+=item passthrough
+
+Doesn't do any normalization. Useful to disable the normalization without to
+change the code
+
+=back
+
+B<Default value>: C<passthrough>
+
+=head2 params_types
+
+Optional, used to specify on which parameters types the normalization should
+apply. The value is an array, that can contain any combination of these
+strings:
+
+=over
+
+=item query
+
+If present in the array, the parameters from the query string will be normalized
+
+=item body
+
+If present in the array, the parameters from the request's body will be normalized
+
+=item route
+
+If present in the array, the parameters from the route definition will be normalized
+
+=back
+
+B<Default value>: [ 'query', 'body']
+
+=head2 params_filter
+
+Optional, used to filters which parameters the normalization should apply to.
+
+The value is a regexp string that will be evaluated against the parameter names.
+
+=head2 no_conflict_warn
+
+Optional, if set to a true value, the plugin won't issue a warning when parameters name
+conflict happens. See L<PARAMETERS NAMES CONFLICT>.
+
+=head1 KEYWORDS
+
+=head2 normalize
+
+The general usage of this plugin is to enable normalization automatically in the configuration.
+
+However, If the configuration field C<general_rule> is set to C<ondemand>, then
+the normalization doesn't happen automatically. The C<normalize> keyword can
+then be used to normalize the parameters on demand.
+
+All you have to do is add
+
+ normalize;
+
+to your route code
+
+=head1 PARAMETERS NAMES CONFLICT
+
+if two normalized parameters names clash, a warning is issued. Example, if
+while lowercasing parameters the route receives two params : C<param> and
+C<Param>, they will be both normalized to C<param>, which leads to a conflict.
+You can avoid the warning being issued by adding the configuration key
+C<no_conflict_warn> to a true value.
+
+=head1 LICENCE
+
+This module is released under the same terms as Perl itself.
+
+=head1 AUTHORS
+
+This module has been written by Damien Krotkine <dams@cpan.org>.
+
+=head1 SEE ALSO
+
+L<Dancer>
+
+=cut
View
71 dist.ini
@@ -0,0 +1,71 @@
+name = Dancer-Plugin-Params-Normalization
+author = Damien "dams" Krotkine
+license = Perl_5
+copyright_holder = Damien "dams" Krotkine
+copyright_year = 2011
+
+main_module = lib/Dancer/Plugin/Params/Normalization.pm
+
+; -- static meta-information
+[MetaResources]
+homepage = http://search.cpan.org/dist/Dancer-Plugin-Params-Normalization
+repository = http://github.com/dams/Dancer-Plugin-Params-Normalization
+
+; -- fetch & generate files
+[GatherDir]
+[PruneFiles]
+; don't include the Makefile.PL as it's to be used only by people building from
+; the source (github), not cpan
+filenames = Makefile.PL
+
+; -- tests
+[Test::Compile]
+[MetaTests]
+[PodCoverageTests]
+[ExtraTests]
+
+; -- remove files
+[PruneCruft]
+[ManifestSkip]
+
+; -- get prereqs
+[AutoPrereqs]
+skip = ^(TestUtils)
+
+[Prereqs]
+Dancer = 1.3001
+
+; -- munge files
+[NextRelease]
+[PkgVersion]
+[PodWeaver]
+[Prepender]
+copyright = 1
+
+; -- dynamic meta information
+; [InstallDirs]
+[MetaProvides::Package]
+
+
+; -- generate meta/build files
+[License]
+[MakeMaker]
+[ModuleBuild]
+[MetaYAML]
+[Readme]
+[Manifest] ; should come last
+
+; -- release
+[CheckChangeLog]
+[Git::NextVersion]
+first_version = 0.50
+[Git::Tag]
+[Git::Check]
+[Git::Commit]
+[UploadToCPAN]
+[Git::Push]
+[Twitter]
+hash-tags = #perl #dancer #plugin #cpan #release
+tweet_url = http://metacpan.org/search?q={{$DIST}}
+tweet = Just released {{$DIST}}-{{$VERSION}} on CPAN : {{$URL}}
+
View
21 lib/Dancer/Plugin/Params/Normalization.pm
@@ -1,11 +1,10 @@
package Dancer::Plugin::Params::Normalization;
+# ABSTRACT: A plugin for normalizing query parameters in Dancer
+
use Dancer ':syntax';
use Dancer::Plugin;
-our $AUTHORITY = 'DAMS';
-our $VERSION = '0.4';
-
my $conf = plugin_setting;
# method that does nothing. It's optimized to nothing at compile time
@@ -100,10 +99,10 @@ if (defined $conf->{general_rule}) {
if ($conf->{general_rule} eq 'ondemand') {
register normalize => sub{ $normalization_fonction->() };
} else {
- before $normalization_fonction;
+ hook before => $normalization_fonction;
}
} else {
- before $normalization_fonction;
+ hook before => $normalization_fonction;
}
register_plugin;
@@ -113,10 +112,6 @@ __END__
=pod
-=head1 NAME
-
-Dancer::Plugin::Params::Normalization - A plugin for normalizing query parameters in Dancer.
-
=head1 SYNOPSYS
In configuration file :
@@ -290,14 +285,6 @@ C<Param>, they will be both normalized to C<param>, which leads to a conflict.
You can avoid the warning being issued by adding the configuration key
C<no_conflict_warn> to a true value.
-=head1 LICENCE
-
-This module is released under the same terms as Perl itself.
-
-=head1 AUTHORS
-
-This module has been written by Damien Krotkine <dams@cpan.org>.
-
=head1 SEE ALSO
L<Dancer>
View
26 lib/Dancer/Plugin/Params/Normalization/Abstract.pm
@@ -2,6 +2,8 @@ package Dancer::Plugin::Params::Normalization::Abstract;
use strict;
use warnings;
+# ABSTRACT: class for custom parameters normalization
+
use base 'Dancer::Engine';
# args: ($class)
@@ -28,7 +30,7 @@ __END__
=head1 NAME
-L<Dancer::Plugin::Params::Normalization::Abstract> - abstract class for custom parameters normalization
+Dancer::Plugin::Params::Normalization::Abstract - abstract class for custom parameters normalization
=head1 DESCRIPTION
@@ -37,37 +39,25 @@ L<Dancer::Plugin::Params::Normalization>. This is done in order to allow
custom transformation of the parameters with a common interface.
Any custom normalization package must inherits from
-L<Dancer::Plugin::Params::Normalization::Abstract> and implement the following
+Dancer::Plugin::Params::Normalization::Abstract and implement the following
abstract methods.
-=head2 Abstract Methods
-
-=over
+=head1 Abstract Methods
-=item B<init()>
+=method init()
Is called once, on initialization of the class. Can be used to create needed
initialization objects, like a database connection, etc.
-=item B<normalize($hashref)>
+=method normalize($hashref)
Receives a hashref that contains the parameters keys/value. It should return a
hashref (it can be the same), containing modified parameters.
-=back
-
-=head2 Inherited Methods
+=head1 Inherited Methods
None for now.
-=head1 LICENCE
-
-This module is released under the same terms as Perl itself.
-
-=head1 AUTHORS
-
-This module has been written by Damien Krotkine <dams@cpan.org>.
-
=head1 SEE ALSO
L<Dancer>, L<Dancer::Engine>
View
8 t/02_test_lowercase.t
@@ -1,7 +1,8 @@
use strict;
use warnings;
-use lib qw(../lib);
+
use Test::More;
+use Dancer::Test;
plan tests => 1;
@@ -23,9 +24,6 @@ plan tests => 1;
};
}
-use lib 't';
-use TestUtils;
-
# test lowercasing
-my $response = get_response_for_request(GET => '/foo', { TEST => 5 });
+my $response = dancer_response GET => '/foo', { params => {TEST => 5 } };
is($response->{content}, 5);
View
8 t/03_test_uppercase.t
@@ -1,7 +1,8 @@
use strict;
use warnings;
-use lib qw(../lib);
+
use Test::More;
+use Dancer::Test;
plan tests => 1;
@@ -23,8 +24,5 @@ plan tests => 1;
};
}
-use lib 't';
-use TestUtils;
-
-my $response = get_response_for_request(GET => '/foo', { test => 5 });
+my $response = dancer_response GET => '/foo', { params => {test => 5 } };
is($response->{content}, 5);
View
8 t/04_ucfirst.t
@@ -1,7 +1,8 @@
use strict;
use warnings;
-use lib qw(../lib);
+
use Test::More;
+use Dancer::Test;
plan tests => 1;
@@ -23,8 +24,5 @@ plan tests => 1;
};
}
-use lib 't';
-use TestUtils;
-
-my $response = get_response_for_request(GET => '/foo', { teSt => 5 });
+my $response = dancer_response GET => '/foo', { params => {teSt => 5 } };
is($response->{content}, 5);
View
10 t/05_filtering.t
@@ -1,7 +1,8 @@
use strict;
use warnings;
-use lib qw(../lib);
+
use Test::More;
+use Dancer::Test;
plan tests => 2;
@@ -24,14 +25,11 @@ plan tests => 2;
};
}
-use lib 't';
-use TestUtils;
-
# param filter regexp doesn't match, thus the param is no lowercased
-my $response = get_response_for_request(GET => '/foo/test', { TEST => 5 });
+my $response = dancer_response GET => '/foo/test', { params => {TEST => 5 } };
ok(! length $response->{content});
# param filter regexp matches, thus the param is no lowercased
-$response = get_response_for_request(GET => '/foo/mytest', { MYTEST => 5 });
+$response = dancer_response GET => '/foo/mytest', { params => {MYTEST => 5 } };
is($response->{content}, 5);
View
11 t/06_types.t
@@ -1,7 +1,8 @@
use strict;
use warnings;
-use lib qw(../lib);
+
use Test::More;
+use Dancer::Test;
plan tests => 2;
@@ -26,14 +27,12 @@ plan tests => 2;
};
}
-use lib 't';
-use TestUtils;
-
# only route params are lowercase'd
-my $response = get_response_for_request(GET => '/foo/test', { TEST => 5 });
+my $response = dancer_response GET => '/foo/test', { params => {TEST => 5 } };
ok(! length $response->{content});
+
# route param (:NAME) is lowercased to 'name', and returns 'plop'
-$response = get_response_for_request(GET => '/foo/plop', { plop => 5});
+$response = dancer_response GET => '/foo/plop', { params => { plop => 5 } };
is($response->{content}, 5);
View
10 t/07_ondemand.t
@@ -1,7 +1,8 @@
use strict;
use warnings;
-use lib qw(../lib);
+
use Test::More;
+use Dancer::Test;
plan tests => 2;
@@ -33,14 +34,11 @@ plan tests => 2;
}
-use lib 't';
-use TestUtils;
-
# this route doesn't do parameters normalization
-my $response = get_response_for_request(GET => '/foo', { TEST => 5 });
+my $response = dancer_response GET => '/foo', { params => {TEST => 5 } };
ok(! length $response->{content});
# this route does parameters normalization
-$response = get_response_for_request(GET => '/bar', { TEST => 5});
+$response = dancer_response GET => '/bar', { params => {TEST => 5 } };
is($response->{content}, 5);
View
10 t/08_custom_normalization.t
@@ -1,7 +1,8 @@
use strict;
use warnings;
-use lib qw(../lib);
+
use Test::More;
+use Dancer::Test;
{
package My::Normalization1;
@@ -45,15 +46,12 @@ plan tests => 2;
}
-use lib 't';
-use TestUtils;
-
# 'testing' should be shortened to 'tes'
-my $response = get_response_for_request(GET => '/foo', { testing => 5 });
+my $response = dancer_response GET => '/foo', { params => {testing => 5 } };
is($response->{content}, 5);
# 'ABCLONGNAME' should be shortened to 'ABC'
-$response = get_response_for_request(GET => '/bar', { ABCLONGNAME => 6});
+$response = dancer_response GET => '/bar', { params => { ABCLONGNAME => 6 } };
is($response->{content}, 6);
View
13 t/09_custom_normalization2.t
@@ -1,8 +1,11 @@
use strict;
use warnings;
-use lib qw(../lib);
+
+use FindBin;
+use lib "$FindBin::Bin/../t";
+
use Test::More;
-use lib 't';
+use Dancer::Test;
plan tests => 2;
@@ -32,12 +35,10 @@ plan tests => 2;
}
-use TestUtils;
-
# 'testing' should be shortened to 'ing'
-my $response = get_response_for_request(GET => '/foo', { testing => 5 });
+my $response = dancer_response GET => '/foo', { params => {testing => 5 } };
is($response->{content}, 5);
# 'ABCLONGNAME' should be shortened to 'AME'
-$response = get_response_for_request(GET => '/bar', { ABCLONGNAME => 6});
+$response = dancer_response GET => '/bar', { params => { ABCLONGNAME => 6 } };
is($response->{content}, 6);
View
29 t/TestUtils.pm
@@ -1,29 +0,0 @@
-package TestUtils;
-
-use base 'Exporter';
-use vars '@EXPORT';
-
-use File::Path qw(mkpath rmtree);
-use Dancer::Request;
-use Dancer::Config 'setting';
-
-@EXPORT =
- qw(fake_request get_response_for_request);
-
-sub fake_request($$;$) {
- my ($method, $path, $params) = @_;
- my $req = Dancer::Request->new_for_request($method => $path);
- if ($params) {
- $req->_set_body_params($params);
- }
- return $req;
-}
-
-sub get_response_for_request {
- my ($method, $path, $params) = @_;
- my $request = fake_request($method => $path, $params);
- Dancer::SharedData->request($request);
- Dancer::Renderer::get_action_response();
-}
-
-1;
Please sign in to comment.
Something went wrong with that request. Please try again.