Sha256: fa159399386b4efff27d1c4d4c055883f06a325f0c9a520bbd8ff104ae2dc45f

Contents?: true

Size: 1.31 KB

Versions: 7

Compression:

Stored size: 1.31 KB

Contents

require 'securerandom'

module DispatchRider
  class Publisher::Base

    class << self

      def subject(subject)
        @subject = subject
      end

      def destinations(destinations)
        @destinations = Array(destinations)
      end

      def default_publisher
        @@default_publisher ||= DispatchRider::Publisher.new
      end

      def publish(*args, &block)
        raise NotImplementedError
      end

      private

      def generate_new_message_id
        if DispatchRider.config.debug
          DispatchRider::Debug::PUBLISHER_MESSAGE_GUID
        else
          SecureRandom.uuid
        end
      end

    end

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

    def publish(body)
      raise ArgumentError, 'body should be a hash' unless body.kind_of?(Hash)
      body = body.merge({
        'guid' => generate_new_message_id,
      })
      publisher.publish(destinations: destinations, message: { subject: subject, body: body })
    end

    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

    private

    def generate_new_message_id
      self.class.send(:generate_new_message_id)
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dispatch-rider-1.3.6 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.3.5 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.3.4 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.3.3 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.3.2 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.3.1 lib/dispatch-rider/publisher/base.rb
dispatch-rider-1.3.0 lib/dispatch-rider/publisher/base.rb