Skip to content

Instantly share code, notes, and snippets.

@rypit
Last active August 29, 2015 14:23
Show Gist options
  • Save rypit/106b796a4d6fb8fde23c to your computer and use it in GitHub Desktop.
Save rypit/106b796a4d6fb8fde23c to your computer and use it in GitHub Desktop.
Message dispatcher
class YourSubscriber
subscribe(:queue, :or, :something) do |message|
Dispatcher::Base.dispatch_message(message)
end
end
class Dispatcher::Base
def can_process_message?
raise NotImplmenttedError
end
def process_message
raise NotImplmentedError
end
class << self
def dispatch_message(message)
valid_dispatchers.each{|d| d.process_message}
end
private
def valid_dispatchers
possible_dispatchers.select{|d| d.can_process_message? }
end
def possible_dispatchers
descendants.map{|klass| klass.new(message) }
end
end
end
class ShipmentEmailDispatcher < Dispatcher::Base
def can_process_message?
message[:routing_key] == "bulk.shipment.shipped"
end
def process_message
Delayed::Job.new(whatever: :idk)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment