sinatra-my-params 0.0.7 → 0.0.8
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 +4 -4
- data/lib/permit_params.rb +24 -10
- data/spec/permit_params_spec.rb +43 -24
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a1f7f7726f5d92878f2f65847f2d853d3ab3033b3dfaf4e84784d0bb56bd9b3
|
4
|
+
data.tar.gz: 4ebf4d253a1286535ff93503433695867faf4ab4c3b3fd08076e807ba40535f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b87393662b298d9acf0c127427ed77894c2c378fc05f4f737c0412c60180eae68f99ba55c7e6bafdfd42afc8428971482c19cac9bb3c0524ca289f741488364c
|
7
|
+
data.tar.gz: d9a899475bb3e5d6af6cd2c5d539b645a5770e9d4de99cf37797899058bd0ad18c2bec8799905aab8dd3c46cbacf8915d3ad97679684dfce15e56583d9d5c554
|
data/lib/permit_params.rb
CHANGED
@@ -11,10 +11,15 @@ module PermitParams
|
|
11
11
|
coerced_params = Hash.new({})
|
12
12
|
|
13
13
|
params.each do |key, value|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
next unless permitted.keys.map(&:to_s).include?(key.to_s) && !value.nil?
|
15
|
+
|
16
|
+
coerced = coerce(
|
17
|
+
param: value,
|
18
|
+
type: permitted[key.to_sym],
|
19
|
+
strong_validation: strong_validation,
|
20
|
+
options: options
|
21
|
+
)
|
22
|
+
coerced_params[key] = coerced unless coerced.nil?
|
18
23
|
end
|
19
24
|
coerced_params
|
20
25
|
end
|
@@ -24,27 +29,36 @@ module PermitParams
|
|
24
29
|
Boolean = :boolean
|
25
30
|
Any = :any
|
26
31
|
|
27
|
-
def coerce(param
|
32
|
+
def coerce(param:, type:, strong_validation: false, options: {})
|
28
33
|
return param if type == Any
|
29
34
|
|
30
35
|
begin
|
31
36
|
return nil if param.nil?
|
32
|
-
return param if
|
33
|
-
|
37
|
+
return param if begin
|
38
|
+
param.is_a?(type)
|
39
|
+
rescue StandardError
|
40
|
+
false
|
41
|
+
end
|
42
|
+
return coerce_integer(param, options) if type == Integer
|
34
43
|
return Float(param) if type == Float
|
35
44
|
return String(param) if type == String
|
36
45
|
return Date.parse(param) if type == Date
|
37
46
|
return Time.parse(param) if type == Time
|
38
47
|
return DateTime.parse(param) if type == DateTime
|
39
|
-
return coerce_array(param) if type == Array
|
40
|
-
return coerce_hash(param) if type == Hash
|
48
|
+
return coerce_array(param, options) if type == Array
|
49
|
+
return coerce_hash(param, options) if type == Hash
|
41
50
|
return coerce_boolean(param) if [TrueClass, FalseClass, Boolean].include? type
|
42
|
-
|
51
|
+
|
52
|
+
nil
|
43
53
|
rescue ArgumentError
|
44
54
|
raise InvalidParameterError, "'#{param}' is not a valid #{type}" if strong_validation
|
45
55
|
end
|
46
56
|
end
|
47
57
|
|
58
|
+
def coerce_integer(param, options = {})
|
59
|
+
Integer(param, options[:integer_precision] || 10)
|
60
|
+
end
|
61
|
+
|
48
62
|
def coerce_array(param, options = {})
|
49
63
|
Array(param.split(options[:delimiter] || ',').map(&:strip))
|
50
64
|
end
|
data/spec/permit_params_spec.rb
CHANGED
@@ -19,6 +19,28 @@ describe 'exceptions' do
|
|
19
19
|
end.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
|
20
20
|
end
|
21
21
|
|
22
|
+
it 'should allow all params when no restriction is given' do
|
23
|
+
input = { param_1: 'a string' }
|
24
|
+
expect(input).to eq permitted_params(input)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should remove a string when a pemitted is integer' do
|
28
|
+
input = { param_1: 'a string' }
|
29
|
+
output = {}
|
30
|
+
expect(output).to eq permitted_params(input, { param_1: Integer })
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should return an integer when a pemitted is integer' do
|
34
|
+
input = { param_1: 1 }
|
35
|
+
expect(input).to eq permitted_params(input, { param_1: Integer })
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should return an integer when a pemitted can be cast into integer' do
|
39
|
+
input = { param_1: '1' }
|
40
|
+
output = { param_1: 1 }
|
41
|
+
expect(output).to eq permitted_params(input, { param_1: Integer })
|
42
|
+
end
|
43
|
+
|
22
44
|
it 'should return a string when a pemitted is string' do
|
23
45
|
input = { param_1: 'a string' }
|
24
46
|
expect(input).to eq permitted_params(input, { param_1: String })
|
@@ -39,17 +61,6 @@ describe 'exceptions' do
|
|
39
61
|
expect(input).to eq permitted_params(input, { param_1: Time })
|
40
62
|
end
|
41
63
|
|
42
|
-
it 'should return an integer when a pemitted is integer' do
|
43
|
-
input = { param_1: 1 }
|
44
|
-
expect(input).to eq permitted_params(input, { param_1: Integer })
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should return an integer when a pemitted can be cast into integer' do
|
48
|
-
input = { param_1: '1' }
|
49
|
-
output = { param_1: 1 }
|
50
|
-
expect(output).to eq permitted_params(input, { param_1: Integer })
|
51
|
-
end
|
52
|
-
|
53
64
|
it 'should return a false(boolean) when a pemitted is boolean' do
|
54
65
|
input = { param_1: 'false' }
|
55
66
|
output = { param_1: false }
|
@@ -73,19 +84,31 @@ describe 'exceptions' do
|
|
73
84
|
expect(output).to eq permitted_params(input, { param_1: Array })
|
74
85
|
end
|
75
86
|
|
87
|
+
it 'should return an array when a pemitted is array' do
|
88
|
+
input = { param_1: '1; 2' }
|
89
|
+
output = { param_1: %w[1 2] }
|
90
|
+
expect(output).to eq permitted_params(input, { param_1: Array }, false, { delimiter: ';' })
|
91
|
+
end
|
92
|
+
|
76
93
|
it 'should return a hash when a pemitted is hash' do
|
77
94
|
input = { param_1: 'a: 1, b: 2' }
|
78
95
|
output = { param_1: { 'a' => '1', 'b' => '2' } }
|
79
96
|
expect(output).to eq permitted_params(input, { param_1: Hash })
|
80
97
|
end
|
81
98
|
|
99
|
+
it 'should return a hash when a pemitted is hash' do
|
100
|
+
input = { param_1: 'a: 1; b: 2' }
|
101
|
+
output = { param_1: { 'a' => '1', 'b' => '2' } }
|
102
|
+
expect(output).to eq permitted_params(input, { param_1: Hash }, false, { delimiter: ';' })
|
103
|
+
end
|
104
|
+
|
82
105
|
it 'should return a hash when a pemitted is hash' do
|
83
106
|
input = { param_1: { a: 1 } }
|
84
107
|
expect(input).to eq permitted_params(input, { param_1: Hash })
|
85
108
|
end
|
86
109
|
|
87
110
|
it 'should return a hash when a pemitted is hash' do
|
88
|
-
input = { param_1: { a: 1 } }
|
111
|
+
input = { param_1: { a: { b: 1 } } }
|
89
112
|
expect(input).to eq permitted_params(input, { param_1: Hash })
|
90
113
|
end
|
91
114
|
|
@@ -114,18 +137,14 @@ describe 'exceptions' do
|
|
114
137
|
output = { param_1: '1' }
|
115
138
|
expect(output).to eq permitted_params(input, { param_1: Any })
|
116
139
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
it 'should remove a string when a pemitted is integer' do
|
122
|
-
input = { param_1: 'a string' }
|
123
|
-
output = {}
|
124
|
-
expect(output).to eq permitted_params(input, { param_1: Integer })
|
125
|
-
end
|
140
|
+
test_class = TestClass.new
|
141
|
+
input = { param_1: test_class }
|
142
|
+
output = { param_1: test_class }
|
143
|
+
expect(output).to eq permitted_params(input, { param_1: Any })
|
126
144
|
|
127
|
-
|
128
|
-
input = { param_1:
|
129
|
-
|
145
|
+
test_class = TestClass.new
|
146
|
+
input = { param_1: test_class, param_2: 2 }
|
147
|
+
output = { param_1: test_class }
|
148
|
+
expect(output).to eq permitted_params(input, { param_1: Any })
|
130
149
|
end
|
131
150
|
end
|