class DispatchRider::Publisher::Base

Main template for a dispatch rider publisher.

Public Class Methods

default_publisher() click to toggle source

@return [DispatchRider::Publisher]

# File lib/dispatch-rider/publisher/base.rb, line 18
def default_publisher
  @@default_publisher ||= DispatchRider::Publisher.new
end
destinations(destinations) click to toggle source

@param [Array<Symbol>, Symbol] destinations

# File lib/dispatch-rider/publisher/base.rb, line 13
def destinations(destinations)
  @destinations = Array(destinations)
end
new(publisher = nil) click to toggle source
# File lib/dispatch-rider/publisher/base.rb, line 27
def initialize(publisher = nil)
  @publisher = publisher
end
publish(*args, &block) click to toggle source
# File lib/dispatch-rider/publisher/base.rb, line 22
def publish(*args, &block)
  raise NotImplementedError, "subclass of DispatchRider::Publisher::Base must implement .publish"
end
subject(subject) click to toggle source

@param [Symbol] subject

# File lib/dispatch-rider/publisher/base.rb, line 8
def subject(subject)
  @subject = subject
end

Public Instance Methods

publish(body) click to toggle source

@param [Hash] body

# File lib/dispatch-rider/publisher/base.rb, line 32
def publish(body)
  validate_body(body)
  publisher.publish(destinations: destinations, message: { subject: subject, body: body })
end
publish_later(body, at:) click to toggle source

@param [Hash] body @param [Time] at

# File lib/dispatch-rider/publisher/base.rb, line 39
def publish_later(body, at:)
  validate_body(body)
  DispatchRider::ScheduledJob.create! scheduled_at: at,
                                      destinations: destinations,
                                      message: { subject: subject, body: body }
end

Private Instance Methods

destinations() click to toggle source
# File lib/dispatch-rider/publisher/base.rb, line 52
def destinations
  self.class.instance_variable_get(:@destinations)
end
publisher() click to toggle source
# File lib/dispatch-rider/publisher/base.rb, line 48
def publisher
  @publisher || self.class.default_publisher
end
subject() click to toggle source
# File lib/dispatch-rider/publisher/base.rb, line 56
def subject
  self.class.instance_variable_get(:@subject)
end
validate_body(body) click to toggle source
# File lib/dispatch-rider/publisher/base.rb, line 60
def validate_body(body)
  raise ArgumentError, 'body should be a hash' unless body.is_a?(Hash)
end