Class: AvroPinions::Codec::SingleObjectCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/avro_pinions/codec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ SingleObjectCodec

Returns a new instance of SingleObjectCodec.



42
43
44
# File 'lib/avro_pinions/codec.rb', line 42

def initialize(schema)
  @schema = schema
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



41
42
43
# File 'lib/avro_pinions/codec.rb', line 41

def schema
  @schema
end

Instance Method Details

#encode(data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/avro_pinions/codec.rb', line 46

def encode(data)
  stream = StringIO.new
  writer = Avro::IO::DatumWriter.new(@schema)
  encoder = Avro::IO::BinaryEncoder.new(stream)

  smd = Avro::IO::SingleObjectMessage.new(@schema)
  smd.header.each do |segment|
    stream.write( segment.chr )
  end
  writer.write(data, encoder)

  stream.string
end