Sha256: f706e391cbc13efd071b7684fa2f0398b71cd2f7177d1f4362ed77554c603c00

Contents?: true

Size: 1.66 KB

Versions: 8

Compression:

Stored size: 1.66 KB

Contents

require 'oj'
require 'stringio'
require 'hyperion/aux/logger'
require 'abstractivator/enum'
require 'hyperion/types/multipart'

class Hyperion
  module Formats
    # Serializes and deserializes the supported formats.
    # This is done as gracefully as possible.

    include Hyperion::Logger

    define_enum :Known, :json, :protobuf

    def write(obj, format)
      return obj.body if obj.is_a?(Multipart)
      return obj if obj.is_a?(String) || obj.nil? || format.nil?

      case Formats.get_from(format)
      when :json; write_json(obj)
      when :protobuf; obj
      else; fail "Unsupported format: #{format}"
      end
    end

    def read(bytes, format)
      return nil if bytes.nil?
      return bytes if format.nil?

      case Formats.get_from(format)
      when :json; read_json(bytes)
      when :protobuf; bytes
      when Multipart.format; bytes # currently only used for testing purposes
      else; fail "Unsupported format: #{format}"
      end
    end

    def self.get_from(x)
      x.respond_to?(:format) ? x.format : x
    end

    private

    def write_json(obj)
      Oj.dump(obj, oj_options)
    end

    def read_json(bytes)
      begin
        Oj.compat_load(bytes, oj_options)
      rescue Oj::ParseError => e
        logger.error e.message
        bytes
      end
    end

    def oj_options
      {
          mode: :compat,
          time_format: :xmlschema,  # xmlschema == iso8601
          use_to_json: false,
          second_precision: 3,
          bigdecimal_load: :float
      }
    end

    def get_oj_line_and_col(e)
      m = e.message.match(/at line (?<line>\d+), column (?<col>\d+)/)
      m ? [m[:line].to_i, m[:col].to_i] : nil
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hyperion_http-0.3.0 lib/hyperion/formats.rb
hyperion_http-0.2.4 lib/hyperion/formats.rb
hyperion_http-0.2.3 lib/hyperion/formats.rb
hyperion_http-0.2.2 lib/hyperion/formats.rb
hyperion_http-0.2.1 lib/hyperion/formats.rb
hyperion_http-0.1.9 lib/hyperion/formats.rb
hyperion_http-0.1.8 lib/hyperion/formats.rb
hyperion_http-0.1.7 lib/hyperion/formats.rb