Sha256: 1dcb144b0f86e8ac987f91375cf2a7bba976a7b4a7d62f681bf6cd9d42b09421

Contents?: true

Size: 1.24 KB

Versions: 11

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require "dry-transformer"
require "json"

module DjiMqttConnect
  module Sys::Product
    class StatusMarshal < MessageMarshal
      include Utils::MessageParsing

      # Rename pesky `method` argument to `_method` and makes a copy of the raw data
      class AttributeTransformer < Dry::Transformer::Pipe
        import Dry::Transformer::HashTransformations

        define! do
          copy_keys "data" => "_data"
          rename_keys "method" => "_method"
        end
      end

      # Attempts to look a the method attribute, and builds a specific Message class for the message
      def load(raw_message)
        # Parse the message from JSON
        parsed_message = parse_json(raw_message)

        # Transform the message
        transformed_message = attribute_transformer.call(parsed_message)
        message_method = transformed_message["_method"]

        # Build an instance of the class, or a generic message from the current class
        message_class = message_class_from_message_method(message_method, StatusMessage)
        build_message(message_class, transformed_message)
      end

      private

      def attribute_transformer
        @attribute_transformer ||= AttributeTransformer.new
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.25.1 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
dji_mqtt_connect-0.1.25 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
dji_mqtt_connect-0.1.24.1 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
dji_mqtt_connect-0.1.24 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
dji_mqtt_connect-0.1.23.3 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
dji_mqtt_connect-0.1.23.2 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
dji_mqtt_connect-0.1.23.1 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
dji_mqtt_connect-0.1.23 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
dji_mqtt_connect-0.1.22.2 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
dji_mqtt_connect-0.1.22.1 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb
dji_mqtt_connect-0.1.22 lib/dji_mqtt_connect/marshals/sys/product/status_marshal.rb