Class: DjiMqttConnect::Utils::MessageSchemaGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/dji_mqtt_connect/utils/message_schema_generator.rb

Overview

Generates a simple message schema from a mqtt message

Schema is simply the class name of any element that is not a Hash or Array

Instance Method Summary collapse

Constructor Details

#initialize(retain_attributes: []) ⇒ MessageSchemaGenerator

Returns a new instance of MessageSchemaGenerator.



11
12
13
# File 'lib/dji_mqtt_connect/utils/message_schema_generator.rb', line 11

def initialize(retain_attributes: [])
  @retain_attributes = Array.wrap(retain_attributes)
end

Instance Method Details

#generate(message) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dji_mqtt_connect/utils/message_schema_generator.rb', line 15

def generate(message)
  case message
  when Hash
    retain_values = retain_attributes.each_with_object({}) do |attribute, hash|
      next unless message.key?(attribute)

      attribute_value = message[attribute]
      next if attribute_value.is_a?(Hash) || attribute_value.is_a?(Array)

      hash[attribute] = "#{attribute_value.class.name}:#{attribute_value}"
    end
    message.transform_values { |v| generate(v) }.merge(retain_values)
  when Array
    message.map { |v| generate(v) }
  else
    message.class.name
  end
end