Class: DjiMqttConnect::Utils::MessageSchemaGenerator
- Inherits:
-
Object
- Object
- DjiMqttConnect::Utils::MessageSchemaGenerator
- 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
- #generate(message) ⇒ Object
-
#initialize(retain_attributes: []) ⇒ MessageSchemaGenerator
constructor
A new instance of MessageSchemaGenerator.
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() case when Hash retain_values = retain_attributes.each_with_object({}) do |attribute, hash| next unless .key?(attribute) attribute_value = [attribute] next if attribute_value.is_a?(Hash) || attribute_value.is_a?(Array) hash[attribute] = "#{attribute_value.class.name}:#{attribute_value}" end .transform_values { |v| generate(v) }.merge(retain_values) when Array .map { |v| generate(v) } else .class.name end end |