Created
August 5, 2012 12:33
Revisions
-
fnando created this gist
Aug 5, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,103 @@ require "socket" require "digest/md5" require "timeout" class GNTP attr_accessor :application_name attr_accessor :host attr_accessor :port attr_accessor :password def initialize(options = {}) if options.kind_of?(String) options = {:name => options} end @application_name = options.fetch(:name, "GNTP/Ruby") @host = options.fetch(:host, "127.0.0.1") @port = options.fetch(:port, 23053) @password = options.fetch(:password, nil) end def line_break(number = 1) "\r\n" * number end def write(*contents) socket = TCPSocket.open(host, port) message = [*contents, line_break(2)].join(line_break) socket.write(message) "".tap do |response| while chunk = socket.gets break if chunk == line_break response << chunk end socket.close end end def notify(options) name = options.fetch(:name, "notification") register(name) icon = fetch_icon(options[:icon]) result = write "GNTP/1.0 NOTIFY NONE", "Application-Name: #{application_name}", "Notification-Name: #{name}", "Notification-Title: #{options[:title]}", "Notification-Text: #{options[:message]}", "Notification-Icon: x-growl-resource://#{icon[:identifier]}", "Notification-Sticky: #{bool options[:sticky]}", nil, "Identifier: #{icon[:identifier]}", "Length: #{icon[:size]}", nil, icon[:contents] end def fetch_icon(path) contents = File.open(path, "rb") {|f| f.read } { :identifier => Digest::MD5.hexdigest(contents), :contents => contents, :size => contents.bytesize } end def bool(boolean) {true => "Yes", false => "No"}[boolean] end def register(name) result = write "GNTP/1.0 REGISTER NONE", "Application-Name: #{application_name}", "Notifications-count: 1", nil, "Notification-Name: #{name}", "Notification-Enabled: True" end end module Notifier module GNTP extend self def supported? ENV["SSH_CONNECTION"] end def notify(options) growl = ::GNTP.new({ :name => "test_notifier", :host => ENV["SSH_CONNECTION"][/^([^ ]+)/, 1] }) growl.notify :name => "status", :title => options[:title], :message => options[:message], :icon => options[:image] end end end