casualjim
4/15/2011 - 1:32 PM

A growl script for backchat notifications.. will be turned into a gem

A growl script for backchat notifications.. will be turned into a gem

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