-
-
Save adamburmister/921742 to your computer and use it in GitHub Desktop.
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
# TODO: include gems in package | |
require 'rubygems' | |
require 'growl' | |
require 'net/http' | |
require 'net/https' | |
require 'yajl/json_gem' | |
# TODO: Read from ~/.backchatio_notifier file | |
API_KEY="YOUR_API_KEY_HERE" | |
STREAM_SLUG="controlbox" | |
ICON_PATH="$1/Contents/Resources/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_SLUG}", | |
'content-type' => "application/json", "accept" => "application/json", "authorization" => "Backchat #{API_KEY}") | |
http.request(req) do |res| | |
res.read_body do |chunk| | |
puts 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 | |
start_listening | |
sleep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment