Sha256: 735898e97411d60745396e779b3a9bb108df68870b0dd375f736df7d078f23f0

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module CycloneLariat
  class MessagesMapper
    class << self
      def from_row(row)
        return if row.nil?

        row[:data] = hash_from_json_column(row[:data])
        row[:client_error_details] = hash_from_json_column(row[:client_error_details]) if row[:client_error_details]
        row
      end

      def to_row(input)
        row = {
          uuid:                   input.uuid,
          kind:                   input.kind,
          type:                   input.type,
          publisher:              input.publisher,
          data:                   JSON.generate(input.data),
          client_error_message:   input.client_error&.message,
          client_error_details:   JSON.generate(input.client_error&.details),
          version:                input.version,
          sent_at:                input.sent_at
        }
        row
      end

      private

      def hash_from_json_column(data)
        return JSON.parse(data) if data.is_a?(String)

        if pg_json_extension_enabled?
          return data.to_h             if data.is_a?(Sequel::Postgres::JSONHash)
          return JSON.parse(data.to_s) if data.is_a?(Sequel::Postgres::JSONString)
        end

        raise ArgumentError, "Unknown type of `#{data}`"
      end

      def pg_json_extension_enabled?
        Object.const_defined?('Sequel::Postgres::JSONHash')
      end
    end
  end
end


Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cyclone_lariat-0.3.5 lib/cyclone_lariat/messages_mapper.rb
cyclone_lariat-0.3.4 lib/cyclone_lariat/messages_mapper.rb