Class: Deimos::AvroDataEncoder

Inherits:
AvroDataCoder show all
Defined in:
lib/deimos/avro_data_encoder.rb

Overview

Service Object to decode Avro messages.

Instance Attribute Summary

Attributes inherited from AvroDataCoder

#config, #namespace, #schema, #schema_store

Instance Method Summary collapse

Methods inherited from AvroDataCoder

#avro_schema, #initialize

Constructor Details

This class inherits a constructor from Deimos::AvroDataCoder

Instance Method Details

#encode(payload, schema: nil, topic: nil) ⇒ String



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/deimos/avro_data_encoder.rb', line 28

def encode(payload, schema: nil, topic: nil)
  schema ||= @schema
  Avro::SchemaValidator.validate!(avro_schema(schema), payload,
                                  recursive: true,
                                  fail_on_extra_fields: true)
  avro_turf_messaging.encode(payload, schema_name: schema, subject: topic)
rescue Avro::IO::AvroTypeError
  # throw a more detailed error
  schema = @schema_store.find(@schema, @namespace)
  Avro::SchemaValidator.validate!(schema, payload)
end

#encode_key(key_id, key, topic = nil) ⇒ String



44
45
46
47
48
49
# File 'lib/deimos/avro_data_encoder.rb', line 44

def encode_key(key_id, key, topic=nil)
  key_schema = _generate_key_schema(key_id)
  field_name = _field_name_from_schema(key_schema)
  payload = { field_name => key }
  encode(payload, schema: key_schema['name'], topic: topic)
end

#encode_local(payload, schema: nil) ⇒ String



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/deimos/avro_data_encoder.rb', line 12

def encode_local(payload, schema: nil)
  schema ||= @schema
  Avro::SchemaValidator.validate!(avro_schema(schema), payload,
                                  recursive: true,
                                  fail_on_extra_fields: true)
  avro_turf.encode(payload, schema_name: schema, namespace: @namespace)
rescue Avro::IO::AvroTypeError
  # throw a more detailed error
  value_schema = @schema_store.find(schema, @namespace)
  Avro::SchemaValidator.validate!(value_schema, payload)
end