sinatra-my-params 0.0.4 → 0.0.5
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 +20 -8
- data/spec/permit_params_spec.rb +38 -3
- 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: 747bacbce6ab90027f92e29c45475964414cda4f62ad362339f1fd515323fcf3
|
4
|
+
data.tar.gz: 2c0d6582d40e18230e5d97e29ddff20484d86163366bc23e7aa24e67c255d9a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2b7b1e994250fce4fbabe3cf9571b62dc6432c7965d81897b54889a1be64198fb0dff9ba1ab83e91462985ee7644a5a3b00188b600b1d7fa4431d72e0ee99d6
|
7
|
+
data.tar.gz: e502b5deafbeb12839eb772b3be83720160ac8f1c99950fa527dcdef9da2e04df7aa55ffee1a32c6e037fa9931e3ec3f28e2e3470e545781e9855245b5db3d5d
|
data/lib/permit_params.rb
CHANGED
@@ -6,18 +6,22 @@ module PermitParams
|
|
6
6
|
def permitted_params(params, permitted = {}, strong_validation = false)
|
7
7
|
return params if permitted.empty?
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
13
16
|
end
|
17
|
+
coerced_params
|
14
18
|
end
|
15
19
|
|
16
20
|
private
|
17
21
|
|
18
22
|
Boolean = :boolean
|
19
23
|
|
20
|
-
def coerce(param, type,
|
24
|
+
def coerce(param, type, strong_validation = false, options = {})
|
21
25
|
begin
|
22
26
|
return nil if param.nil?
|
23
27
|
return param if (param.is_a?(type) rescue false)
|
@@ -27,15 +31,23 @@ module PermitParams
|
|
27
31
|
return Date.parse(param) if type == Date
|
28
32
|
return Time.parse(param) if type == Time
|
29
33
|
return DateTime.parse(param) if type == DateTime
|
30
|
-
return
|
31
|
-
return
|
34
|
+
return coerce_array(param) if type == Array
|
35
|
+
return coerce_hash(param) if type == Hash
|
32
36
|
return coerce_boolean(param) if [TrueClass, FalseClass, Boolean].include? type
|
33
37
|
return nil
|
34
38
|
rescue ArgumentError
|
35
|
-
raise InvalidParameterError, "'#{param}' is not a valid #{type}" if
|
39
|
+
raise InvalidParameterError, "'#{param}' is not a valid #{type}" if strong_validation
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
43
|
+
def coerce_array(param)
|
44
|
+
Array(param.split(options[:delimiter] || ","))
|
45
|
+
end
|
46
|
+
|
47
|
+
def coerce_hash(param)
|
48
|
+
Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}]
|
49
|
+
end
|
50
|
+
|
39
51
|
def coerce_boolean(param)
|
40
52
|
coerced = /^(false|f|no|n|0)$/i === param.to_s ? false : /^(true|t|yes|y|1)$/i === param.to_s ? true : nil
|
41
53
|
raise ArgumentError if coerced.nil?
|
data/spec/permit_params_spec.rb
CHANGED
@@ -41,21 +41,56 @@ describe "exceptions" do
|
|
41
41
|
it "should return an integer when a pemitted can be cast into integer" do
|
42
42
|
input = { param_1: "1" }
|
43
43
|
output = { param_1: 1 }
|
44
|
-
expect(
|
44
|
+
expect(output).to eq permitted_params(input, { param_1: Integer })
|
45
45
|
end
|
46
46
|
|
47
47
|
it "should return a false(boolean) when a pemitted is boolean" do
|
48
48
|
input = { param_1: "false" }
|
49
|
-
output = { param_1:
|
49
|
+
output = { param_1: false }
|
50
50
|
expect(output).to eq permitted_params(input, { param_1: Boolean })
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should return a true(boolean) when a pemitted is boolean" do
|
54
54
|
input = { param_1: "true" }
|
55
|
-
output = { param_1:
|
55
|
+
output = { param_1: true }
|
56
56
|
expect(output).to eq permitted_params(input, { param_1: Boolean })
|
57
57
|
end
|
58
58
|
|
59
|
+
it "should return an array when a pemitted is array" do
|
60
|
+
input = { param_1: [1,2] }
|
61
|
+
expect(input).to eq permitted_params(input, { param_1: Array })
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should return a hash when a pemitted is hash" do
|
65
|
+
input = { param_1: { a: 1 } }
|
66
|
+
expect(input).to eq permitted_params(input, { param_1: Hash })
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return a hash when a pemitted is hash" do
|
70
|
+
input = { param_1: { :a => 1 } }
|
71
|
+
expect(input).to eq permitted_params(input, { param_1: Hash })
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should return a hash when a pemitted is hash" do
|
75
|
+
input = { param_1: { "a": 1 } }
|
76
|
+
expect(input).to eq permitted_params(input, { param_1: Hash })
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should return a several types for several inputs" do
|
80
|
+
input = { number: 1, string: "string", bol: "true", array: [1,2], hsh: {a: 3} }
|
81
|
+
output = { number: 1, string: "string", bol: true, array: [1,2], hsh: {a: 3} }
|
82
|
+
expect(output).to eq permitted_params(
|
83
|
+
input,
|
84
|
+
{
|
85
|
+
number: Integer,
|
86
|
+
string: String,
|
87
|
+
bol: Boolean,
|
88
|
+
array: Array,
|
89
|
+
hsh: Hash
|
90
|
+
}
|
91
|
+
)
|
92
|
+
end
|
93
|
+
|
59
94
|
it "should remove a string when a pemitted is integer" do
|
60
95
|
input = { param_1: "a string" }
|
61
96
|
output = {}
|