Sha256: 7280b31b1fa59600de1b81d6f2ff3c61cd8a88efd1dc5f65f789abf062f075e6
Contents?: true
Size: 1.78 KB
Versions: 2
Compression:
Stored size: 1.78 KB
Contents
require 'time' module InputSanitizer class ConversionError < Exception end class IntegerConverter def call(value) cast = value.to_i if cast.to_s != value.to_s raise ConversionError.new("invalid integer") end cast end end class StringConverter def call(value) value.to_s end end class DateConverter ISO_RE = /\A\d{4}-?\d{2}-?\d{2}/ def call(value) raise ConversionError.new("invalid time") unless value =~ ISO_RE Date.parse(value) rescue ArgumentError raise ConversionError.new("invalid iso8601 date") end end class TimeConverter ISO_RE = /\A\d{4}-?\d{2}-?\d{2}([T ]?\d{2}(:?\d{2}(:?\d{2})?)?)?\Z/ def call(value) if value =~ ISO_RE strip_timezone(Time.parse(value)) else raise ConversionError.new("invalid time") end rescue ArgumentError raise ConversionError.new("invalid time") end def strip_timezone(time) Time.utc(time.year, time.month, time.day, time.hour, time.min, time.sec) end end class BooleanConverter BOOLEAN_MAP = { true => true, false => false, 'true' => true, 'false' => false, '1' => true, '0' => false, 'yes' => true, 'no' => false, } def call(value) if BOOLEAN_MAP.has_key?(value) BOOLEAN_MAP[value] else truthy, falsy = BOOLEAN_MAP.partition { |_, value| value } truthy = truthy.map { |e| "'#{e[0]}'" }.uniq falsy = falsy.map { |e| "'#{e[0]}'" }.uniq message = "Invalid boolean: use " message += truthy.join(", ") message += " for true, or " message += falsy.join(", ") message += " for false." raise ConversionError.new(message) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
input_sanitizer-0.1.9 | lib/input_sanitizer/default_converters.rb |
input_sanitizer-0.1.8 | lib/input_sanitizer/default_converters.rb |