Skip to content

Commit

Permalink
Add only, except, add options
Browse files Browse the repository at this point in the history
  • Loading branch information
bguest committed Aug 21, 2015
1 parent 0491aae commit 169652c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ the new value if it succeeds
Integer: Saneitized.convert('42') #=> 42
Float: Saneitized.convert('22.2') #=> 22.2
JSON: Saneitized.convert("{\"hello\":\"goodbye\"}") #=> {"hello"=>"goodbye"}
Time: Saneitized.convert("2014-05-28T23:15:26Z") #=> 2014-05-28 23:15:26 UTC

Additionaly you can sanitize time as well

Time: Saneitized.convert("2014-05-28T23:15:26Z", :add => :time) #=> 2014-05-28 23:15:26 UTC

Time is left out of the default sanitizers because the Chronic parser used is pretty agressive and
will convert some things you might not thing would be time.

You can checkout `lib/saneitized/converter.rb` for more information

Expand Down Expand Up @@ -78,6 +84,24 @@ You can also black list keys of hashes if thats your thing
Saneitized.convert( {name:'12345', age:'21'}, :key_blacklist => :name}) #=> {name:'12345', age: 21}
Saneitized.convert( {name:'12345', 'age' => '21'}, :key_blacklist => [:name, 'age'}) #=> {name:'12345', 'age' => '21'}

### Sanitizers

By default convert runs through the sanitizers, but you can pick and choose what sanitizers you want to use.

You can select to 'only' use certian converter

Saneitized.convert( {a_float:'12.34', an_integer:'1234'}, :only => [:integer] ) #=> {a_float:'12.34', an_integer: 1234}

You can choose to use all the sanitizers 'except' a selection

Saneitized.convert( {a_float:'12.34', an_integer:'1234'}, :except => :float ) #=> {a_float:'12.34', an_integer: 1234}

You can also add sanitizers that are overly agressive and not part of the default set

Saneitized.convert( "2001-02-03 10:11:12 -0400", add: :time) #=> 2001-02-03 10:11:12 -0400

You can pass these options as a single symbol or as an array of symbols

### Important Notes

To convert a sanetized array/hash back to a non-saneitized hash, simply call the #to_a and #to_h
Expand Down
12 changes: 10 additions & 2 deletions lib/saneitized/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ module Saneitized

def self.convert(unknown, options = {})
options[:blacklist] ||= nil
options[:except] ||= []
options[:add] ||= []
options[:only] ||= %w(true false nil integer float json)

return Saneitized::Hash.new(unknown, options) if unknown.is_a? ::Hash
return Saneitized::Array.new(unknown, options) if unknown.is_a? ::Array
return unknown unless unknown.is_a? String #Only attempt to convert string
return unknown if Array(options[:blacklist]).include?(unknown)

%w(true false nil integer float json time).each do |type|
except = Array(options[:except]).map(&:to_s)
only = Array(options[:only]).map(&:to_s)
add = Array(options[:add]).map(&:to_s)

sanitizers = (only + add).uniq - except

sanitizers.each do |type|
value = Converter.send(type + '?', unknown)
next if value == :nope
return (type == 'json') ? convert(value, options) : value
Expand All @@ -20,7 +29,6 @@ def self.convert(unknown, options = {})
unknown
end


module Converter
extend self

Expand Down
2 changes: 1 addition & 1 deletion lib/saneitized/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Saneitized
VERSION = '1.5.0'
VERSION = '2.0.0'
end
28 changes: 24 additions & 4 deletions spec/saneitized/converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
it 'should convert array' do
insane = ['1', '3', '2014-05-29 19:19:44 -0400']
sane = [1, 3, Time.new(2014,5,29,19,19,44,'-04:00')]
expect(Saneitized.convert(insane)).to eql sane
expect(Saneitized.convert(insane, add: :time)).to eql sane
end

it 'should convert json' do
Expand Down Expand Up @@ -59,16 +59,16 @@
end

it 'should convert datetime string' do
expect(Saneitized.convert("2001-02-03 10:11:12 -0400")).to eql Time.new(2001,2,3,10,11,12,'-04:00')
expect(Saneitized.convert("2001-02-03 10:11:12 -0400", add: :time)).to eql Time.new(2001,2,3,10,11,12,'-04:00')
end

%w(marketplaces).each do |string|
["marketplaces", "cpr-first-aid.wonderhowto.com"].each do |string|
it "should leave #{string} alone" do
expect(Saneitized.convert(string)).to eq string
end
end

context 'with blacklist' do
context 'with blacklist' do
it 'should not convertet blacklisted item' do
expect(Saneitized.convert('day', blacklist:'day')).to eql 'day'
end
Expand All @@ -90,5 +90,25 @@
expect(sane).to eq expected
end
end

let(:input){ { integer:'12345', float:'12.345', true:'true', false:'false' } }

context 'with except' do
it 'should not convert ' do
expected = {integer: 12345, float:'12.345', true: 'true', false: false }
sane = Saneitized.convert( input, except:[:float, :true] )
expect(sane).to eq expected
end
end

context 'with only' do
it 'should only used passed in converters' do
expected = {integer: 12345.0, float: 12.345, true: true, false:'false'}
sane = Saneitized.convert( input, only:[:float, :true, :false], except: :false)
expect(sane).to eq expected
end
end


end
end

0 comments on commit 169652c

Please sign in to comment.