Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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