sinatra-my-params 0.0.3 → 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: 517d1c8ee0d4be39d56aa775659ccb5a158c3f27f2f2e0f27abb6b80b890dbb8
4
- data.tar.gz: aa9aca42a85144fbd460016ba3abddc9fbc2d4862fe42da3f8ffda02b65d8a85
3
+ metadata.gz: cab1fb0c3d28a4c3d088ed4221774c92bf3a298a4b02384bd19007b5cdea4879
4
+ data.tar.gz: 175b6ad103a99ee688106ef98d8cee5ce6fde7ba2f12684577235db597227e7d
5
5
  SHA512:
6
- metadata.gz: d34af72735130ba06e1754e03f64b2179b1561ee8325deb1e22516843674fccafcb289f6f9fe902f5979b86cc5823083de996b6a063d904df5301f100f3eda1d
7
- data.tar.gz: '09fdd6bdec6704c5edd45d60c24dcca194e6ed7751cd82b1b463a29efdffe3fa8a455b8b3fc2737386477bf50308987198eeac5db7de5eae244d3aded7477762'
6
+ metadata.gz: 46c2485d12ddfbac286cf2793cebef3a1fc1273dbca5b62acc6358c4fdf44250acc0772af8b1ee4a19696a2773b2c92f57bf12c911c915a7059989c618d701f8
7
+ data.tar.gz: f49ce81b039ab4d02af84220c74f72fc68b6f1f8dec0c4df6f57686acee7db6a30eabb8d261fc27e521d7644642524fa4e3502c1761190a02cb0975c8db8ec0f
data/Rakefile CHANGED
@@ -1,8 +1,13 @@
1
1
  require "rake/testtask"
2
+ require "rspec/core/rake_task"
2
3
 
3
4
  Rake::TestTask.new do |t|
4
5
  t.libs << "test"
5
6
  end
6
7
 
7
- desc "Run tests"
8
- task :default => :test
8
+ begin
9
+ RSpec::Core::RakeTask.new(:spec)
10
+
11
+ task :default => :spec
12
+ rescue LoadError
13
+ end
data/lib/permit_params.rb CHANGED
@@ -1,42 +1,69 @@
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
-
7
- def permitted_params(params, permitted, strong_validation = false)
8
- params.select do |k,v|
9
- permitted.keys.map(&:to_s).include?(k.to_s) &&
10
- !v.nil? &&
11
- coerce(v, permitted[k.to_sym], strong_validation)
8
+ def permitted_params(params, permitted = {}, strong_validation = false, options = {})
9
+ return params if permitted.empty?
10
+
11
+ coerced_params = Hash.new({})
12
+
13
+ params.each do |key, value|
14
+ if permitted.keys.map(&:to_s).include?(key.to_s) && !value.nil?
15
+ coerced = coerce(value, permitted[key.to_sym], strong_validation, options)
16
+ coerced_params[key] = coerced unless coerced.nil?
17
+ end
12
18
  end
19
+ coerced_params
13
20
  end
14
21
 
15
22
  private
16
23
 
17
- def coerce(param, type, strong = false, options = {})
24
+ Boolean = :boolean
25
+ Any = :any
26
+
27
+ def coerce(param, type, strong_validation = false, options = {})
28
+ return param if type == Any
29
+
18
30
  begin
19
31
  return nil if param.nil?
20
32
  return param if (param.is_a?(type) rescue false)
21
- return Integer(param, 10) if type == Integer
33
+ return Integer(param, options[:integer_precision] || 10 ) if type == Integer
22
34
  return Float(param) if type == Float
23
35
  return String(param) if type == String
24
36
  return Date.parse(param) if type == Date
25
37
  return Time.parse(param) if type == Time
26
38
  return DateTime.parse(param) if type == DateTime
27
- return Array(param.split(options[:delimiter] || ",")) if type == Array
28
- return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
29
- if [TrueClass, FalseClass, Boolean].include? type
30
- coerced = /^(false|f|no|n|0)$/i === param.to_s ? false : /^(true|t|yes|y|1)$/i === param.to_s ? true : nil
31
- raise ArgumentError if coerced.nil?
32
- return coerced
33
- end
39
+ return coerce_array(param) if type == Array
40
+ return coerce_hash(param) if type == Hash
41
+ return coerce_boolean(param) if [TrueClass, FalseClass, Boolean].include? type
34
42
  return nil
35
43
  rescue ArgumentError
36
- if strong
37
- raise InvalidParameterError, "'#{param}' is not a valid #{type}"
38
- end
44
+ raise InvalidParameterError, "'#{param}' is not a valid #{type}" if strong_validation
39
45
  end
40
46
  end
41
- end
42
47
 
48
+ def coerce_array(param, options = {})
49
+ Array(param.split(options[:delimiter] || ',').map(&:strip))
50
+ end
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]
57
+ end
58
+
59
+ def coerce_boolean(param)
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
65
+ raise ArgumentError if coerced.nil?
66
+
67
+ coerced
68
+ end
69
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test/unit'
4
+ require 'permit_params'
5
+ require 'rspec'
6
+ require 'rack/test'
7
+
8
+ include PermitParams
9
+
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
18
+ permitted_params(input, { param_1: Integer }, true)
19
+ end.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
20
+ end
21
+
22
+ it 'should return a string when a pemitted is string' do
23
+ input = { param_1: 'a string' }
24
+ expect(input).to eq permitted_params(input, { param_1: String })
25
+ end
26
+
27
+ it 'should return a float when a pemitted is float' do
28
+ input = { param_1: 10.0 }
29
+ expect(input).to eq permitted_params(input, { param_1: Float })
30
+ end
31
+
32
+ it 'should return a date when a pemitted is date' do
33
+ input = { param_1: Date.new }
34
+ expect(input).to eq permitted_params(input, { param_1: Date })
35
+ end
36
+
37
+ it 'should return a time when a pemitted is time' do
38
+ input = { param_1: Time.new }
39
+ expect(input).to eq permitted_params(input, { param_1: Time })
40
+ end
41
+
42
+ it 'should return an integer when a pemitted is integer' do
43
+ input = { param_1: 1 }
44
+ expect(input).to eq permitted_params(input, { param_1: Integer })
45
+ end
46
+
47
+ it 'should return an integer when a pemitted can be cast into integer' do
48
+ input = { param_1: '1' }
49
+ output = { param_1: 1 }
50
+ expect(output).to eq permitted_params(input, { param_1: Integer })
51
+ end
52
+
53
+ it 'should return a false(boolean) when a pemitted is boolean' do
54
+ input = { param_1: 'false' }
55
+ output = { param_1: false }
56
+ expect(output).to eq permitted_params(input, { param_1: Boolean })
57
+ end
58
+
59
+ it 'should return a true(boolean) when a pemitted is boolean' do
60
+ input = { param_1: 'true' }
61
+ output = { param_1: true }
62
+ expect(output).to eq permitted_params(input, { param_1: Boolean })
63
+ end
64
+
65
+ it 'should return an array when a pemitted is array' do
66
+ input = { param_1: [1, 2] }
67
+ expect(input).to eq permitted_params(input, { param_1: Array })
68
+ end
69
+
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
83
+ input = { param_1: { a: 1 } }
84
+ expect(input).to eq permitted_params(input, { param_1: Hash })
85
+ end
86
+
87
+ it 'should return a hash when a pemitted is hash' do
88
+ input = { param_1: { a: 1 } }
89
+ expect(input).to eq permitted_params(input, { param_1: Hash })
90
+ end
91
+
92
+ it 'should return a hash when a pemitted is hash' do
93
+ input = { param_1: { "a": 1 } }
94
+ expect(input).to eq permitted_params(input, { param_1: Hash })
95
+ end
96
+
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 } }
100
+ expect(output).to eq permitted_params(
101
+ input,
102
+ {
103
+ number: Integer,
104
+ string: String,
105
+ bol: Boolean,
106
+ array: Array,
107
+ hsh: Hash
108
+ }
109
+ )
110
+ end
111
+
112
+ it 'returns the paramter without casting if Any' do
113
+ input = { param_1: '1' }
114
+ output = { param_1: '1' }
115
+ expect(output).to eq permitted_params(input, { param_1: Any })
116
+
117
+ input = { param_1: TestClass.new }
118
+ expect(input).to eq permitted_params(input, { param_1: Any })
119
+ end
120
+
121
+ it 'should remove a string when a pemitted is integer' do
122
+ input = { param_1: 'a string' }
123
+ output = {}
124
+ expect(output).to eq permitted_params(input, { param_1: Integer })
125
+ end
126
+
127
+ it 'should allow all params when no restriction is given' do
128
+ input = { param_1: 'a string' }
129
+ expect(input).to eq permitted_params(input)
130
+ end
131
+ 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.3
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Aviles
@@ -11,7 +11,7 @@ cert_chain: []
11
11
  date: 2021-09-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple sinatra params sanitizer
14
- email: nick@quaran.to
14
+ email: gdmarav374@gmail.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
@@ -19,7 +19,7 @@ files:
19
19
  - Rakefile
20
20
  - bin/permit_params
21
21
  - lib/permit_params.rb
22
- - test/test_permit_params.rb
22
+ - spec/permit_params_spec.rb
23
23
  homepage: https://github.com/mhero/sinatra-my-params
24
24
  licenses: []
25
25
  metadata: {}
@@ -43,4 +43,4 @@ signing_key:
43
43
  specification_version: 3
44
44
  summary: permit_params!
45
45
  test_files:
46
- - test/test_permit_params.rb
46
+ - spec/permit_params_spec.rb
@@ -1,35 +0,0 @@
1
- require "test/unit"
2
- require "permit_params"
3
- require "rspec"
4
- require "rack/test"
5
-
6
- class PermitParamsTest < Test::Unit::TestCase
7
- include PermitParams
8
-
9
- def test_pemit_string
10
- input = { param_1: "a string" }
11
- output = { param_1: "a string" }
12
- assert_equal output, permitted_params( input, { param_1: String } )
13
- end
14
-
15
- def test_permit_integer
16
- input = { param_1: 1 }
17
- output = { param_1: 1 }
18
- assert_equal output, permitted_params( input, { param_1: Integer } )
19
- end
20
-
21
- def test_remove_integer
22
- input = { param_1: "a string" }
23
- output = {}
24
- assert_equal output, permitted_params( input, { param_1: Integer } )
25
- end
26
-
27
- describe "exceptions" do
28
- describe "raise" do
29
- it "should raise error when at least one param is invalid" do
30
- input = { param_1: "a" }
31
- expect(permitted_params( input, { param_1: Integer }, true )).to raise_error InvalidParameterError
32
- end
33
- end
34
- end
35
- end