Sha256: d0eb5ef8dc7707144541e9a3f394f0775cc56162fc3008f37e5325e1e29965fd
Contents?: true
Size: 1.98 KB
Versions: 6
Compression:
Stored size: 1.98 KB
Contents
# frozen_string_literal: true module PubSubModelSync class Payload class MissingInfo < StandardError; end attr_reader :data, :attributes, :headers # @param data (Hash: { any value }): # @param attributes (Hash: { klass: string, action: :sym }): def initialize(data, attributes, headers = {}) @data = data @attributes = attributes @headers = headers build_headers validate! end # @return Hash: payload data def to_h { data: data, attributes: attributes, headers: headers } end def klass attributes[:klass] end def action attributes[:action] end # Process payload data # (If error will raise exception and wont call on_error_processing callback) def process! process do |publisher| publisher.raise_error = true end end # Process payload data # (If error will call on_error_processing callback) def process publisher = PubSubModelSync::MessageProcessor.new(self) yield(publisher) if block_given? publisher.process end # Publish payload to pubsub # (If error will raise exception and wont call on_error_publish callback) def publish! klass = PubSubModelSync::MessagePublisher klass.publish(self, raise_error: true) end # Publish payload to pubsub # (If error will call on_error_publish callback) def publish klass = PubSubModelSync::MessagePublisher klass.publish(self) end # convert payload data into Payload # @param data [Hash]: payload data (:data, :attributes, :headers) def self.from_payload_data(data) data = data.deep_symbolize_keys new(data[:data], data[:attributes], data[:headers]) end private def build_headers headers[:uuid] ||= SecureRandom.uuid headers[:app_key] ||= PubSubModelSync::Config.subscription_key end def validate! raise MissingInfo if !attributes[:klass] || !attributes[:action] end end end
Version data entries
6 entries across 6 versions & 1 rubygems