Skip to content

Instantly share code, notes, and snippets.

@byronmejia
Created March 29, 2016 13:24
Show Gist options
  • Save byronmejia/0efd8a095cb6ee21db68 to your computer and use it in GitHub Desktop.
Save byronmejia/0efd8a095cb6ee21db68 to your computer and use it in GitHub Desktop.
What if.. dreams...
require 'celluloid/autostart'
require 'twitter'
class TweetActor
include Celluloid
include Celluloid::Notifications
attr_reader :twitter_client
def initialize
now = Time.now.to_f
sleep now.ceil - now + 0.001
@twitter_client = Twitter::Streaming::Client.new do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.access_token = ENV['TWITTER_IBIS_ACCESS']
config.access_token_secret = ENV['TWITTER_IBIS_SECRET']
end
async.run
end
def run
loop do
@twitter_client.user do |object|
case object
when Twitter::Tweet
publish 'twitter', "It's a Tweet!"
when Twitter::DirectMessage
publish 'twitter', "It's a direct message!"
when Twitter::Streaming::StallWarning
publish 'twitter', 'Falling behind!'
end
end
end
end
end
class Publisher
include Celluloid
include Celluloid::Notifications
def initialize
now = Time.now.to_f
sleep now.ceil - now + 0.001
async.run
end
def run
every(2) do
# What if, we tell our subscribers the time, ever 2 seconds...
# This couldn't go wrong, at all.
publish 'Publisher class topics', Time.now
end
end
end
class Subscriber
include Celluloid
include Celluloid::Notifications
include Celluloid::Internals::Logger
def initialize
info 'Subscribing to topics.'
subscribe 'Publisher class topics', :new_message
subscribe 'twitter', :new_message
end
def new_message(topic, data)
info "#{topic}: #{data}"
end
end
class MyGroup < Celluloid::SupervisionGroup
supervise Publisher, as: :publish_actor
end
#sub2 = TweetActor.new
sub = Subscriber.new
pub = Publisher.new
pub2 = TweetActor.new
puts 'Main thread is finished. I can sleep now forever.'
sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment