Class: DjiMqttConnect::Utils::MqttxFixtureDataGenerator

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

Overview

Generates anonymised fixture data from MQTTX JSON Export Dumps

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mqttx_data, message_sanitizer:) ⇒ MqttxFixtureDataGenerator

Returns a new instance of MqttxFixtureDataGenerator.



15
16
17
18
# File 'lib/dji_mqtt_connect/utils/mqttx_fixture_data_generator.rb', line 15

def initialize(mqttx_data, message_sanitizer:)
  @mqttx_data = mqttx_data
  @message_sanitizer = message_sanitizer
end

Class Method Details

.from_file(file_path, **sanitizer_args) ⇒ Object



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

def self.from_file(file_path, **sanitizer_args)
  raw_data = File.read(file_path)
  new JSON.parse(raw_data), message_sanitizer: MessageSanitizer.new(**sanitizer_args)
end

Instance Method Details

#generateObject

Generates fixture data using ALL found messages in the MQTTX export

# Output is: <topic 1>: <message 1 as json> <topic 1>: <message 2 as json>



25
26
27
28
29
# File 'lib/dji_mqtt_connect/utils/mqttx_fixture_data_generator.rb', line 25

def generate
  extracted_messages.each_with_object(StringIO.new) do |(topic, message), builder|
    builder.puts [topic, message.to_json].join(": ")
  end.string
end

#generate_unique(**schema_generator_args) ⇒ Object

Generates fixture data using unique found messages in the MQTTX export

A message is considered unique if the topic and schema does not match any other messages

# Output is: <topic 1>: <message 1 as json>



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dji_mqtt_connect/utils/mqttx_fixture_data_generator.rb', line 37

def generate_unique(**schema_generator_args)
  message_schema_generator = MessageSchemaGenerator.new(**schema_generator_args)

  unique_messages = extracted_messages.each_with_object({}) do |(topic, message), found|
    message_schema = message_schema_generator.generate(message)
    found[[topic, message_schema]] ||= [topic, message]
  end.values

  unique_messages.each_with_object(StringIO.new) do |(topic, message), builder|
    builder.puts [topic, message.to_json].join(": ")
  end.string
end