sinatra-my-params 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 517d1c8ee0d4be39d56aa775659ccb5a158c3f27f2f2e0f27abb6b80b890dbb8
4
- data.tar.gz: aa9aca42a85144fbd460016ba3abddc9fbc2d4862fe42da3f8ffda02b65d8a85
3
+ metadata.gz: a2c387628a7d4582e28e5fe2bdc905abc284cdcf81c76fc8bd2f0c3e23f254e1
4
+ data.tar.gz: e86107885442e79700caf8f293b591814798296275b5588dbe2120d80df63898
5
5
  SHA512:
6
- metadata.gz: d34af72735130ba06e1754e03f64b2179b1561ee8325deb1e22516843674fccafcb289f6f9fe902f5979b86cc5823083de996b6a063d904df5301f100f3eda1d
7
- data.tar.gz: '09fdd6bdec6704c5edd45d60c24dcca194e6ed7751cd82b1b463a29efdffe3fa8a455b8b3fc2737386477bf50308987198eeac5db7de5eae244d3aded7477762'
6
+ metadata.gz: 8c544a84df83f1e507dced93487e175f0d3b4044141f47774f59fdd8f12592070f98134a472b524983ddde905af04444da6acf359cbf8a34c14fe5e0d1eb65b5
7
+ data.tar.gz: b03d81b690692988673bdf9fbf944ca533fa770ff3e4b21f358e371579b6c0ebc6c5688d5729adc243c4e1f522737b256941cee23ee71df6964917b87c6c3a3e
data/Rakefile CHANGED
@@ -1,8 +1,13 @@
1
1
  require "rake/testtask"
2
+ require "rspec/core/rake_task"
2
3
 
3
4
  Rake::TestTask.new do |t|
4
5
  t.libs << "test"
5
6
  end
6
7
 
7
- desc "Run tests"
8
- task :default => :test
8
+ begin
9
+ RSpec::Core::RakeTask.new(:spec)
10
+
11
+ task :default => :spec
12
+ rescue LoadError
13
+ end
data/lib/permit_params.rb CHANGED
@@ -3,17 +3,20 @@ module PermitParams
3
3
  attr_accessor :param, :options
4
4
  end
5
5
 
6
+ def permitted_params(params, permitted = {}, strong_validation = false)
7
+ return params if permitted.empty?
6
8
 
7
- def permitted_params(params, permitted, strong_validation = false)
8
9
  params.select do |k,v|
9
10
  permitted.keys.map(&:to_s).include?(k.to_s) &&
10
11
  !v.nil? &&
11
- coerce(v, permitted[k.to_sym], strong_validation)
12
+ !coerce(v, permitted[k.to_sym], strong_validation).nil?
12
13
  end
13
14
  end
14
15
 
15
16
  private
16
17
 
18
+ Boolean = :boolean
19
+
17
20
  def coerce(param, type, strong = false, options = {})
18
21
  begin
19
22
  return nil if param.nil?
@@ -26,17 +29,17 @@ module PermitParams
26
29
  return DateTime.parse(param) if type == DateTime
27
30
  return Array(param.split(options[:delimiter] || ",")) if type == Array
28
31
  return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
29
- if [TrueClass, FalseClass, Boolean].include? type
30
- coerced = /^(false|f|no|n|0)$/i === param.to_s ? false : /^(true|t|yes|y|1)$/i === param.to_s ? true : nil
31
- raise ArgumentError if coerced.nil?
32
- return coerced
33
- end
32
+ return coerce_boolean(param) if [TrueClass, FalseClass, Boolean].include? type
34
33
  return nil
35
34
  rescue ArgumentError
36
- if strong
37
- raise InvalidParameterError, "'#{param}' is not a valid #{type}"
38
- end
35
+ raise InvalidParameterError, "'#{param}' is not a valid #{type}" if strong
39
36
  end
40
37
  end
38
+
39
+ def coerce_boolean(param)
40
+ coerced = /^(false|f|no|n|0)$/i === param.to_s ? false : /^(true|t|yes|y|1)$/i === param.to_s ? true : nil
41
+ raise ArgumentError if coerced.nil?
42
+ coerced
43
+ end
41
44
  end
42
45
 
@@ -0,0 +1,70 @@
1
+ require "test/unit"
2
+ require "permit_params"
3
+ require "rspec"
4
+ require "rack/test"
5
+
6
+ include PermitParams
7
+
8
+ describe "exceptions" do
9
+ it "should raise error when at least one param is invalid" do
10
+ input = { param_1: "a" }
11
+ expect{
12
+ permitted_params(input, { param_1: Integer }, true)
13
+ }.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
14
+ end
15
+
16
+ it "should return a string when a pemitted is string" do
17
+ input = { param_1: "a string" }
18
+ expect(input).to eq permitted_params(input, { param_1: String })
19
+ end
20
+
21
+ it "should return a float when a pemitted is float" do
22
+ input = { param_1: 10.0 }
23
+ expect(input).to eq permitted_params(input, { param_1: Float })
24
+ end
25
+
26
+ it "should return a date when a pemitted is date" do
27
+ input = { param_1: Date.new }
28
+ expect(input).to eq permitted_params(input, { param_1: Date })
29
+ end
30
+
31
+ it "should return a time when a pemitted is time" do
32
+ input = { param_1: Time.new }
33
+ expect(input).to eq permitted_params(input, { param_1: Time })
34
+ end
35
+
36
+ it "should return an integer when a pemitted is integer" do
37
+ input = { param_1: 1 }
38
+ expect(input).to eq permitted_params(input, { param_1: Integer })
39
+ end
40
+
41
+ it "should return an integer when a pemitted can be cast into integer" do
42
+ input = { param_1: "1" }
43
+ output = { param_1: 1 }
44
+ expect(input).to eq permitted_params(input, { param_1: Integer })
45
+ end
46
+
47
+ it "should return a false(boolean) when a pemitted is boolean" do
48
+ input = { param_1: "false" }
49
+ output = { param_1: "false" }
50
+ expect(output).to eq permitted_params(input, { param_1: Boolean })
51
+ end
52
+
53
+ it "should return a true(boolean) when a pemitted is boolean" do
54
+ input = { param_1: "true" }
55
+ output = { param_1: "true" }
56
+ expect(output).to eq permitted_params(input, { param_1: Boolean })
57
+ end
58
+
59
+ it "should remove a string when a pemitted is integer" do
60
+ input = { param_1: "a string" }
61
+ output = {}
62
+ expect(output).to eq permitted_params(input, { param_1: Integer })
63
+ end
64
+
65
+ it "should allow all params when no restriction is given" do
66
+ input = { param_1: "a string" }
67
+ expect(input).to eq permitted_params(input)
68
+ end
69
+ end
70
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-my-params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Aviles
@@ -11,7 +11,7 @@ cert_chain: []
11
11
  date: 2021-09-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple sinatra params sanitizer
14
- email: nick@quaran.to
14
+ email: gdmarav374@gmail.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
@@ -19,7 +19,7 @@ files:
19
19
  - Rakefile
20
20
  - bin/permit_params
21
21
  - lib/permit_params.rb
22
- - test/test_permit_params.rb
22
+ - spec/permit_params_spec.rb
23
23
  homepage: https://github.com/mhero/sinatra-my-params
24
24
  licenses: []
25
25
  metadata: {}
@@ -43,4 +43,4 @@ signing_key:
43
43
  specification_version: 3
44
44
  summary: permit_params!
45
45
  test_files:
46
- - test/test_permit_params.rb
46
+ - spec/permit_params_spec.rb
@@ -1,35 +0,0 @@
1
- require "test/unit"
2
- require "permit_params"
3
- require "rspec"
4
- require "rack/test"
5
-
6
- class PermitParamsTest < Test::Unit::TestCase
7
- include PermitParams
8
-
9
- def test_pemit_string
10
- input = { param_1: "a string" }
11
- output = { param_1: "a string" }
12
- assert_equal output, permitted_params( input, { param_1: String } )
13
- end
14
-
15
- def test_permit_integer
16
- input = { param_1: 1 }
17
- output = { param_1: 1 }
18
- assert_equal output, permitted_params( input, { param_1: Integer } )
19
- end
20
-
21
- def test_remove_integer
22
- input = { param_1: "a string" }
23
- output = {}
24
- assert_equal output, permitted_params( input, { param_1: Integer } )
25
- end
26
-
27
- describe "exceptions" do
28
- describe "raise" do
29
- it "should raise error when at least one param is invalid" do
30
- input = { param_1: "a" }
31
- expect(permitted_params( input, { param_1: Integer }, true )).to raise_error InvalidParameterError
32
- end
33
- end
34
- end
35
- end