Class: AFCSalesforce::Tools::Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/afc_salesforce/tools/utilities.rb

Constant Summary collapse

SUPPORTED_TYPES =
[:integer, :float, :date, :boolean].freeze

Class Method Summary collapse

Class Method Details

.parse_by_type(input, type) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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