Skip to content

Instantly share code, notes, and snippets.

@mikekelly
Last active Nov 13, 2015
Embed
What would you like to do?
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