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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 004ffa9a419a6ca94f76672d37e9cd242aca9f69ca1c92ada97ac135a1329abc
4
- data.tar.gz: a0dcc3771df668d6356a4443e98cd1bcb4d1ad6e877076bbc3cc5f99e904a078
3
+ metadata.gz: cab1fb0c3d28a4c3d088ed4221774c92bf3a298a4b02384bd19007b5cdea4879
4
+ data.tar.gz: 175b6ad103a99ee688106ef98d8cee5ce6fde7ba2f12684577235db597227e7d
5
5
  SHA512:
6
- metadata.gz: 04d4e285426806ac16f36f7b757eed7682d1502c272dedf7f0fd3291234e9cf3eb611c52c25a73cca3f3b29d6190ed83523cde335125599cfb92f81e2eee8c17
7
- data.tar.gz: 861c6fc9858865ac856e66ed030f5bd8ac7005b268c3a3a2cb66deec648feec52b1c596808e6736b4495bf20e1fe7e4e818ccbfa8ebb24fe2727d77511bb13cf
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 if !coerced.nil?
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
- Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}]
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 ? false : /^(true|t|yes|y|1)$/i === param.to_s ? true : nil
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
-
@@ -1,91 +1,105 @@
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
10
+ describe 'exceptions' do
9
11
  before do
10
12
  class TestClass; end
11
13
  end
12
14
 
13
- it "should raise error when at least one param is invalid" do
14
- input = { param_1: "a" }
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
- }.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
19
+ end.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
18
20
  end
19
21
 
20
- it "should return a string when a pemitted is string" do
21
- input = { param_1: "a string" }
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 "should return a float when a pemitted is float" do
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 "should return a date when a pemitted is date" do
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 "should return a time when a pemitted is time" do
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 "should return an integer when a pemitted is integer" do
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 "should return an integer when a pemitted can be cast into integer" do
46
- input = { param_1: "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 "should return a false(boolean) when a pemitted is boolean" do
52
- input = { param_1: "false" }
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 "should return a true(boolean) when a pemitted is boolean" do
58
- input = { param_1: "true" }
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 "should return an array when a pemitted is array" do
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 "should return a hash when a pemitted is hash" do
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 "should return a hash when a pemitted is hash" do
74
- input = { param_1: { :a => 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 "should return a hash when a pemitted is hash" do
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 "should return a several types for several inputs" do
84
- input = { number: 1, string: "string", bol: "true", array: [1,2], hsh: {a: 3} }
85
- output = { number: 1, string: "string", bol: true, array: [1,2], hsh: {a: 3} }
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 "returns the paramter without casting if Any" do
99
- input = { param_1: "1" }
100
- output = { param_1: "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 "should remove a string when a pemitted is integer" do
108
- input = { param_1: "a string" }
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 "should allow all params when no restriction is given" do
114
- input = { param_1: "a string" }
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
-
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.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Aviles