module Hyperion::Formats

Public Class Methods

get_from(x) click to toggle source
# File lib/hyperion/formats.rb, line 39
def self.get_from(x)
  x.respond_to?(:format) ? x.format : x
end

Public Instance Methods

read(bytes, format) click to toggle source
# File lib/hyperion/formats.rb, line 27
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
write(obj, format) click to toggle source
# File lib/hyperion/formats.rb, line 16
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

Private Instance Methods

get_oj_line_and_col(e) click to toggle source
# File lib/hyperion/formats.rb, line 67
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
oj_options() click to toggle source
# File lib/hyperion/formats.rb, line 58
def oj_options
  {
      mode: :rails,
      time_format: :xmlschema,  # xmlschema == iso8601
      second_precision: 3,
      bigdecimal_load: :float
  }
end
read_json(bytes) click to toggle source
# File lib/hyperion/formats.rb, line 49
def read_json(bytes)
  begin
    Oj.compat_load(bytes, oj_options)
  rescue Oj::ParseError => e
    logger.error e.message
    bytes
  end
end
write_json(obj) click to toggle source
# File lib/hyperion/formats.rb, line 45
def write_json(obj)
  Oj.dump(obj, oj_options)
end