Created
September 24, 2012 17:52
-
-
Save lisa/3777279 to your computer and use it in GitHub Desktop.
hacklab_door_tweeter.rb
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'twitter' | |
require 'oauth' | |
require 'mq' | |
require 'rexml/document' | |
require 'syslog' | |
include REXML | |
Syslog.open('hacklab_door_tweeter') | |
# TODO: It would be nice if these Signals actually syslogged. | |
Signal.trap('INT') do | |
Syslog.warn "Caught SIGINT, Exiting..." | |
AMQP.stop { EM.stop } | |
end | |
Signal.trap('TERM') do | |
Syslog.warn "Caught SIGTERM, Exiting...." | |
AMQP.stop { EM.stop } | |
end | |
oauth_token = 'x' | |
oauth_token_secret = 'x' | |
consumer_key = 'x' | |
consumer_secret = 'x' | |
username = 'x' | |
password = 'x' | |
Twitter.configure do |config| | |
config.oauth_token = oauth_token | |
config.oauth_token_secret = oauth_token_secret | |
config.consumer_key = consumer_key | |
config.consumer_secret = consumer_secret | |
end | |
@@client = Twitter::Client.new | |
Syslog.info "Entering main loop..." | |
AMQP.start(:host => 'moa.hacklab.to', :user => username, :pass => password) do | |
def tweet(name) | |
begin | |
@@client.update "#{name} has entered." | |
Syslog.info "tweeted about #{name} entering" | |
rescue Exception => e | |
Syslog.err "tweet(#{name}) caught exception => #{e}" | |
end | |
end | |
def get_entered_name(doc) | |
XPath.first(doc.root,"nickName").text rescue nil | |
end | |
def allowed_to_enter?(doc) | |
XPath.first(doc.root,"allowed").text.eql?("true") rescue false | |
end | |
amq = MQ.queue('door.entry.twitter', :durable => false, :auto_delete => true) | |
amq.bind(MQ.fanout('door.entry', :durable => false, :auto_delete => false)).subscribe do |entry| | |
Syslog.info "Door Entry" | |
Syslog.debug "XML = #{entry}" | |
doc = Document.new(entry) rescue Document.new("") | |
name = get_entered_name(doc) rescue false | |
Syslog.warn "Couldn't find a name!" unless name | |
tweet name if name && allowed_to_enter?(doc) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment