param_checker 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -1,2 +1,5 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -1,3 +1,3 @@
1
1
  module ParamChecker
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/param_checker.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  module ParamChecker
2
- def self.check_integer_param(param, default, min = nil, max = nil)
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 self.check_float_param(param, default, min = nil, max = nil)
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 self.check_string_param(param, default, allowed)
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 self.check_symbol_param(param, default, allowed)
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 self.check_boolean_param(param, default)
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
@@ -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 = "http://www.medihack.org"
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 parsing and validation. A handy way to check user provided params in Rails.}
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
- ParamChecker.check_integer_param("5", 99).should == 5
6
- ParamChecker.check_integer_param("5", 99, 4).should == 5
7
- ParamChecker.check_integer_param("5", 99, nil, 6).should == 5
8
- ParamChecker.check_integer_param("5", 99, 4, 6).should == 5
9
- ParamChecker.check_integer_param("-5", 99).should == -5
10
- ParamChecker.check_integer_param(" 5 ", 99).should == 5
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
- ParamChecker.check_integer_param("", 99).should == 99
13
- ParamChecker.check_integer_param("5abc", 99).should == 99
14
- ParamChecker.check_integer_param("5", 99, 6).should == 99
15
- ParamChecker.check_integer_param("5", 99, nil, 4).should == 99
16
- ParamChecker.check_integer_param("5", 99, 1, 4).should == 99
17
- ParamChecker.check_integer_param("5", 99, 10, 1).should == 99
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
- ParamChecker.check_float_param("5.1", 99.2).should == 5.1
22
- ParamChecker.check_float_param("5.1", 99.2, 4.3).should == 5.1
23
- ParamChecker.check_float_param("5.1", 99.2, nil, 6.4).should == 5.1
24
- ParamChecker.check_float_param("5.1", 99.2, 4.3, 6.4).should == 5.1
25
- ParamChecker.check_float_param("-5.1", 99.2).should == -5.1
26
- ParamChecker.check_float_param(" 5.1 ", 99.2).should == 5.1
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
- ParamChecker.check_float_param("", 99.2).should == 99.2
29
- ParamChecker.check_float_param("5abc", 99.2).should == 99.2
30
- ParamChecker.check_float_param("5", 99.2, 6.4).should == 99.2
31
- ParamChecker.check_float_param("5", 99.2, nil, 4.3).should == 99.2
32
- ParamChecker.check_float_param("5", 99.2, 1, 4.3).should == 99.2
33
- ParamChecker.check_float_param("5", 99.2, 10.5, 1.6).should == 99.2
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
- ParamChecker.check_string_param("lorem", "dolor", /.*ore.*/).should == "lorem"
38
- ParamChecker.check_string_param("lorem", "dolor", ["lorem", "ipsum"]).should == "lorem"
39
- ParamChecker.check_string_param("lorem", "dolor", "lorem").should == "lorem"
40
- ParamChecker.check_string_param("", "dolor", /.*/).should == ""
41
- ParamChecker.check_string_param("", "dolor", "").should == ""
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
- ParamChecker.check_string_param("lorem", "dolor", /.*ips.*/).should == "dolor"
44
- ParamChecker.check_string_param("lorem", "dolor", ["patre", "ipsum"]).should == "dolor"
45
- ParamChecker.check_string_param("lorem", "dolor", "ipsum").should == "dolor"
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
- ParamChecker.check_symbol_param("lorem", :dolor, /.*ore.*/).should == :lorem
50
- ParamChecker.check_symbol_param("lorem", :dolor, ["lorem", :ipsum]).should == :lorem
51
- ParamChecker.check_symbol_param("lorem", :dolor, :lorem).should == :lorem
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
- ParamChecker.check_symbol_param("lorem", :dolor, /.*ips.*/).should == :dolor
54
- ParamChecker.check_symbol_param("lorem", :dolor, ["patre", "ipsum"]).should == :dolor
55
- ParamChecker.check_symbol_param("lorem", :dolor, "ipsum").should == :dolor
56
- ParamChecker.check_symbol_param("lorem", "dolor", "ipsum").should == :dolor
57
- ParamChecker.check_symbol_param("", :dolor, /.*/).should == :dolor
58
- ParamChecker.check_symbol_param("", :dolor, "").should == :dolor
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
- ParamChecker.check_boolean_param("1", false).should == true
63
- ParamChecker.check_boolean_param("true", false).should == true
64
- ParamChecker.check_boolean_param("0", true).should == false
65
- ParamChecker.check_boolean_param("false", true).should == false
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
- ParamChecker.check_boolean_param("3", true).should == true
68
- ParamChecker.check_boolean_param("", true).should == true
69
- ParamChecker.check_boolean_param("abc", true).should == true
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
@@ -0,0 +1,3 @@
1
+ class ParamCheckerModel
2
+ include ParamChecker
3
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
2
  require 'param_checker'
3
3
 
4
- #load(File.dirname(__FILE__) + '/models.rb')
4
+ load(File.dirname(__FILE__) + '/model.rb')
5
5
 
6
6
  RSpec.configure do |config|
7
7
  config.filter_run :focus => true
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: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
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 parsing and validation. A handy way to check user provided params in Rails.
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: http://www.medihack.org
59
+ homepage: https://github.com/medihack/param_checker
57
60
  licenses: []
58
61
 
59
62
  post_install_message: