Sha256: 1b7a8f570ab89f3e49f06afb2a8a88a49fde059d44bcb2c93c8f5ce2e7c19caf
Contents?: true
Size: 1.27 KB
Versions: 1
Compression:
Stored size: 1.27 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 AttributeTransformer < 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 = JSON.parse(raw_message) # Transform the message transformed_message = attribute_transformer.call(parsed_message) # Build an instance of the class, or a generic message from the current class StateMessage.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 #{StateMessage} payload") end private def attribute_transformer @attribute_transformer ||= AttributeTransformer.new end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dji_mqtt_connect-0.1.21 | lib/dji_mqtt_connect/marshals/thing/product/state_marshal.rb |