Sha256: d35d72ab6ada178364b5308625267b6838883e1220cb13d60350277d4e2b0c05

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

module Avrocado
  class Serializer
    attr_reader :schema

    def initialize(schema)
      @schema = schema
    end

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

    def serialize(message)
      Avrocado::Encoder.new(message, schema).encoded
    end
  end

  class Encoder
    attr_reader :message, :schema

    def initialize(message, schema)
      @message = message
      @schema  = schema
    end

    def encoded
      message.each { |field, value| record.put(field.to_s, value) }

      writer.write(record, encoder)

      encoder.flush

      output.to_byte_array
    end

    private

    # TODO: explore whether we can reuse the Java-based objects below
    #       if so, we can reuse an instance of a Serializer

    def record
      @record ||= Java::Avro::GenericData::Record.new(schema)
    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

3 entries across 3 versions & 1 rubygems

Version Path
avrocado-0.2.2 lib/avrocado/serializer.rb
avrocado-0.2.1 lib/avrocado/serializer.rb
avrocado-0.2.0 lib/avrocado/serializer.rb