class Avrocado::Encoder

Attributes

message[R]
schema[R]

Public Class Methods

new(message, schema) click to toggle source
# File lib/avrocado/serializer.rb, line 21
def initialize(message, schema)
  @message = message
  @schema  = schema
end

Public Instance Methods

encoded() click to toggle source
# File lib/avrocado/serializer.rb, line 26
def encoded
  message.each { |field, value| record.put(field.to_s, value) }

  writer.write(record, encoder)

  encoder.flush

  output.to_byte_array
end

Private Instance Methods

encoder() click to toggle source
# File lib/avrocado/serializer.rb, line 49
def encoder
  @encoder ||= Avrocado::EncoderFactory.new.binary_encoder(output, nil)
end
output() click to toggle source
# File lib/avrocado/serializer.rb, line 45
def output
  @output ||= java.io.ByteArrayOutputStream.new
end
record() click to toggle source

TODO: explore whether we can reuse the Java-based objects below

if so, we can reuse an instance of a Serializer
# File lib/avrocado/serializer.rb, line 41
def record
  @record ||= Java::Avro::GenericData::Record.new(schema)
end
writer() click to toggle source
# File lib/avrocado/serializer.rb, line 53
def writer
  @writer ||= Java::Avro::GenericDatumWriter.new(schema)
end