Sha256: 42e0215ee881de9d8f03419cbf72a1310ea8d7e369a39142e21ce12451cc828d

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require "dry-struct"
require "dry-transformer"
require "json"

module DjiMqttConnect
  module Thing::Product
    class ServicesReplyMarshal < MessageMarshal
      include Utils::MessageParsing

      # Rename pesky `method` argument to `_method` and makes a copy of the raw data
      class ServicesReplyTransformer < 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 = services_reply_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, ServicesReplyMessage)
        build_message(message_class, transformed_message)
      end

      private

      def services_reply_transformer
        @services_reply_transformer ||= ServicesReplyTransformer.new
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.25.1 lib/dji_mqtt_connect/marshals/thing/product/services_reply_marshal.rb
dji_mqtt_connect-0.1.25 lib/dji_mqtt_connect/marshals/thing/product/services_reply_marshal.rb