Last active
April 6, 2017 19:24
-
-
Save joeybeninghove/0c16fa33ebc2e0f32499136be5313a1b to your computer and use it in GitHub Desktop.
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
# setting up and running a slurp | |
slurper = Slurpeee::Slurper.new | |
slurper.on(:progress) { |data| do_something_with(data) } | |
slurper.on(:some_other_known_event) { |data| do_something_with(data) } | |
slurp = slurper.slurp(url_to_slurp) | |
# slurper.rb | |
module Slurpeee | |
class Slurper | |
def initialize | |
@notifier = Notifier.instance | |
end | |
def slurp(url) | |
# misc code here | |
@notifier.broadcast(:progress, "The slurp is starting") | |
@notifier.broadcast(:progress, complex: "data structures", foo: "bar") | |
# other code to kick off slurp | |
end | |
end | |
end | |
# notifier.rb | |
require "singleton" | |
module Slurpeee | |
class Notifier | |
include Singleton | |
def initialize | |
@events = {} | |
end | |
def subscribe(event, &handler) | |
@events[event] ||= [] | |
@events[event] << handler | |
end | |
def broadcast(event, data) | |
@events[event].each do |handler| | |
handler.call(data) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment