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



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

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

  parse_timestamp(value)
rescue ArgumentError
  value
end

.parse(json_string, immutable_keys: []) ⇒ Object



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

def parse(json_string, immutable_keys: [])
  hash = JSON.parse(json_string, symbolize_names: true)
  HashSnakeCaser.new(hash, immutable_keys: immutable_keys).to_snake_keys
end

.parse_timestamp(value) ⇒ Object



28
29
30
31
32
# File 'lib/chartmogul/utils/json_parser.rb', line 28

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

.typecast_custom_attributes(custom_attributes) ⇒ Object



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

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