Created
July 27, 2012 05:12
-
-
Save sosedoff/3186275 to your computer and use it in GitHub Desktop.
Travis build notifications for Apple Notification Center
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
source :rubygems | |
gem 'terminal-notifier' | |
gem 'pusher-client', :git => 'git://github.com/pusher/pusher-ruby-client.git' |
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
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