sinatra-my-params 0.0.6 → 0.0.7
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 -11
- data/spec/permit_params_spec.rb +53 -40
- 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: cab1fb0c3d28a4c3d088ed4221774c92bf3a298a4b02384bd19007b5cdea4879
|
4
|
+
data.tar.gz: 175b6ad103a99ee688106ef98d8cee5ce6fde7ba2f12684577235db597227e7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46c2485d12ddfbac286cf2793cebef3a1fc1273dbca5b62acc6358c4fdf44250acc0772af8b1ee4a19696a2773b2c92f57bf12c911c915a7059989c618d701f8
|
7
|
+
data.tar.gz: f49ce81b039ab4d02af84220c74f72fc68b6f1f8dec0c4df6f57686acee7db6a30eabb8d261fc27e521d7644642524fa4e3502c1761190a02cb0975c8db8ec0f
|
data/lib/permit_params.rb
CHANGED
@@ -1,17 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module PermitParams
|
2
4
|
class InvalidParameterError < StandardError
|
3
5
|
attr_accessor :param, :options
|
4
6
|
end
|
5
7
|
|
6
|
-
def permitted_params(params, permitted = {}, strong_validation = false)
|
8
|
+
def permitted_params(params, permitted = {}, strong_validation = false, options = {})
|
7
9
|
return params if permitted.empty?
|
8
|
-
|
10
|
+
|
9
11
|
coerced_params = Hash.new({})
|
10
12
|
|
11
13
|
params.each do |key, value|
|
12
14
|
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
|
15
|
+
coerced = coerce(value, permitted[key.to_sym], strong_validation, options)
|
16
|
+
coerced_params[key] = coerced unless coerced.nil?
|
15
17
|
end
|
16
18
|
end
|
17
19
|
coerced_params
|
@@ -28,7 +30,7 @@ module PermitParams
|
|
28
30
|
begin
|
29
31
|
return nil if param.nil?
|
30
32
|
return param if (param.is_a?(type) rescue false)
|
31
|
-
return Integer(param, 10) if type == Integer
|
33
|
+
return Integer(param, options[:integer_precision] || 10 ) if type == Integer
|
32
34
|
return Float(param) if type == Float
|
33
35
|
return String(param) if type == String
|
34
36
|
return Date.parse(param) if type == Date
|
@@ -43,18 +45,25 @@ module PermitParams
|
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
46
|
-
def coerce_array(param)
|
47
|
-
Array(param.split(options[:delimiter] ||
|
48
|
+
def coerce_array(param, options = {})
|
49
|
+
Array(param.split(options[:delimiter] || ',').map(&:strip))
|
48
50
|
end
|
49
51
|
|
50
|
-
def coerce_hash(param)
|
51
|
-
|
52
|
+
def coerce_hash(param, options = {})
|
53
|
+
key_value = param.split(options[:delimiter] || ',').map(&:strip).map do |c|
|
54
|
+
c.split(options[:separator] || ':').map(&:strip)
|
55
|
+
end
|
56
|
+
Hash[key_value]
|
52
57
|
end
|
53
58
|
|
54
59
|
def coerce_boolean(param)
|
55
|
-
coerced = /^(false|f|no|n|0)$/i === param.to_s
|
60
|
+
coerced = if /^(false|f|no|n|0)$/i === param.to_s
|
61
|
+
false
|
62
|
+
else
|
63
|
+
/^(true|t|yes|y|1)$/i === param.to_s ? true : nil
|
64
|
+
end
|
56
65
|
raise ArgumentError if coerced.nil?
|
66
|
+
|
57
67
|
coerced
|
58
68
|
end
|
59
69
|
end
|
60
|
-
|
data/spec/permit_params_spec.rb
CHANGED
@@ -1,91 +1,105 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'permit_params'
|
5
|
+
require 'rspec'
|
6
|
+
require 'rack/test'
|
5
7
|
|
6
8
|
include PermitParams
|
7
9
|
|
8
|
-
describe
|
10
|
+
describe 'exceptions' do
|
9
11
|
before do
|
10
12
|
class TestClass; end
|
11
13
|
end
|
12
14
|
|
13
|
-
it
|
14
|
-
input = { param_1:
|
15
|
-
expect
|
15
|
+
it 'should raise error when at least one param is invalid' do
|
16
|
+
input = { param_1: 'a' }
|
17
|
+
expect do
|
16
18
|
permitted_params(input, { param_1: Integer }, true)
|
17
|
-
|
19
|
+
end.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
|
18
20
|
end
|
19
21
|
|
20
|
-
it
|
21
|
-
input = { param_1:
|
22
|
+
it 'should return a string when a pemitted is string' do
|
23
|
+
input = { param_1: 'a string' }
|
22
24
|
expect(input).to eq permitted_params(input, { param_1: String })
|
23
25
|
end
|
24
26
|
|
25
|
-
it
|
27
|
+
it 'should return a float when a pemitted is float' do
|
26
28
|
input = { param_1: 10.0 }
|
27
29
|
expect(input).to eq permitted_params(input, { param_1: Float })
|
28
30
|
end
|
29
31
|
|
30
|
-
it
|
32
|
+
it 'should return a date when a pemitted is date' do
|
31
33
|
input = { param_1: Date.new }
|
32
34
|
expect(input).to eq permitted_params(input, { param_1: Date })
|
33
35
|
end
|
34
36
|
|
35
|
-
it
|
37
|
+
it 'should return a time when a pemitted is time' do
|
36
38
|
input = { param_1: Time.new }
|
37
39
|
expect(input).to eq permitted_params(input, { param_1: Time })
|
38
40
|
end
|
39
41
|
|
40
|
-
it
|
42
|
+
it 'should return an integer when a pemitted is integer' do
|
41
43
|
input = { param_1: 1 }
|
42
44
|
expect(input).to eq permitted_params(input, { param_1: Integer })
|
43
45
|
end
|
44
46
|
|
45
|
-
it
|
46
|
-
input = { param_1:
|
47
|
+
it 'should return an integer when a pemitted can be cast into integer' do
|
48
|
+
input = { param_1: '1' }
|
47
49
|
output = { param_1: 1 }
|
48
50
|
expect(output).to eq permitted_params(input, { param_1: Integer })
|
49
51
|
end
|
50
52
|
|
51
|
-
it
|
52
|
-
input = { param_1:
|
53
|
+
it 'should return a false(boolean) when a pemitted is boolean' do
|
54
|
+
input = { param_1: 'false' }
|
53
55
|
output = { param_1: false }
|
54
56
|
expect(output).to eq permitted_params(input, { param_1: Boolean })
|
55
57
|
end
|
56
58
|
|
57
|
-
it
|
58
|
-
input = { param_1:
|
59
|
+
it 'should return a true(boolean) when a pemitted is boolean' do
|
60
|
+
input = { param_1: 'true' }
|
59
61
|
output = { param_1: true }
|
60
62
|
expect(output).to eq permitted_params(input, { param_1: Boolean })
|
61
63
|
end
|
62
64
|
|
63
|
-
it
|
64
|
-
input = { param_1: [1,2] }
|
65
|
+
it 'should return an array when a pemitted is array' do
|
66
|
+
input = { param_1: [1, 2] }
|
65
67
|
expect(input).to eq permitted_params(input, { param_1: Array })
|
66
68
|
end
|
67
69
|
|
68
|
-
it
|
70
|
+
it 'should return an array when a pemitted is array' do
|
71
|
+
input = { param_1: '1, 2' }
|
72
|
+
output = { param_1: %w[1 2] }
|
73
|
+
expect(output).to eq permitted_params(input, { param_1: Array })
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should return a hash when a pemitted is hash' do
|
77
|
+
input = { param_1: 'a: 1, b: 2' }
|
78
|
+
output = { param_1: { 'a' => '1', 'b' => '2' } }
|
79
|
+
expect(output).to eq permitted_params(input, { param_1: Hash })
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should return a hash when a pemitted is hash' do
|
69
83
|
input = { param_1: { a: 1 } }
|
70
84
|
expect(input).to eq permitted_params(input, { param_1: Hash })
|
71
85
|
end
|
72
86
|
|
73
|
-
it
|
74
|
-
input = { param_1: { :
|
87
|
+
it 'should return a hash when a pemitted is hash' do
|
88
|
+
input = { param_1: { a: 1 } }
|
75
89
|
expect(input).to eq permitted_params(input, { param_1: Hash })
|
76
90
|
end
|
77
91
|
|
78
|
-
it
|
92
|
+
it 'should return a hash when a pemitted is hash' do
|
79
93
|
input = { param_1: { "a": 1 } }
|
80
94
|
expect(input).to eq permitted_params(input, { param_1: Hash })
|
81
95
|
end
|
82
96
|
|
83
|
-
it
|
84
|
-
input = { number: 1, string:
|
85
|
-
output = { number: 1, string:
|
97
|
+
it 'should return a several types for several inputs' do
|
98
|
+
input = { number: 1, string: 'string', bol: 'true', array: [1, 2], hsh: { a: 3 } }
|
99
|
+
output = { number: 1, string: 'string', bol: true, array: [1, 2], hsh: { a: 3 } }
|
86
100
|
expect(output).to eq permitted_params(
|
87
|
-
input,
|
88
|
-
{
|
101
|
+
input,
|
102
|
+
{
|
89
103
|
number: Integer,
|
90
104
|
string: String,
|
91
105
|
bol: Boolean,
|
@@ -95,24 +109,23 @@ describe "exceptions" do
|
|
95
109
|
)
|
96
110
|
end
|
97
111
|
|
98
|
-
it
|
99
|
-
input = { param_1:
|
100
|
-
output = { param_1:
|
112
|
+
it 'returns the paramter without casting if Any' do
|
113
|
+
input = { param_1: '1' }
|
114
|
+
output = { param_1: '1' }
|
101
115
|
expect(output).to eq permitted_params(input, { param_1: Any })
|
102
116
|
|
103
117
|
input = { param_1: TestClass.new }
|
104
118
|
expect(input).to eq permitted_params(input, { param_1: Any })
|
105
119
|
end
|
106
120
|
|
107
|
-
it
|
108
|
-
input = { param_1:
|
121
|
+
it 'should remove a string when a pemitted is integer' do
|
122
|
+
input = { param_1: 'a string' }
|
109
123
|
output = {}
|
110
124
|
expect(output).to eq permitted_params(input, { param_1: Integer })
|
111
125
|
end
|
112
126
|
|
113
|
-
it
|
114
|
-
input = { param_1:
|
127
|
+
it 'should allow all params when no restriction is given' do
|
128
|
+
input = { param_1: 'a string' }
|
115
129
|
expect(input).to eq permitted_params(input)
|
116
130
|
end
|
117
131
|
end
|
118
|
-
|