sinatra-my-params 0.0.4 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a2c387628a7d4582e28e5fe2bdc905abc284cdcf81c76fc8bd2f0c3e23f254e1
4
- data.tar.gz: e86107885442e79700caf8f293b591814798296275b5588dbe2120d80df63898
3
+ metadata.gz: 5a1f7f7726f5d92878f2f65847f2d853d3ab3033b3dfaf4e84784d0bb56bd9b3
4
+ data.tar.gz: 4ebf4d253a1286535ff93503433695867faf4ab4c3b3fd08076e807ba40535f9
5
5
  SHA512:
6
- metadata.gz: 8c544a84df83f1e507dced93487e175f0d3b4044141f47774f59fdd8f12592070f98134a472b524983ddde905af04444da6acf359cbf8a34c14fe5e0d1eb65b5
7
- data.tar.gz: b03d81b690692988673bdf9fbf944ca533fa770ff3e4b21f358e371579b6c0ebc6c5688d5729adc243c4e1f522737b256941cee23ee71df6964917b87c6c3a3e
6
+ metadata.gz: b87393662b298d9acf0c127427ed77894c2c378fc05f4f737c0412c60180eae68f99ba55c7e6bafdfd42afc8428971482c19cac9bb3c0524ca289f741488364c
7
+ data.tar.gz: d9a899475bb3e5d6af6cd2c5d539b645a5770e9d4de99cf37797899058bd0ad18c2bec8799905aab8dd3c46cbacf8915d3ad97679684dfce15e56583d9d5c554
data/lib/permit_params.rb CHANGED
@@ -1,45 +1,83 @@
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
-
9
- params.select do |k,v|
10
- permitted.keys.map(&:to_s).include?(k.to_s) &&
11
- !v.nil? &&
12
- !coerce(v, permitted[k.to_sym], strong_validation).nil?
10
+
11
+ coerced_params = Hash.new({})
12
+
13
+ params.each do |key, value|
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?
13
23
  end
24
+ coerced_params
14
25
  end
15
26
 
16
27
  private
17
28
 
18
29
  Boolean = :boolean
30
+ Any = :any
31
+
32
+ def coerce(param:, type:, strong_validation: false, options: {})
33
+ return param if type == Any
19
34
 
20
- def coerce(param, type, strong = false, options = {})
21
35
  begin
22
36
  return nil if param.nil?
23
- return param if (param.is_a?(type) rescue false)
24
- return Integer(param, 10) if type == Integer
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
25
43
  return Float(param) if type == Float
26
44
  return String(param) if type == String
27
45
  return Date.parse(param) if type == Date
28
46
  return Time.parse(param) if type == Time
29
47
  return DateTime.parse(param) if type == DateTime
30
- return Array(param.split(options[:delimiter] || ",")) if type == Array
31
- return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
48
+ return coerce_array(param, options) if type == Array
49
+ return coerce_hash(param, options) if type == Hash
32
50
  return coerce_boolean(param) if [TrueClass, FalseClass, Boolean].include? type
33
- return nil
51
+
52
+ nil
34
53
  rescue ArgumentError
35
- raise InvalidParameterError, "'#{param}' is not a valid #{type}" if strong
54
+ raise InvalidParameterError, "'#{param}' is not a valid #{type}" if strong_validation
36
55
  end
37
56
  end
38
57
 
58
+ def coerce_integer(param, options = {})
59
+ Integer(param, options[:integer_precision] || 10)
60
+ end
61
+
62
+ def coerce_array(param, options = {})
63
+ Array(param.split(options[:delimiter] || ',').map(&:strip))
64
+ end
65
+
66
+ def coerce_hash(param, options = {})
67
+ key_value = param.split(options[:delimiter] || ',').map(&:strip).map do |c|
68
+ c.split(options[:separator] || ':').map(&:strip)
69
+ end
70
+ Hash[key_value]
71
+ end
72
+
39
73
  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
74
+ coerced = if /^(false|f|no|n|0)$/i === param.to_s
75
+ false
76
+ else
77
+ /^(true|t|yes|y|1)$/i === param.to_s ? true : nil
78
+ end
41
79
  raise ArgumentError if coerced.nil?
80
+
42
81
  coerced
43
82
  end
44
83
  end
45
-
@@ -1,70 +1,150 @@
1
- require "test/unit"
2
- require "permit_params"
3
- require "rspec"
4
- require "rack/test"
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 "exceptions" do
9
- it "should raise error when at least one param is invalid" do
10
- input = { param_1: "a" }
11
- expect{
10
+ describe 'exceptions' do
11
+ before do
12
+ class TestClass; end
13
+ end
14
+
15
+ it 'should raise error when at least one param is invalid' do
16
+ input = { param_1: 'a' }
17
+ expect do
12
18
  permitted_params(input, { param_1: Integer }, true)
13
- }.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
19
+ end.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
20
+ end
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 })
14
36
  end
15
37
 
16
- it "should return a string when a pemitted is string" do
17
- input = { param_1: "a string" }
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
+
44
+ it 'should return a string when a pemitted is string' do
45
+ input = { param_1: 'a string' }
18
46
  expect(input).to eq permitted_params(input, { param_1: String })
19
47
  end
20
48
 
21
- it "should return a float when a pemitted is float" do
49
+ it 'should return a float when a pemitted is float' do
22
50
  input = { param_1: 10.0 }
23
51
  expect(input).to eq permitted_params(input, { param_1: Float })
24
52
  end
25
53
 
26
- it "should return a date when a pemitted is date" do
54
+ it 'should return a date when a pemitted is date' do
27
55
  input = { param_1: Date.new }
28
56
  expect(input).to eq permitted_params(input, { param_1: Date })
29
57
  end
30
58
 
31
- it "should return a time when a pemitted is time" do
59
+ it 'should return a time when a pemitted is time' do
32
60
  input = { param_1: Time.new }
33
61
  expect(input).to eq permitted_params(input, { param_1: Time })
34
62
  end
35
63
 
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 })
64
+ it 'should return a false(boolean) when a pemitted is boolean' do
65
+ input = { param_1: 'false' }
66
+ output = { param_1: false }
67
+ expect(output).to eq permitted_params(input, { param_1: Boolean })
39
68
  end
40
69
 
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 })
70
+ it 'should return a true(boolean) when a pemitted is boolean' do
71
+ input = { param_1: 'true' }
72
+ output = { param_1: true }
73
+ expect(output).to eq permitted_params(input, { param_1: Boolean })
45
74
  end
46
75
 
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 })
76
+ it 'should return an array when a pemitted is array' do
77
+ input = { param_1: [1, 2] }
78
+ expect(input).to eq permitted_params(input, { param_1: Array })
51
79
  end
52
80
 
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 })
81
+ it 'should return an array when a pemitted is array' do
82
+ input = { param_1: '1, 2' }
83
+ output = { param_1: %w[1 2] }
84
+ expect(output).to eq permitted_params(input, { param_1: Array })
57
85
  end
58
86
 
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 })
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: ';' })
63
91
  end
64
92
 
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)
93
+ it 'should return a hash when a pemitted is hash' do
94
+ input = { param_1: 'a: 1, b: 2' }
95
+ output = { param_1: { 'a' => '1', 'b' => '2' } }
96
+ expect(output).to eq permitted_params(input, { param_1: Hash })
97
+ end
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
+
105
+ it 'should return a hash when a pemitted is hash' do
106
+ input = { param_1: { a: 1 } }
107
+ expect(input).to eq permitted_params(input, { param_1: Hash })
108
+ end
109
+
110
+ it 'should return a hash when a pemitted is hash' do
111
+ input = { param_1: { a: { b: 1 } } }
112
+ expect(input).to eq permitted_params(input, { param_1: Hash })
68
113
  end
69
- end
70
114
 
115
+ it 'should return a hash when a pemitted is hash' do
116
+ input = { param_1: { "a": 1 } }
117
+ expect(input).to eq permitted_params(input, { param_1: Hash })
118
+ end
119
+
120
+ it 'should return a several types for several inputs' do
121
+ input = { number: 1, string: 'string', bol: 'true', array: [1, 2], hsh: { a: 3 } }
122
+ output = { number: 1, string: 'string', bol: true, array: [1, 2], hsh: { a: 3 } }
123
+ expect(output).to eq permitted_params(
124
+ input,
125
+ {
126
+ number: Integer,
127
+ string: String,
128
+ bol: Boolean,
129
+ array: Array,
130
+ hsh: Hash
131
+ }
132
+ )
133
+ end
134
+
135
+ it 'returns the paramter without casting if Any' do
136
+ input = { param_1: '1' }
137
+ output = { param_1: '1' }
138
+ expect(output).to eq permitted_params(input, { param_1: Any })
139
+
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 })
144
+
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 })
149
+ end
150
+ end
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.4
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Aviles