param_checker 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +64 -0
- data/Rakefile +3 -0
- data/lib/param_checker/version.rb +1 -1
- data/lib/param_checker.rb +14 -5
- data/param_checker.gemspec +2 -2
- data/spec/lib/param_checker_spec.rb +60 -48
- data/spec/model.rb +3 -0
- data/spec/spec_helper.rb +1 -1
- metadata +8 -5
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010-2011 by Kai Schlamp
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
= ParamChecker
|
2
|
+
|
3
|
+
ParamChecker is a small library for validation and parsing of user provided parameters. It is a handy way to check GET/POST params in Ruby webframeworks (like Rails or Sinatra).
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install param_checker
|
8
|
+
|
9
|
+
or add ParamChecker to your Gemfile
|
10
|
+
|
11
|
+
gem 'param_checker'
|
12
|
+
|
13
|
+
and afterwards (when using the Gemfile) execute
|
14
|
+
|
15
|
+
bundle install
|
16
|
+
|
17
|
+
== Usage
|
18
|
+
|
19
|
+
Include the ParamChecker module where ever you like. I usually put it into my Rails +ApplicationController.rb+
|
20
|
+
|
21
|
+
class ApplicationController < ActionController::Base
|
22
|
+
include ParamChecker
|
23
|
+
end
|
24
|
+
|
25
|
+
You can then simply call +check_string_param(param_to_check, "my default", ["foo", "bar"])+ in every controller.
|
26
|
+
|
27
|
+
Instead of including the module you could also call all functions of the module directly.
|
28
|
+
|
29
|
+
ParamChecker.check_string_param(param_to_check, "my default", ["foo", "bar"])
|
30
|
+
|
31
|
+
There are currently 5 supported functions:
|
32
|
+
|
33
|
+
* +check_integer_param(param, default, min, max)+
|
34
|
+
* +check_float_param(param, default, min, max)+
|
35
|
+
* +check_string_param(param, default, allowed)+
|
36
|
+
* +check_symbol_param(param, default, allowed)+
|
37
|
+
* +check_boolean_param(param, default)+
|
38
|
+
|
39
|
+
+param+ is always the string parameter to check. +default+ is a value that is returned when +param+ does not succeed the check.
|
40
|
+
+min+ and +max+ in +check_integer_param+ and +check_float_param+ are the minimum and maximum allowed values of param. Both +min+ and +max+ options are optional. If they are not provided then no range is checked at all.
|
41
|
+
+allowed+ in +check_string_param+ and +check_symbol_param+ represent the allowed values of +param+. They can be either a string (resp. a symbol for +check_symbol_param+), a regular expression, or an array of strings (resp. symbols for +check_symbol_param+).
|
42
|
+
+check_boolean_param+ evaluates "1" or "true" string as true and "0" or "false" string to false.
|
43
|
+
All functions return the parsed and type cast value (check_integer_param returns an integer, check_symbol_param returns a symbol, and so on).
|
44
|
+
|
45
|
+
== Examples
|
46
|
+
|
47
|
+
Below are some simple examples how I use those function in my controllers.
|
48
|
+
|
49
|
+
# +max+ is not provided in this case. I just want to ensure that page is bigger than 1 and otherwise return 1.
|
50
|
+
page = check_integer_param(params[:page], 1, 1)
|
51
|
+
|
52
|
+
# Returns "name" if params[:field] is not "name" or "address"
|
53
|
+
field = check_string_param(params[:field], "name", ["name", "address"])
|
54
|
+
|
55
|
+
# Return the boolean if params[:accepted] is a valid boolean representation, and the default false otherwise.
|
56
|
+
accepted = check_boolean_param(params[:accepted], false)
|
57
|
+
|
58
|
+
== Testing
|
59
|
+
|
60
|
+
ParamChecker uses RSpec for testing and has a rake task for executing the provided specs
|
61
|
+
|
62
|
+
rake spec
|
63
|
+
|
64
|
+
Copyright © 2010-2011 Kai Schlamp (http://www.medihack.org), released under the MIT license
|
data/Rakefile
CHANGED
data/lib/param_checker.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
module ParamChecker
|
2
|
-
|
2
|
+
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def check_integer_param(param, default, min = nil, max = nil)
|
3
6
|
min_lambda = (min.nil? ? lambda { true } : lambda { param.strip.to_i >= min })
|
4
7
|
max_lambda = (max.nil? ? lambda { true } : lambda { param.strip.to_i <= max })
|
5
8
|
|
@@ -10,7 +13,7 @@ module ParamChecker
|
|
10
13
|
end
|
11
14
|
end
|
12
15
|
|
13
|
-
def
|
16
|
+
def check_float_param(param, default, min = nil, max = nil)
|
14
17
|
min_lambda = (min.nil? ? lambda { true } : lambda { param.strip.to_i >= min })
|
15
18
|
max_lambda = (max.nil? ? lambda { true } : lambda { param.strip.to_i <= max })
|
16
19
|
|
@@ -21,7 +24,7 @@ module ParamChecker
|
|
21
24
|
end
|
22
25
|
end
|
23
26
|
|
24
|
-
def
|
27
|
+
def check_string_param(param, default, allowed)
|
25
28
|
if (param && allowed.class == Regexp && param =~ allowed)
|
26
29
|
param
|
27
30
|
elsif (param && allowed.class == Array && allowed.include?(param))
|
@@ -33,7 +36,7 @@ module ParamChecker
|
|
33
36
|
end
|
34
37
|
end
|
35
38
|
|
36
|
-
def
|
39
|
+
def check_symbol_param(param, default, allowed)
|
37
40
|
begin
|
38
41
|
if (param && allowed.class == Regexp && param.to_s =~ allowed)
|
39
42
|
param.to_sym
|
@@ -49,7 +52,7 @@ module ParamChecker
|
|
49
52
|
end
|
50
53
|
end
|
51
54
|
|
52
|
-
def
|
55
|
+
def check_boolean_param(param, default)
|
53
56
|
if (param && param == "1" || param == "true")
|
54
57
|
true
|
55
58
|
elsif (param && param == "0" || param == "false")
|
@@ -58,4 +61,10 @@ module ParamChecker
|
|
58
61
|
default
|
59
62
|
end
|
60
63
|
end
|
64
|
+
|
65
|
+
public :check_integer_param,
|
66
|
+
:check_float_param,
|
67
|
+
:check_string_param,
|
68
|
+
:check_symbol_param,
|
69
|
+
:check_boolean_param
|
61
70
|
end
|
data/param_checker.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Kai Schlamp"]
|
10
10
|
s.email = ["schlamp@gmx.de"]
|
11
|
-
s.homepage = "
|
11
|
+
s.homepage = "https://github.com/medihack/param_checker"
|
12
12
|
s.summary = %q{Parameter parsing and validation}
|
13
|
-
s.description = %q{A library for parameter
|
13
|
+
s.description = %q{A library for parameter validation and parsing. A handy way to check GET/POST params in Ruby webframeworks (like Rails or Sinatra).}
|
14
14
|
|
15
15
|
s.rubyforge_project = "param_checker"
|
16
16
|
|
@@ -1,71 +1,83 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "ParamChecker" do
|
4
|
+
before(:all) do
|
5
|
+
@model = ParamCheckerModel.new
|
6
|
+
end
|
7
|
+
|
4
8
|
it "should check integer" do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
@model.check_integer_param("5", 99).should == 5
|
10
|
+
@model.check_integer_param("5", 99, 4).should == 5
|
11
|
+
@model.check_integer_param("5", 99, nil, 6).should == 5
|
12
|
+
@model.check_integer_param("5", 99, 4, 6).should == 5
|
13
|
+
@model.check_integer_param("-5", 99).should == -5
|
14
|
+
@model.check_integer_param(" 5 ", 99).should == 5
|
11
15
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
@model.check_integer_param("", 99).should == 99
|
17
|
+
@model.check_integer_param("5abc", 99).should == 99
|
18
|
+
@model.check_integer_param("5", 99, 6).should == 99
|
19
|
+
@model.check_integer_param("5", 99, nil, 4).should == 99
|
20
|
+
@model.check_integer_param("5", 99, 1, 4).should == 99
|
21
|
+
@model.check_integer_param("5", 99, 10, 1).should == 99
|
18
22
|
end
|
19
23
|
|
20
24
|
it "should check float" do
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
@model.check_float_param("5.1", 99.2).should == 5.1
|
26
|
+
@model.check_float_param("5.1", 99.2, 4.3).should == 5.1
|
27
|
+
@model.check_float_param("5.1", 99.2, nil, 6.4).should == 5.1
|
28
|
+
@model.check_float_param("5.1", 99.2, 4.3, 6.4).should == 5.1
|
29
|
+
@model.check_float_param("-5.1", 99.2).should == -5.1
|
30
|
+
@model.check_float_param(" 5.1 ", 99.2).should == 5.1
|
27
31
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
@model.check_float_param("", 99.2).should == 99.2
|
33
|
+
@model.check_float_param("5abc", 99.2).should == 99.2
|
34
|
+
@model.check_float_param("5", 99.2, 6.4).should == 99.2
|
35
|
+
@model.check_float_param("5", 99.2, nil, 4.3).should == 99.2
|
36
|
+
@model.check_float_param("5", 99.2, 1, 4.3).should == 99.2
|
37
|
+
@model.check_float_param("5", 99.2, 10.5, 1.6).should == 99.2
|
34
38
|
end
|
35
39
|
|
36
40
|
it "should check string" do
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
41
|
+
@model.check_string_param("lorem", "dolor", /.*ore.*/).should == "lorem"
|
42
|
+
@model.check_string_param("lorem", "dolor", ["lorem", "ipsum"]).should == "lorem"
|
43
|
+
@model.check_string_param("lorem", "dolor", "lorem").should == "lorem"
|
44
|
+
@model.check_string_param("", "dolor", /.*/).should == ""
|
45
|
+
@model.check_string_param("", "dolor", "").should == ""
|
42
46
|
|
43
|
-
|
44
|
-
|
45
|
-
|
47
|
+
@model.check_string_param("lorem", "dolor", /.*ips.*/).should == "dolor"
|
48
|
+
@model.check_string_param("lorem", "dolor", ["patre", "ipsum"]).should == "dolor"
|
49
|
+
@model.check_string_param("lorem", "dolor", "ipsum").should == "dolor"
|
46
50
|
end
|
47
51
|
|
48
52
|
it "should check symbol" do
|
49
|
-
|
50
|
-
|
51
|
-
|
53
|
+
@model.check_symbol_param("lorem", :dolor, /.*ore.*/).should == :lorem
|
54
|
+
@model.check_symbol_param("lorem", :dolor, ["lorem", :ipsum]).should == :lorem
|
55
|
+
@model.check_symbol_param("lorem", :dolor, :lorem).should == :lorem
|
52
56
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
@model.check_symbol_param("lorem", :dolor, /.*ips.*/).should == :dolor
|
58
|
+
@model.check_symbol_param("lorem", :dolor, ["patre", "ipsum"]).should == :dolor
|
59
|
+
@model.check_symbol_param("lorem", :dolor, "ipsum").should == :dolor
|
60
|
+
@model.check_symbol_param("lorem", "dolor", "ipsum").should == :dolor
|
61
|
+
@model.check_symbol_param("", :dolor, /.*/).should == :dolor
|
62
|
+
@model.check_symbol_param("", :dolor, "").should == :dolor
|
59
63
|
end
|
60
64
|
|
61
65
|
it "should check boolean" do
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
+
@model.check_boolean_param("1", false).should == true
|
67
|
+
@model.check_boolean_param("true", false).should == true
|
68
|
+
@model.check_boolean_param("0", true).should == false
|
69
|
+
@model.check_boolean_param("false", true).should == false
|
66
70
|
|
67
|
-
|
68
|
-
|
69
|
-
|
71
|
+
@model.check_boolean_param("3", true).should == true
|
72
|
+
@model.check_boolean_param("", true).should == true
|
73
|
+
@model.check_boolean_param("abc", true).should == true
|
74
|
+
end
|
75
|
+
|
76
|
+
it "can be called as module functions" do
|
77
|
+
ParamChecker.check_integer_param("5", 99).should == 5
|
78
|
+
ParamChecker.check_float_param("5.1", 99.2).should == 5.1
|
79
|
+
ParamChecker.check_string_param("lorem", "dolor", /.*ore.*/).should == "lorem"
|
80
|
+
ParamChecker.check_symbol_param("lorem", :dolor, /.*ore.*/).should == :lorem
|
81
|
+
ParamChecker.check_boolean_param("1", false).should == true
|
70
82
|
end
|
71
83
|
end
|
data/spec/model.rb
ADDED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: param_checker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kai Schlamp
|
@@ -34,7 +34,7 @@ dependencies:
|
|
34
34
|
version: 2.0.0
|
35
35
|
type: :development
|
36
36
|
version_requirements: *id001
|
37
|
-
description: A library for parameter
|
37
|
+
description: A library for parameter validation and parsing. A handy way to check GET/POST params in Ruby webframeworks (like Rails or Sinatra).
|
38
38
|
email:
|
39
39
|
- schlamp@gmx.de
|
40
40
|
executables: []
|
@@ -46,14 +46,17 @@ extra_rdoc_files: []
|
|
46
46
|
files:
|
47
47
|
- .gitignore
|
48
48
|
- Gemfile
|
49
|
+
- MIT-LICENSE
|
50
|
+
- README.rdoc
|
49
51
|
- Rakefile
|
50
52
|
- lib/param_checker.rb
|
51
53
|
- lib/param_checker/version.rb
|
52
54
|
- param_checker.gemspec
|
53
55
|
- spec/lib/param_checker_spec.rb
|
56
|
+
- spec/model.rb
|
54
57
|
- spec/spec_helper.rb
|
55
58
|
has_rdoc: true
|
56
|
-
homepage:
|
59
|
+
homepage: https://github.com/medihack/param_checker
|
57
60
|
licenses: []
|
58
61
|
|
59
62
|
post_install_message:
|