Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mikekelly
Last active November 13, 2015 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikekelly/7ee59123ba691a224dcd to your computer and use it in GitHub Desktop.
Save mikekelly/7ee59123ba691a224dcd to your computer and use it in GitHub Desktop.
A better consumer interface for hutch consumers
require 'hutch'
class BaseConsumer
# Ensure subclass is passed to Hutch::Consumer.included
def self.inherited(subclass)
subclass.include Hutch::Consumer
end
# Option to initialize a consumer with a message
def initialize(message: nil)
@message = message
end
# Adapter for hutch interface so we can implement ExampleConsumer#call to take no args
def process(message)
@message = message
call
end
private
def payload
message.body
end
attr_reader :message
end
require 'hutch'
# Usage:
# AppEvent.emit(name: 'example', payload: { some: 'json object' })
class AppEvent
def self.emit(*args)
new(*args).call
end
def initialize(name:, payload: Hash.new)
@name, @payload = name, payload
end
def call
Hutch.connect
Hutch.publish(name, payload)
end
private
attr_reader :name, :payload
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment