# frozen_string_literal: true module DjiMqttConnect module Utils module MessageParsing private def message_class_from_method_value(parsed_message, generic_class) message_method = parsed_message["method"] return generic_class if message_method.blank? # update_topo => UpdateTopo classified_method = message_method.split("_").collect! { |w| w.capitalize }.join # UpdateTopo => DjiMqttConnect::Sys::Product::UpdateTopoStatusMessage module_prefix, joiner, class_suffix = generic_class.name.rpartition("::") message_class_name = [module_prefix, joiner, classified_method, class_suffix].join # Constantize an instance of the message-specific class, or fallback to a generic message ActiveSupport::Inflector.safe_constantize(message_class_name) || generic_class end end end end