class AFCSalesforce::Tools::Utilities

Constants

SUPPORTED_TYPES

Public Class Methods

parse_by_type(input, type) click to toggle source
# File lib/afc_salesforce/tools/utilities.rb, line 8
def self.parse_by_type(input, type)
  return input if input.nil?
  raise UnsupportedTypeError unless SUPPORTED_TYPES.include?(type)

  output = input
  case type
    when :integer
      output_sub   = input.to_s.sub(/,/,'')
      output_match = output_sub.match(/[0-9|\.]+/)
      output       = output_match.to_s.to_i
    when :float
      output_sub   = input.to_s.sub(/,/,'')
      output_match = output_sub.match(/[0-9|\.]+/)
      output       = output_match.to_s.to_f
    when :date
      begin
        parsed_date = Time.strptime(input, "%m/%d/%Y")
        output      = parsed_date.strftime("%Y-%m-%d")
      rescue ArgumentError => e
        output = input
      end
    when :boolean
      output = false
      if input == false || input == '0' || input == 0
        output = false
      end

      if input == true || input == '1' || input == 1
        output = true
      end
      output
  end

  return output
end