Sha256: c001244345cb6dfa0342f0a631dbfab87f852c48ec079f14cfbe589f10e48c2d
Contents?: true
Size: 1.47 KB
Versions: 19
Compression:
Stored size: 1.47 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 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, ServicesReplyMessage) 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
19 entries across 19 versions & 1 rubygems