Skip to content

Instantly share code, notes, and snippets.

@sosedoff
Created July 27, 2012 05:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sosedoff/3186275 to your computer and use it in GitHub Desktop.
Save sosedoff/3186275 to your computer and use it in GitHub Desktop.
Travis build notifications for Apple Notification Center
source :rubygems
gem 'terminal-notifier'
gem 'pusher-client', :git => 'git://github.com/pusher/pusher-ruby-client.git'
require 'terminal-notifier'
require 'pusher-client'
require 'json'
require 'pp'
class Travis
attr_accessor :watchlist
# Initialize a new connection to travis feed
#
# publisher_token - Valid pusher token
# watchlist - Array or repositories to watch
#
def initialize(pusher_token, watchlist=[])
@socket = PusherClient::Socket.new(pusher_token)
@watchlist = watchlist
end
def start
@socket.subscribe('common')
@socket['common'].bind('build:finished') do |data|
n = make_notification(parse_build(data))
if watchlist.empty?
add_notification(n)
else
add_notification(n) if watchlist.include?(n[:title])
end
end
@socket.connect
end
private
def parse_build(payload)
JSON.parse(payload)
end
def make_notification(data)
id = data['build']['id']
number = data['repository']['last_build_number']
result = data['build']['result'] == 0 ? 'PASSED' : 'FAILED'
title = data['repository']['slug']
message = "Build ##{number} #{result}"
url = "http://travis-ci.org/#!/#{title}/builds/#{id}"
{:title => title, :message => message, :url => url}
end
def add_notification(n)
TerminalNotifier.notify(n[:message], :title => n[:title], :open => n[:url])
end
end
travis = Travis.new('23ed642e81512118260e')
travis.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment