Last active
November 13, 2015 10:38
-
-
Save mikekelly/7ee59123ba691a224dcd to your computer and use it in GitHub Desktop.
A better consumer interface for hutch consumers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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