Sha256: 9b50a1de3ecc60ca5d2c34450ed4d1cab132292a1e93353b744ab319d41b595b

Contents?: true

Size: 1.54 KB

Versions: 35

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require_relative '../client'

module Rubyists
  module Dapr
    module Client
      # Handles publishing messages to Dapr pub/sub topics
      class Publisher
        # Include the client module
        include Client

        # The name of the pubsub component, the client, and the serialization to use
        attr_reader :pubsub_name, :client, :serialization

        # The proto class for the publish event request
        Proto = ::Dapr::Proto::Runtime::V1::PublishEventRequest

        # Initialize the publisher
        # @param name [String] The name of the pubsub component in Dapr
        # @param serialization [Symbol] The serialization format to use. Defaults to :to_json
        #                               this can be :to_json, :to_dapr, or any object that responds to :wrap.
        #                               If it responds to :wrap, it will be called with the message to be sent.
        def initialize(name, serialization: :to_json)
          @serialization = serialization
          @pubsub_name = name
        end

        def publish(topic, message)
          singleton.publish_event(Proto.new(pubsub_name:, topic:, data: wrap(message)))
        end

        private

        def wrap(message)
          case serialization
          when :to_dapr, :to_json
            message.send(serialization)
          when ->(s) { s.respond_to?(:wrap) }
            serialization.wrap(message)
          else
            raise "Unknown serialization format: #{serialization}"
          end
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
dapr-0.5.0 lib/dapr/client/publisher.rb
dapr-0.4.3 lib/dapr/client/publisher.rb
dapr-0.4.2 lib/dapr/client/publisher.rb
dapr-0.4.1 lib/dapr/client/publisher.rb
dapr-0.4.0 lib/dapr/client/publisher.rb
dapr-0.3.3 lib/dapr/client/publisher.rb
dapr-0.3.2 lib/dapr/client/publisher.rb
dapr-0.3.1 lib/dapr/client/publisher.rb
dapr-0.3.0 lib/dapr/client/publisher.rb
dapr-0.2.11 lib/dapr/client/publisher.rb
dapr-0.2.9 lib/dapr/client/publisher.rb
dapr-0.2.8 lib/dapr/client/publisher.rb
dapr-0.2.7 lib/dapr/client/publisher.rb
dapr-0.2.6 lib/dapr/client/publisher.rb
dapr-0.2.4 lib/dapr/client/publisher.rb
dapr-0.2.2 lib/dapr/client/publisher.rb
dapr-0.1.25 lib/dapr/client/publisher.rb
dapr-0.1.24 lib/dapr/client/publisher.rb
dapr-0.1.23 lib/dapr/client/publisher.rb
dapr-0.1.22 lib/dapr/client/publisher.rb