Sha256: d86c4951c8ab667e5da01b3d8e77ff9e057735f8f6ac9f13b6711bbbe1b018b7

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

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

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

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

        define! do
          copy_keys "data" => "_data"
        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 = state_transformer.call(parsed_message)

        # Build an instance of the class, or a generic message from the current class
        build_message(StateMessage, transformed_message)
      end

      private

      def state_transformer
        @state_transformer ||= StateTransformer.new
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.25.4 lib/dji_mqtt_connect/marshals/thing/product/state_marshal.rb
dji_mqtt_connect-0.1.25.3 lib/dji_mqtt_connect/marshals/thing/product/state_marshal.rb
dji_mqtt_connect-0.1.25.2 lib/dji_mqtt_connect/marshals/thing/product/state_marshal.rb
dji_mqtt_connect-0.1.25.1 lib/dji_mqtt_connect/marshals/thing/product/state_marshal.rb
dji_mqtt_connect-0.1.25 lib/dji_mqtt_connect/marshals/thing/product/state_marshal.rb