Sha256: 7102e32121ccae32dbed7b3ec20a739d05c1a4316beaf9259703e20325044c4e

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

require 'securerandom'

module DispatchRider
  # Main template for a dispatch rider publisher.
  class Publisher::Base
    class << self
      # @param [Symbol] subject
      def subject(subject)
        @subject = subject
      end

      # @param [Array<Symbol>, Symbol] destinations
      def destinations(destinations)
        @destinations = Array(destinations)
      end

      # @return [DispatchRider::Publisher]
      def default_publisher
        @@default_publisher ||= DispatchRider::Publisher.new
      end

      def publish(*args, &block)
        raise NotImplementedError, "subclass of DispatchRider::Publisher::Base must implement .publish"
      end
    end

    def initialize(publisher = nil)
      @publisher = publisher
    end

    # @param [Hash] body
    def publish(body)
      raise ArgumentError, 'body should be a hash' unless body.kind_of?(Hash)
      publisher.publish(destinations: destinations, message: { subject: subject, body: body })
    end

    private

    def publisher
      @publisher || self.class.default_publisher
    end

    def destinations
      self.class.instance_variable_get(:@destinations)
    end

    def subject
      self.class.instance_variable_get(:@subject)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dispatch-rider-1.6.2 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.6.1 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.6.0 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.5.3 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.5.2 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.5.1 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.5.0 lib/dispatch-rider/publisher/base.rb