sinatra-my-params 0.0.1 → 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: 03d95a0948071a629809d0733d17cf36ff552f451594de835cdda64670b9d32c
4
- data.tar.gz: 4d80191893aeb6a86f6bbe269fa2be607f2a0c2807e0a125fd1e9d3090ab293d
3
+ metadata.gz: 747bacbce6ab90027f92e29c45475964414cda4f62ad362339f1fd515323fcf3
4
+ data.tar.gz: 2c0d6582d40e18230e5d97e29ddff20484d86163366bc23e7aa24e67c255d9a6
5
5
  SHA512:
6
- metadata.gz: bd1456d10b45d4238ac698aff9247e869e6a0a7ec01ac7e6d544b3d5f0f3449f1a3cd511167e358e98cab476383fba69c5cfbe0ef92093448ed2b61102b76dd4
7
- data.tar.gz: 8c576e0f4ae287df3a148a913ce62cc155269be7bb9b54db57138edb29b325f770e1248b1ad6dcb4ecce2fa43068e49812814b597493405bbf44c6dac85c109d
6
+ metadata.gz: b2b7b1e994250fce4fbabe3cf9571b62dc6432c7965d81897b54889a1be64198fb0dff9ba1ab83e91462985ee7644a5a3b00188b600b1d7fa4431d72e0ee99d6
7
+ data.tar.gz: e502b5deafbeb12839eb772b3be83720160ac8f1c99950fa527dcdef9da2e04df7aa55ffee1a32c6e037fa9931e3ec3f28e2e3470e545781e9855245b5db3d5d
data/Rakefile CHANGED
@@ -1,8 +1,13 @@
1
- require 'rake/testtask'
1
+ require "rake/testtask"
2
+ require "rspec/core/rake_task"
2
3
 
3
4
  Rake::TestTask.new do |t|
4
- t.libs << 'test'
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/bin/permit_params CHANGED
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'permit_params'
3
+ require "permit_params"
data/lib/permit_params.rb CHANGED
@@ -1,13 +1,27 @@
1
1
  module PermitParams
2
- def permitted_params(params, permitted)
3
- params.select do |k,v|
4
- permitted.keys.map(&:to_s).include?(k.to_s) && !v.nil? && coerce(v, permitted[k.to_sym])
2
+ class InvalidParameterError < StandardError
3
+ attr_accessor :param, :options
4
+ end
5
+
6
+ def permitted_params(params, permitted = {}, strong_validation = false)
7
+ return params if permitted.empty?
8
+
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
5
16
  end
17
+ coerced_params
6
18
  end
7
19
 
8
20
  private
9
21
 
10
- def coerce(param, type, strong = false, options = {})
22
+ Boolean = :boolean
23
+
24
+ def coerce(param, type, strong_validation = false, options = {})
11
25
  begin
12
26
  return nil if param.nil?
13
27
  return param if (param.is_a?(type) rescue false)
@@ -17,19 +31,27 @@ module PermitParams
17
31
  return Date.parse(param) if type == Date
18
32
  return Time.parse(param) if type == Time
19
33
  return DateTime.parse(param) if type == DateTime
20
- return Array(param.split(options[:delimiter] || ",")) if type == Array
21
- return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
22
- if [TrueClass, FalseClass, Boolean].include? type
23
- coerced = /^(false|f|no|n|0)$/i === param.to_s ? false : /^(true|t|yes|y|1)$/i === param.to_s ? true : nil
24
- raise ArgumentError if coerced.nil?
25
- return coerced
26
- end
34
+ return coerce_array(param) if type == Array
35
+ return coerce_hash(param) if type == Hash
36
+ return coerce_boolean(param) if [TrueClass, FalseClass, Boolean].include? type
27
37
  return nil
28
38
  rescue ArgumentError
29
- if strong
30
- raise InvalidParameterError, "'#{param}' is not a valid #{type}"
31
- end
39
+ raise InvalidParameterError, "'#{param}' is not a valid #{type}" if strong_validation
32
40
  end
33
41
  end
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
+
51
+ def coerce_boolean(param)
52
+ coerced = /^(false|f|no|n|0)$/i === param.to_s ? false : /^(true|t|yes|y|1)$/i === param.to_s ? true : nil
53
+ raise ArgumentError if coerced.nil?
54
+ coerced
55
+ end
34
56
  end
35
57
 
@@ -0,0 +1,105 @@
1
+ require "test/unit"
2
+ require "permit_params"
3
+ require "rspec"
4
+ require "rack/test"
5
+
6
+ include PermitParams
7
+
8
+ describe "exceptions" do
9
+ it "should raise error when at least one param is invalid" do
10
+ input = { param_1: "a" }
11
+ expect{
12
+ permitted_params(input, { param_1: Integer }, true)
13
+ }.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
14
+ end
15
+
16
+ it "should return a string when a pemitted is string" do
17
+ input = { param_1: "a string" }
18
+ expect(input).to eq permitted_params(input, { param_1: String })
19
+ end
20
+
21
+ it "should return a float when a pemitted is float" do
22
+ input = { param_1: 10.0 }
23
+ expect(input).to eq permitted_params(input, { param_1: Float })
24
+ end
25
+
26
+ it "should return a date when a pemitted is date" do
27
+ input = { param_1: Date.new }
28
+ expect(input).to eq permitted_params(input, { param_1: Date })
29
+ end
30
+
31
+ it "should return a time when a pemitted is time" do
32
+ input = { param_1: Time.new }
33
+ expect(input).to eq permitted_params(input, { param_1: Time })
34
+ end
35
+
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 })
39
+ end
40
+
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(output).to eq permitted_params(input, { param_1: Integer })
45
+ end
46
+
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 })
51
+ end
52
+
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 })
57
+ end
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
+
94
+ it "should remove a string when a pemitted is integer" do
95
+ input = { param_1: "a string" }
96
+ output = {}
97
+ expect(output).to eq permitted_params(input, { param_1: Integer })
98
+ end
99
+
100
+ it "should allow all params when no restriction is given" do
101
+ input = { param_1: "a string" }
102
+ expect(input).to eq permitted_params(input)
103
+ end
104
+ end
105
+
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.1
4
+ version: 0.0.5
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,8 +19,8 @@ files:
19
19
  - Rakefile
20
20
  - bin/permit_params
21
21
  - lib/permit_params.rb
22
- - test/test_permit_params.rb
23
- homepage: http://rubygems.org/gems/sinatra-my-params
22
+ - spec/permit_params_spec.rb
23
+ homepage: https://github.com/mhero/sinatra-my-params
24
24
  licenses: []
25
25
  metadata: {}
26
26
  post_install_message:
@@ -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,12 +0,0 @@
1
- require 'test/unit'
2
- require 'permit_params'
3
-
4
- class PermitParamsTest < Test::Unit::TestCase
5
- include PermitParams
6
-
7
- def test_pemit_string
8
- input = { param_1: "hola" }
9
- output = { param_1: "hola" }
10
- assert_equal output, permitted_params( input, { param_1: String } )
11
- end
12
- end