Sha256: 10bd54e4eb21fb5a2294419492478a848f47f7723bd26e26341c77ef323504b8

Contents?: true

Size: 833 Bytes

Versions: 2

Compression:

Stored size: 833 Bytes

Contents

module Avrocado
  class Serializer
    attr_reader :message, :schema

    def initialize(message, schema = Schema.parse)
      @message = message
      @schema  = schema
    end

    def self.serialize(message)
      new(message).serialize
    end

    def serialize
      message.each do |field, value|
        record.put(field.to_s, value)
      end

      writer.write(record, encoder)

      encoder.flush

      output.to_byte_array
    end

    private

    def record
      @record ||= Java::Avro::GenericData::Record.new(Avrocado::Schema.parse)
    end

    def output
      @output ||= java.io.ByteArrayOutputStream.new
    end

    def encoder
      @encoder ||= Avrocado::EncoderFactory.new.binary_encoder(output, nil)
    end

    def writer
      @writer ||= Java::Avro::GenericDatumWriter.new(schema)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
avrocado-0.1.1 lib/avrocado/serializer.rb
avrocado-0.1.0 lib/avrocado/serializer.rb