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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a2c387628a7d4582e28e5fe2bdc905abc284cdcf81c76fc8bd2f0c3e23f254e1
4
- data.tar.gz: e86107885442e79700caf8f293b591814798296275b5588dbe2120d80df63898
3
+ metadata.gz: 747bacbce6ab90027f92e29c45475964414cda4f62ad362339f1fd515323fcf3
4
+ data.tar.gz: 2c0d6582d40e18230e5d97e29ddff20484d86163366bc23e7aa24e67c255d9a6
5
5
  SHA512:
6
- metadata.gz: 8c544a84df83f1e507dced93487e175f0d3b4044141f47774f59fdd8f12592070f98134a472b524983ddde905af04444da6acf359cbf8a34c14fe5e0d1eb65b5
7
- data.tar.gz: b03d81b690692988673bdf9fbf944ca533fa770ff3e4b21f358e371579b6c0ebc6c5688d5729adc243c4e1f522737b256941cee23ee71df6964917b87c6c3a3e
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
- 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?
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, strong = false, options = {})
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 Array(param.split(options[:delimiter] || ",")) if type == Array
31
- return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
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 strong
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?
@@ -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(input).to eq permitted_params(input, { param_1: Integer })
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: "false" }
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: "true" }
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 = {}
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.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Aviles