sinatra-my-params 0.0.2 → 0.0.6

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: '08a02ee0bcfe3f018ef811ac5ba3c1c5b53591461a4d6022714faa9ea55d57c8'
4
- data.tar.gz: d3e4314359090e1118e7e007bf05bf9e760aaa2f43170d42dd33c7daa95714da
3
+ metadata.gz: 004ffa9a419a6ca94f76672d37e9cd242aca9f69ca1c92ada97ac135a1329abc
4
+ data.tar.gz: a0dcc3771df668d6356a4443e98cd1bcb4d1ad6e877076bbc3cc5f99e904a078
5
5
  SHA512:
6
- metadata.gz: a596f13ac286229ba0bb7c836e5f177716e9fc9ce25c86da25ee730ef36e6833bd0e2ae980d7ceeb0f8f0e56641799e287eeea17d7c0082be88fbfd690e9b3a3
7
- data.tar.gz: 7daedfd337eaa9ff6320d182690b5430f943105f98c17e2a9295c971964103ed3b931fbbdc50dbeaa1e6bb2fd75b79973c1ca76ccdc48f4f5a6535e2989357a6
6
+ metadata.gz: 04d4e285426806ac16f36f7b757eed7682d1502c272dedf7f0fd3291234e9cf3eb611c52c25a73cca3f3b29d6190ed83523cde335125599cfb92f81e2eee8c17
7
+ data.tar.gz: 861c6fc9858865ac856e66ed030f5bd8ac7005b268c3a3a2cb66deec648feec52b1c596808e6736b4495bf20e1fe7e4e818ccbfa8ebb24fe2727d77511bb13cf
data/Rakefile CHANGED
@@ -1,8 +1,13 @@
1
- require 'rake/testtask'
1
+ require "rake/testtask"
2
+ require "rspec/core/rake_task"
2
3
 
3
4
  Rake::TestTask.new do |t|
4
- t.libs << 'test'
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/bin/permit_params CHANGED
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'permit_params'
3
+ require "permit_params"
data/lib/permit_params.rb CHANGED
@@ -3,18 +3,28 @@ 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
- params.select do |k,v|
9
- permitted.keys.map(&:to_s).include?(k.to_s) &&
10
- !v.nil? &&
11
- coerce(v, permitted[k.to_sym], strong_validation)
9
+ coerced_params = Hash.new({})
10
+
11
+ params.each do |key, value|
12
+ if permitted.keys.map(&:to_s).include?(key.to_s) && !value.nil?
13
+ coerced = coerce(value, permitted[key.to_sym], strong_validation)
14
+ coerced_params[key] = coerced if !coerced.nil?
15
+ end
12
16
  end
17
+ coerced_params
13
18
  end
14
19
 
15
20
  private
16
21
 
17
- def coerce(param, type, strong = false, options = {})
22
+ Boolean = :boolean
23
+ Any = :any
24
+
25
+ def coerce(param, type, strong_validation = false, options = {})
26
+ return param if type == Any
27
+
18
28
  begin
19
29
  return nil if param.nil?
20
30
  return param if (param.is_a?(type) rescue false)
@@ -24,19 +34,27 @@ module PermitParams
24
34
  return Date.parse(param) if type == Date
25
35
  return Time.parse(param) if type == Time
26
36
  return DateTime.parse(param) if type == DateTime
27
- return Array(param.split(options[:delimiter] || ",")) if type == Array
28
- 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
37
+ return coerce_array(param) if type == Array
38
+ return coerce_hash(param) if type == Hash
39
+ return coerce_boolean(param) if [TrueClass, FalseClass, Boolean].include? type
34
40
  return nil
35
41
  rescue ArgumentError
36
- if strong
37
- raise InvalidParameterError, "'#{param}' is not a valid #{type}"
38
- end
42
+ raise InvalidParameterError, "'#{param}' is not a valid #{type}" if strong_validation
39
43
  end
40
44
  end
45
+
46
+ def coerce_array(param)
47
+ Array(param.split(options[:delimiter] || ","))
48
+ end
49
+
50
+ def coerce_hash(param)
51
+ Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}]
52
+ end
53
+
54
+ def coerce_boolean(param)
55
+ coerced = /^(false|f|no|n|0)$/i === param.to_s ? false : /^(true|t|yes|y|1)$/i === param.to_s ? true : nil
56
+ raise ArgumentError if coerced.nil?
57
+ coerced
58
+ end
41
59
  end
42
60
 
@@ -0,0 +1,118 @@
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
+ before do
10
+ class TestClass; end
11
+ end
12
+
13
+ it "should raise error when at least one param is invalid" do
14
+ input = { param_1: "a" }
15
+ expect{
16
+ permitted_params(input, { param_1: Integer }, true)
17
+ }.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
18
+ end
19
+
20
+ it "should return a string when a pemitted is string" do
21
+ input = { param_1: "a string" }
22
+ expect(input).to eq permitted_params(input, { param_1: String })
23
+ end
24
+
25
+ it "should return a float when a pemitted is float" do
26
+ input = { param_1: 10.0 }
27
+ expect(input).to eq permitted_params(input, { param_1: Float })
28
+ end
29
+
30
+ it "should return a date when a pemitted is date" do
31
+ input = { param_1: Date.new }
32
+ expect(input).to eq permitted_params(input, { param_1: Date })
33
+ end
34
+
35
+ it "should return a time when a pemitted is time" do
36
+ input = { param_1: Time.new }
37
+ expect(input).to eq permitted_params(input, { param_1: Time })
38
+ end
39
+
40
+ it "should return an integer when a pemitted is integer" do
41
+ input = { param_1: 1 }
42
+ expect(input).to eq permitted_params(input, { param_1: Integer })
43
+ end
44
+
45
+ it "should return an integer when a pemitted can be cast into integer" do
46
+ input = { param_1: "1" }
47
+ output = { param_1: 1 }
48
+ expect(output).to eq permitted_params(input, { param_1: Integer })
49
+ end
50
+
51
+ it "should return a false(boolean) when a pemitted is boolean" do
52
+ input = { param_1: "false" }
53
+ output = { param_1: false }
54
+ expect(output).to eq permitted_params(input, { param_1: Boolean })
55
+ end
56
+
57
+ it "should return a true(boolean) when a pemitted is boolean" do
58
+ input = { param_1: "true" }
59
+ output = { param_1: true }
60
+ expect(output).to eq permitted_params(input, { param_1: Boolean })
61
+ end
62
+
63
+ it "should return an array when a pemitted is array" do
64
+ input = { param_1: [1,2] }
65
+ expect(input).to eq permitted_params(input, { param_1: Array })
66
+ end
67
+
68
+ it "should return a hash when a pemitted is hash" do
69
+ input = { param_1: { a: 1 } }
70
+ expect(input).to eq permitted_params(input, { param_1: Hash })
71
+ end
72
+
73
+ it "should return a hash when a pemitted is hash" do
74
+ input = { param_1: { :a => 1 } }
75
+ expect(input).to eq permitted_params(input, { param_1: Hash })
76
+ end
77
+
78
+ it "should return a hash when a pemitted is hash" do
79
+ input = { param_1: { "a": 1 } }
80
+ expect(input).to eq permitted_params(input, { param_1: Hash })
81
+ end
82
+
83
+ it "should return a several types for several inputs" do
84
+ input = { number: 1, string: "string", bol: "true", array: [1,2], hsh: {a: 3} }
85
+ output = { number: 1, string: "string", bol: true, array: [1,2], hsh: {a: 3} }
86
+ expect(output).to eq permitted_params(
87
+ input,
88
+ {
89
+ number: Integer,
90
+ string: String,
91
+ bol: Boolean,
92
+ array: Array,
93
+ hsh: Hash
94
+ }
95
+ )
96
+ end
97
+
98
+ it "returns the paramter without casting if Any" do
99
+ input = { param_1: "1" }
100
+ output = { param_1: "1" }
101
+ expect(output).to eq permitted_params(input, { param_1: Any })
102
+
103
+ input = { param_1: TestClass.new }
104
+ expect(input).to eq permitted_params(input, { param_1: Any })
105
+ end
106
+
107
+ it "should remove a string when a pemitted is integer" do
108
+ input = { param_1: "a string" }
109
+ output = {}
110
+ expect(output).to eq permitted_params(input, { param_1: Integer })
111
+ end
112
+
113
+ it "should allow all params when no restriction is given" do
114
+ input = { param_1: "a string" }
115
+ expect(input).to eq permitted_params(input)
116
+ end
117
+ end
118
+
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.2
4
+ version: 0.0.6
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,8 +19,8 @@ files:
19
19
  - Rakefile
20
20
  - bin/permit_params
21
21
  - lib/permit_params.rb
22
- - test/test_permit_params.rb
23
- homepage: http://rubygems.org/gems/sinatra-my-params
22
+ - spec/permit_params_spec.rb
23
+ homepage: https://github.com/mhero/sinatra-my-params
24
24
  licenses: []
25
25
  metadata: {}
26
26
  post_install_message:
@@ -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: "hola" }
11
- output = { param_1: "hola" }
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"}
23
- output = {}
24
- assert_equal output, permitted_params( input, { param_1: Integer } )
25
- end
26
-
27
- describe 'Exception' do
28
- describe 'raise' do
29
- it 'should raise error when no parameters are specified' 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