Class: ChartMogul::Utils::JSONParser

Inherits:
Object
  • Object
show all
Defined in:
lib/chartmogul/utils/json_parser.rb

Class Method Summary collapse

Class Method Details

.opt_string_to_time(value) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/chartmogul/utils/json_parser.rb', line 22

def opt_string_to_time(value)
  return value unless value.instance_of?(String)

  parse_timestamp(value)
rescue ArgumentError
  value
end

.parse(json_string, skip_case_conversion: false) ⇒ Object



7
8
9
10
11
12
# File 'lib/chartmogul/utils/json_parser.rb', line 7

def parse(json_string, skip_case_conversion: false)
  hash = JSON.parse(json_string, symbolize_names: true)
  return hash if skip_case_conversion

  HashSnakeCaser.new(hash).to_snake_keys
end

.parse_timestamp(value) ⇒ Object



30
31
32
33
34
# File 'lib/chartmogul/utils/json_parser.rb', line 30

def parse_timestamp(value)
  Time.iso8601(value)
rescue ArgumentError
  Time.rfc2822(value)
end

.typecast_custom_attributes(custom_attributes) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/chartmogul/utils/json_parser.rb', line 14

def typecast_custom_attributes(custom_attributes)
  return {} unless custom_attributes

  custom_attributes.each_with_object({}) do |(key, value), hash|
    hash[key] = opt_string_to_time(value)
  end
end