Sha256: d1f1803fd4cb42450bb3fd897d379cb7bf5cdb3bc0602d7bc340ee4fa9501ecb

Contents?: true

Size: 1.46 KB

Versions: 28

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

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

module DjiMqttConnect
  module Thing::Product
    class RequestsMarshal < 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 = JSON.parse(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, RequestsMessage)
        message_class.new transformed_message
      rescue JSON::ParserError => e
        raise ParseError.new(e, "Unable to parse message as JSON")
      rescue Dry::Struct::Error => e
        raise ParseError.new(e, "Unexpected #{message_class} payload")
      end

      private

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

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
dji_mqtt_connect-0.1.21 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.20.1 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.20 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.19.2 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.19.1 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.19 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.18 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.17.1 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.17 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.16.2 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.16.1 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.16 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.15.2 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.15.1 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.15 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.14.1 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.14 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.13.1 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.13 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb
dji_mqtt_connect-0.1.12.1 lib/dji_mqtt_connect/marshals/thing/product/requests_marshal.rb