Sha256: 3992e173b109c9650cb9374e9ee258ed320fb73569ef3e3ed2146fc4d71f6993

Contents?: true

Size: 1.59 KB

Versions: 13

Compression:

Stored size: 1.59 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)
      validate_body(body)
      publisher.publish(destinations: destinations, message: { subject: subject, body: body })
    end

    # @param [Hash] body
    # @param [Time] at
    def publish_later(body, at:)
      validate_body(body)
      DispatchRider::ScheduledJob.create! scheduled_at: at,
                                          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

    def validate_body(body)
      raise ArgumentError, 'body should be a hash' unless body.is_a?(Hash)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dispatch-rider-2.1.0 lib/dispatch-rider/publisher/base.rb
dispatch-rider-2.0.0 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.9.0 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.8.6 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.8.5 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.8.4 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.8.3 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.8.2 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.8.1 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.8.0 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.7.2 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.7.1 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.7.0 lib/dispatch-rider/publisher/base.rb