class AvroWrapper

Public Class Methods

from_avro_string(str) click to toggle source
# File lib/avro_wrapper.rb, line 31
def self.from_avro_string(str)
  schema = Avro::Schema.parse(schema_doc)
  raw_data = StringIO.new(str)
  decoder = Avro::IO::BinaryDecoder.new(raw_data)
  datum_reader = Avro::IO::DatumReader.new(schema)
  object_hash = datum_reader.read(decoder)
  eval "#{self}.new(#{object_hash})"
end
new(args={}) click to toggle source
# File lib/avro_wrapper.rb, line 4
def initialize(args={})
  args.each {|k,v| send("#{k}=",v)}
end
schema_doc() click to toggle source
# File lib/avro_wrapper.rb, line 8
def self.schema_doc
  "NOT INITIALIZED"
end

Public Instance Methods

schema_doc() click to toggle source
# File lib/avro_wrapper.rb, line 12
def schema_doc
  self.class.schema_doc
end
to_avro_string() click to toggle source
# File lib/avro_wrapper.rb, line 22
def to_avro_string
  schema = Avro::Schema.parse(schema_doc)
  dw = Avro::IO::DatumWriter.new(schema)
  buffer = StringIO.new("".force_encoding("BINARY"))
  encoder = Avro::IO::BinaryEncoder.new(buffer)
  dw.write(self.to_h,encoder)
  buffer.string
end
to_h() click to toggle source
# File lib/avro_wrapper.rb, line 16
def to_h
  Hash[instance_variables.map do |k|
    [k.to_s.gsub(/^@/,""),eval(k.to_s)]
  end]
end