Created
March 22, 2010 10:08
-
-
Save auxesis/339939 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
campfire-notifier is a NotifierOSD compatible Campfire notifier. | |
Usage: campfire-notifier <token> <domain> <room> [room] | |
You can get your token from your 37signals account. Specify as many rooms as you want. | |
Assumes you have HTTParty and YAJL-ruby installed. | |
Nice Campfire icon at http://bit.ly/9r3BRd, which you can customise on line 14. |
This file contains hidden or 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 | |
fork do | |
while true do | |
system("campfire-notifier $api_key $domain $room1 $room2") | |
end | |
end | |
This file contains hidden or 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 "uri" | |
require "yajl/http_stream" | |
require "httparty" | |
class CampfireNotifier | |
def initialize(opts={}) | |
@token = opts[:token] | |
@rooms = opts[:rooms] | |
@domain = opts[:domain] | |
@icon = "/path/to/campfire-logo.png" | |
@threads = [] | |
@users = {} | |
eval <<-DODGY | |
class Connection | |
include HTTParty | |
basic_auth('#{@token}', 'x') | |
end | |
DODGY | |
end | |
def icon | |
File.exists?(@icon) ? "-i #{@icon}" : nil | |
end | |
def rooms_uri | |
"http://#{@domain}.campfirenow.com/rooms.json" | |
end | |
def room_uri(room_id) | |
"http://#{@domain}.campfirenow.com/room/#{room_id}.json" | |
end | |
def listen_uri(room_id) | |
URI.parse("http://#{@token}:[email protected]/room/#{room_id}/live.json") | |
end | |
def process_notifications(uri) | |
Yajl::HttpStream.get(uri) do |message| | |
notify(message) | |
end | |
end | |
def find_room(opts={}) | |
id = opts[:room] | |
@rooms[id] ||= rooms.find {|r| r["id"] == id } | |
end | |
def find_user(opts={}) | |
room_id = opts[:room] | |
user_id = opts[:user] | |
@users[user_id] ||= users(room_id).find {|u| u["id"] == user_id } | |
end | |
def build_message(attrs) | |
room = find_room(:room => attrs["room_id"]) | |
user = find_user(:user => attrs["user_id"], :room => attrs["room_id"]) | |
message = [] | |
message << user["name"] | |
message << "in #{room["name"]}:" | |
message << attrs["body"] | |
message.join(' ') | |
end | |
def notify(attrs) | |
message = build_message(attrs) | |
puts message | |
command = "notify-send #{icon} '#{message}'" | |
system(command) | |
end | |
def rooms | |
Connection.get(rooms_uri)['rooms'] | |
end | |
def users(room_id) | |
Connection.get(room_uri(room_id))['room']['users'] | |
end | |
def room_ids | |
@rooms.map do |name| | |
rooms.find {|r| r["name"] == name } | |
end | |
end | |
def run | |
room_ids.each do |room| | |
@threads << Thread.new do | |
uri = listen_uri(room["id"]) | |
process_notifications(uri) | |
end | |
end | |
@threads.each {|thread| thread.join} | |
end | |
end | |
if $0 == __FILE__ | |
unless ARGV.size >= 3 | |
puts "campfire-notifier <token> <domain> <room> [room]" | |
exit 1 | |
end | |
token = ARGV[0] | |
domain = ARGV[1] | |
rooms = ARGV[2..-1] | |
notifier = CampfireNotifier.new(:token => token, :rooms => rooms, :domain => domain) | |
notifier.run | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment