Sha256: 1ebae936d93cae11a2a4c65e0b85468fd747d0c85f21abac1e51bf4af7289d14

Contents?: true

Size: 943 Bytes

Versions: 4

Compression:

Stored size: 943 Bytes

Contents

# frozen_string_literal: true

module ChartMogul
  module Utils
    class JSONParser
      class << self
        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

        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

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

          parse_timestamp(value)
        rescue ArgumentError
          value
        end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chartmogul-ruby-1.7.1 lib/chartmogul/utils/json_parser.rb
chartmogul-ruby-1.6.9 lib/chartmogul/utils/json_parser.rb
chartmogul-ruby-1.6.8 lib/chartmogul/utils/json_parser.rb
chartmogul-ruby-1.6.7 lib/chartmogul/utils/json_parser.rb