Module: Saneitized

Defined in:
lib/saneitized/hash.rb,
lib/saneitized/array.rb,
lib/saneitized/version.rb,
lib/saneitized/converter.rb

Defined Under Namespace

Modules: Converter Classes: Array, Hash

Constant Summary collapse

VERSION =
'2.0.0'

Class Method Summary collapse

Class Method Details

.convert(unknown, options = {}) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/saneitized/converter.rb', line 6

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)

  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
  end

  unknown
end