Class: AvroPinions::FileRegistry

Inherits:
SchemaRegistry show all
Defined in:
lib/avro_pinions/schema_registries/file_registry.rb

Instance Method Summary collapse

Methods inherited from SchemaRegistry

#canonical_name, #schema, #with_cache

Constructor Details

#initialize(options = {}) ⇒ FileRegistry

Returns a new instance of FileRegistry.



7
8
9
10
# File 'lib/avro_pinions/schema_registries/file_registry.rb', line 7

def initialize(options = {})
  super()
  @path = options[:schema_path] or raise AvroPinions::ConfigurationError.new("please define the path to load schemas from")
end

Instance Method Details

#file_path(schema_name, namespace) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/avro_pinions/schema_registries/file_registry.rb', line 18

def file_path(schema_name, namespace)
  schema_file = "#{schema_name}.avsc"
  boring_filename = File.join(@path, schema_file)
  if File.exists?(boring_filename)
    boring_filename
  else
    fancy_filename = File.join(@path, *namespace.split(/\./), schema_file)
    if File.exists?(fancy_filename)
      fancy_filename
    else
      raise "File not found for namespace #{namespace} and schema #{schema_name}. Tried #{fancy_filename} and #{boring_filename}"
    end
  end
end

#load_schema(schema_name, namespace) ⇒ Object



12
13
14
15
16
# File 'lib/avro_pinions/schema_registries/file_registry.rb', line 12

def load_schema(schema_name, namespace)
  data = File.read(file_path(schema_name, namespace))
  json = JSON.parse(data)
  Avro::Schema.real_parse(json)
end