Class: FlippRubyKafka::Utils::PlatformSchemaValidation

Inherits:
Object
  • Object
show all
Defined in:
lib/flipp_ruby_kafka/utils/platform_schema_validation.rb

Overview

Generates the Microservice Platform schema validation file. Run this before committing schema changes (or include it in your CircleCI config if you don’t mind having to build the same image multiple times). Run this using ‘rails runner` or in Rails console if you’re using Rails.

Instance Method Summary collapse

Constructor Details

#initialize(base_path: '') ⇒ PlatformSchemaValidation

Returns a new instance of PlatformSchemaValidation.

Parameters:

  • base_path (String) (defaults to: '')

    Set this to Rails.root if running Rails.



13
14
15
# File 'lib/flipp_ruby_kafka/utils/platform_schema_validation.rb', line 13

def initialize(base_path: '')
  @base_path ||= FlippRubyKafka.config.schema.path.gsub(%r{^#{base_path}\/}, '')
end

Instance Method Details

#generate_configString

Returns:

  • (String)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/flipp_ruby_kafka/utils/platform_schema_validation.rb', line 49

def generate_config
  @full_config = []
  FlippRubyKafka::Producer.descendants.each do |klass|
    next if klass.topic.blank?

    config = process_producer(klass)
    @full_config << config if config
  end

  Phobos.config['listeners'].each do |h|
    config = process_consumer(h)
    @full_config << config if config
  end

  {
    environments: {
      default: @full_config
    }
  }
end

#process_consumer(hash) ⇒ Hash

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flipp_ruby_kafka/utils/platform_schema_validation.rb', line 34

def process_consumer(hash)
  klass = hash['handler'].constantize
  topic = hash['topic']
  schema = klass.config[:schema]
  return if schema.blank?

  namespace = klass.config[:namespace] || ''
  path = namespace.tr('.', '/')
  {
    'file' => File.join(@base_path, path, "#{schema}.avsc"),
    'subject' => "#{topic}-value"
  }
end

#process_producer(klass) ⇒ Hash

Parameters:

  • klass (Class < FlippRubyKafka::Producer])

    lass [Class < FlippRubyKafka::Producer]

Returns:

  • (Hash)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flipp_ruby_kafka/utils/platform_schema_validation.rb', line 19

def process_producer(klass)
  schema = klass.config[:schema]
  topic = klass.config[:topic]
  return if topic.blank?

  namespace = klass.config[:namespace] || FlippRubyKafka.config.producers.schema_namespace
  path = namespace.tr('.', '/')
  {
    'file' => File.join(@base_path, path, "#{schema}.avsc"),
    'subject' => "#{topic}-value"
  }
end

#runObject

:nodoc:



71
72
73
74
# File 'lib/flipp_ruby_kafka/utils/platform_schema_validation.rb', line 71

def run
  full_json = generate_config
  File.write('schema-validation.json', JSON.pretty_generate(full_json))
end