Created
April 15, 2011 13:32
-
-
Save casualjim/921689 to your computer and use it in GitHub Desktop.
A growl script for backchat notifications.. will be turned into a gem
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
require 'rubygems' | |
require 'growl' | |
require 'net/http' | |
require 'net/https' | |
require 'yajl/json_gem' | |
API_KEY="YOUR_API_KEY" | |
STREAM_NAME="controlbox" | |
ICON_PATH="/Users/ivan/Dropbox/Mojolly/design/backchat/apple-touch-icon.png" | |
def start_listening | |
Thread.new do | |
begin | |
http_client = Net::HTTP.new("api.backchat.io", 443) | |
http_client.use_ssl = true | |
http_client.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
http_client.start do |http| | |
req = Net::HTTP::Get.new( | |
"/1/stream/#{STREAM_NAME}", | |
'content-type' => "application/json", "accept" => "application/json", "authorization" => "Backchat #{API_KEY}") | |
http.request(req) do |res| | |
res.read_body do |chunk| | |
unless chunk.chomp.strip.empty? | |
msg = JSON.parse(chunk)["messages"].first["content"] | |
Growl.notify msg, :title => "BackChat.IO", :icon => ICON_PATH | |
end | |
end | |
end | |
end | |
rescue Exception => ex | |
puts "[#{Time.now} | #{ex.class}] #{ex.message}\n#{ex.backtrace.join("\n")}" | |
raise ex | |
end | |
end | |
end | |
puts "Listening to stream #{STREAM_NAME} on BackChat.IO" | |
start_listening | |
sleep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment