Sha256: c21742b959892c3f02d79e128a3b2332c48dfd5fd24c464431d564fd1b0a7a15

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 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

  module AllowNil
    def call(value)
      if value.nil? || value == ""
        nil
      else
        super(value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
input_sanitizer-0.1.10 lib/input_sanitizer/default_converters.rb