Class: AvroWrapper

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ AvroWrapper

Returns a new instance of AvroWrapper.



4
5
6
# File 'lib/avro_wrapper.rb', line 4

def initialize(args={})
  args.each {|k,v| send("#{k}=",v)}
end

Class Method Details

.from_avro_string(str) ⇒ Object



31
32
33
34
35
36
37
38
# 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

.schema_docObject



8
9
10
# File 'lib/avro_wrapper.rb', line 8

def self.schema_doc
  "NOT INITIALIZED"
end

Instance Method Details

#schema_docObject



12
13
14
# File 'lib/avro_wrapper.rb', line 12

def schema_doc
  self.class.schema_doc
end

#to_avro_stringObject



22
23
24
25
26
27
28
29
# 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_hObject



16
17
18
19
20
# 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