saneitized 1.5.0 → 2.0.0
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 +4 -4
- data/README.md +25 -1
- data/lib/saneitized/converter.rb +10 -2
- data/lib/saneitized/version.rb +1 -1
- data/spec/saneitized/converter_spec.rb +24 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f390ff80b0e9ce1850fd6653214bd1fb6afced93
|
4
|
+
data.tar.gz: e068a16d99d010429df56ad57b72ba6e11ddb594
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bcb8bcef97aeba5d342666c6f7a81943b82d018b329a27276cc28bc49c3f6c1c2ce0ca3e183742ac9d417b80a4a891a511f9f912efbbc2b87773f47b4ccfa843
|
7
|
+
data.tar.gz: b0305d4e33ca6033ef3e165946873b4e6df76afcafe8394fd1674a5c32e3b9e65273f6214cdb4cbd34e648e0d8f48842531748a15166b2b5734725fa511d21bb
|
data/README.md
CHANGED
@@ -37,7 +37,13 @@ the new value if it succeeds
|
|
37
37
|
Integer: Saneitized.convert('42') #=> 42
|
38
38
|
Float: Saneitized.convert('22.2') #=> 22.2
|
39
39
|
JSON: Saneitized.convert("{\"hello\":\"goodbye\"}") #=> {"hello"=>"goodbye"}
|
40
|
-
|
40
|
+
|
41
|
+
Additionaly you can sanitize time as well
|
42
|
+
|
43
|
+
Time: Saneitized.convert("2014-05-28T23:15:26Z", :add => :time) #=> 2014-05-28 23:15:26 UTC
|
44
|
+
|
45
|
+
Time is left out of the default sanitizers because the Chronic parser used is pretty agressive and
|
46
|
+
will convert some things you might not thing would be time.
|
41
47
|
|
42
48
|
You can checkout `lib/saneitized/converter.rb` for more information
|
43
49
|
|
@@ -78,6 +84,24 @@ You can also black list keys of hashes if thats your thing
|
|
78
84
|
Saneitized.convert( {name:'12345', age:'21'}, :key_blacklist => :name}) #=> {name:'12345', age: 21}
|
79
85
|
Saneitized.convert( {name:'12345', 'age' => '21'}, :key_blacklist => [:name, 'age'}) #=> {name:'12345', 'age' => '21'}
|
80
86
|
|
87
|
+
### Sanitizers
|
88
|
+
|
89
|
+
By default convert runs through the sanitizers, but you can pick and choose what sanitizers you want to use.
|
90
|
+
|
91
|
+
You can select to 'only' use certian converter
|
92
|
+
|
93
|
+
Saneitized.convert( {a_float:'12.34', an_integer:'1234'}, :only => [:integer] ) #=> {a_float:'12.34', an_integer: 1234}
|
94
|
+
|
95
|
+
You can choose to use all the sanitizers 'except' a selection
|
96
|
+
|
97
|
+
Saneitized.convert( {a_float:'12.34', an_integer:'1234'}, :except => :float ) #=> {a_float:'12.34', an_integer: 1234}
|
98
|
+
|
99
|
+
You can also add sanitizers that are overly agressive and not part of the default set
|
100
|
+
|
101
|
+
Saneitized.convert( "2001-02-03 10:11:12 -0400", add: :time) #=> 2001-02-03 10:11:12 -0400
|
102
|
+
|
103
|
+
You can pass these options as a single symbol or as an array of symbols
|
104
|
+
|
81
105
|
### Important Notes
|
82
106
|
|
83
107
|
To convert a sanetized array/hash back to a non-saneitized hash, simply call the #to_a and #to_h
|
data/lib/saneitized/converter.rb
CHANGED
@@ -5,13 +5,22 @@ module Saneitized
|
|
5
5
|
|
6
6
|
def self.convert(unknown, options = {})
|
7
7
|
options[:blacklist] ||= nil
|
8
|
+
options[:except] ||= []
|
9
|
+
options[:add] ||= []
|
10
|
+
options[:only] ||= %w(true false nil integer float json)
|
8
11
|
|
9
12
|
return Saneitized::Hash.new(unknown, options) if unknown.is_a? ::Hash
|
10
13
|
return Saneitized::Array.new(unknown, options) if unknown.is_a? ::Array
|
11
14
|
return unknown unless unknown.is_a? String #Only attempt to convert string
|
12
15
|
return unknown if Array(options[:blacklist]).include?(unknown)
|
13
16
|
|
14
|
-
|
17
|
+
except = Array(options[:except]).map(&:to_s)
|
18
|
+
only = Array(options[:only]).map(&:to_s)
|
19
|
+
add = Array(options[:add]).map(&:to_s)
|
20
|
+
|
21
|
+
sanitizers = (only + add).uniq - except
|
22
|
+
|
23
|
+
sanitizers.each do |type|
|
15
24
|
value = Converter.send(type + '?', unknown)
|
16
25
|
next if value == :nope
|
17
26
|
return (type == 'json') ? convert(value, options) : value
|
@@ -20,7 +29,6 @@ module Saneitized
|
|
20
29
|
unknown
|
21
30
|
end
|
22
31
|
|
23
|
-
|
24
32
|
module Converter
|
25
33
|
extend self
|
26
34
|
|
data/lib/saneitized/version.rb
CHANGED
@@ -11,7 +11,7 @@ describe Saneitized do
|
|
11
11
|
it 'should convert array' do
|
12
12
|
insane = ['1', '3', '2014-05-29 19:19:44 -0400']
|
13
13
|
sane = [1, 3, Time.new(2014,5,29,19,19,44,'-04:00')]
|
14
|
-
expect(Saneitized.convert(insane)).to eql sane
|
14
|
+
expect(Saneitized.convert(insane, add: :time)).to eql sane
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'should convert json' do
|
@@ -59,16 +59,16 @@ describe Saneitized do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it 'should convert datetime string' do
|
62
|
-
expect(Saneitized.convert("2001-02-03 10:11:12 -0400")).to eql Time.new(2001,2,3,10,11,12,'-04:00')
|
62
|
+
expect(Saneitized.convert("2001-02-03 10:11:12 -0400", add: :time)).to eql Time.new(2001,2,3,10,11,12,'-04:00')
|
63
63
|
end
|
64
64
|
|
65
|
-
|
65
|
+
["marketplaces", "cpr-first-aid.wonderhowto.com"].each do |string|
|
66
66
|
it "should leave #{string} alone" do
|
67
67
|
expect(Saneitized.convert(string)).to eq string
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
-
context 'with blacklist' do
|
71
|
+
context 'with blacklist' do
|
72
72
|
it 'should not convertet blacklisted item' do
|
73
73
|
expect(Saneitized.convert('day', blacklist:'day')).to eql 'day'
|
74
74
|
end
|
@@ -90,5 +90,25 @@ describe Saneitized do
|
|
90
90
|
expect(sane).to eq expected
|
91
91
|
end
|
92
92
|
end
|
93
|
+
|
94
|
+
let(:input){ { integer:'12345', float:'12.345', true:'true', false:'false' } }
|
95
|
+
|
96
|
+
context 'with except' do
|
97
|
+
it 'should not convert ' do
|
98
|
+
expected = {integer: 12345, float:'12.345', true: 'true', false: false }
|
99
|
+
sane = Saneitized.convert( input, except:[:float, :true] )
|
100
|
+
expect(sane).to eq expected
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'with only' do
|
105
|
+
it 'should only used passed in converters' do
|
106
|
+
expected = {integer: 12345.0, float: 12.345, true: true, false:'false'}
|
107
|
+
sane = Saneitized.convert( input, only:[:float, :true, :false], except: :false)
|
108
|
+
expect(sane).to eq expected
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
|
93
113
|
end
|
94
114
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: saneitized
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Guest
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chronic
|