Class: AvroTurf

Inherits:
Object
  • Object
show all
Defined in:
lib/avro_turf.rb,
lib/avro_turf/version.rb,
lib/avro_turf/messaging.rb,
lib/avro_turf/schema_to_avro_patch.rb

Defined Under Namespace

Modules: AvroGemPatch Classes: CachedSchemaRegistry, Error, Messaging, SchemaError, SchemaNotFoundError, SchemaRegistry, SchemaStore

Constant Summary collapse

DEFAULT_SCHEMAS_PATH =
"./schemas"
VERSION =
"0.7.4"

Instance Method Summary collapse

Constructor Details

#initialize(schemas_path: nil, namespace: nil, codec: nil) ⇒ AvroTurf

Create a new AvroTurf instance with the specified configuration.

schemas_path - The String path to the root directory containing Avro schemas (default: “./schemas”). namespace - The String namespace that should be used to qualify schema names (optional). codec - The String name of a codec that should be used to compress messages (optional).

Currently, the only valid codec name is ‘deflate`.



22
23
24
25
26
# File 'lib/avro_turf.rb', line 22

def initialize(schemas_path: nil, namespace: nil, codec: nil)
  @namespace = namespace
  @schema_store = SchemaStore.new(path: schemas_path || DEFAULT_SCHEMAS_PATH)
  @codec = codec
end

Instance Method Details

#decode(encoded_data, schema_name: nil, namespace: @namespace) ⇒ Object

Decodes Avro data.

encoded_data - A String containing Avro-encoded data. schema_name - The String name of the schema that should be used to read

the data. If nil, the writer schema will be used.

namespace - The namespace of the Avro schema used to decode the data.

Returns whatever is encoded in the data.



67
68
69
70
# File 'lib/avro_turf.rb', line 67

def decode(encoded_data, schema_name: nil, namespace: @namespace)
  stream = StringIO.new(encoded_data)
  decode_stream(stream, schema_name: schema_name, namespace: namespace)
end

#decode_stream(stream, schema_name: nil, namespace: @namespace) ⇒ Object

Decodes Avro data from an IO stream.

stream - An IO object containing Avro data. schema_name - The String name of the schema that should be used to read

the data. If nil, the writer schema will be used.

namespace - The namespace of the Avro schema used to decode the data.

Returns whatever is encoded in the stream.



80
81
82
83
84
85
# File 'lib/avro_turf.rb', line 80

def decode_stream(stream, schema_name: nil, namespace: @namespace)
  schema = schema_name && @schema_store.find(schema_name, namespace)
  reader = Avro::IO::DatumReader.new(nil, schema)
  dr = Avro::DataFile::Reader.new(stream, reader)
  dr.first
end

#encode(data, schema_name: nil, namespace: @namespace) ⇒ Object

Encodes data to Avro using the specified schema.

data - The data that should be encoded. schema_name - The name of a schema in the ‘schemas_path`.

Returns a String containing the encoded data.



34
35
36
37
38
39
40
# File 'lib/avro_turf.rb', line 34

def encode(data, schema_name: nil, namespace: @namespace)
  stream = StringIO.new

  encode_to_stream(data, stream: stream, schema_name: schema_name, namespace: namespace)

  stream.string
end

#encode_to_stream(data, schema_name: nil, stream: nil, namespace: @namespace) ⇒ Object

Encodes data to Avro using the specified schema and writes it to the specified stream.

data - The data that should be encoded. schema_name - The name of a schema in the ‘schemas_path`. stream - An IO object that the encoded data should be written to (optional).

Returns nothing.



50
51
52
53
54
55
56
57
# File 'lib/avro_turf.rb', line 50

def encode_to_stream(data, schema_name: nil, stream: nil, namespace: @namespace)
  schema = @schema_store.find(schema_name, namespace)
  writer = Avro::IO::DatumWriter.new(schema)

  dw = Avro::DataFile::Writer.new(stream, writer, schema, @codec)
  dw << data.as_avro
  dw.close
end

#load_schemas!Object

Loads all schema definition files in the ‘schemas_dir`.



102
103
104
# File 'lib/avro_turf.rb', line 102

def load_schemas!
  @schema_store.load_schemas!
end

#valid?(data, schema_name: nil, namespace: @namespace) ⇒ Boolean

Validates data against an Avro schema.

data - The data that should be validated. schema - The String name of the schema that should be used to validate

the data.

namespace - The namespace of the Avro schema (optional).

Returns true if the data is valid, false otherwise.

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/avro_turf.rb', line 95

def valid?(data, schema_name: nil, namespace: @namespace)
  schema = schema_name && @schema_store.find(schema_name, namespace)

  Avro::Schema.validate(schema, data.as_avro)
end