Skip to content

Instantly share code, notes, and snippets.

@eric
Created August 9, 2011 21:47
Show Gist options
  • Select an option

  • Save eric/1135284 to your computer and use it in GitHub Desktop.

Select an option

Save eric/1135284 to your computer and use it in GitHub Desktop.

Notifo notifier

Send yourself a notification with:

$ notifo --user myself --token c01e66f7c7ebb828cd8f59282a3c6c18 'The job is done'

Don't worry about remembering your token:

$ echo "NOTIFO_USER=myself" >> ~/.notifo_credentials
$ echo "NOTIFO_TOKEN=c01e66f7c7ebb828cd8f59282a3c6c18" >> ~/.notifo_credentials
$ notifo 'The job is done'

Notify you after a long-running process has finished:

$ notifo --wait-for-pid 3729 -t 'Hurray' 'The big job is done'
#!/usr/bin/env ruby
require 'net/http'
require 'ostruct'
require 'optparse'
class Notifo
def initialize(user, token)
@user, @token = user, token
end
def post(endpoint, params)
url = URI.parse("https://api.notifo.com/v1/#{endpoint}")
http = Net::HTTP.new(url.host, url.port)
if url.scheme == 'https'
require 'net/https'
http.use_ssl = true
store = OpenSSL::X509::Store.new
store.set_default_paths
http.cert_store = store
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end
req = Net::HTTP::Post.new(url.path)
req.basic_auth @user, @token
req.set_form_data(params)
http.start do |http|
case res = http.request(req)
when Net::HTTPSuccess, Net::HTTPOK
else
res.error!
end
end
end
end
if File.exists?(notifo_credentials = File.join(ENV['HOME'], '.notifo_credentials'))
IO.read(notifo_credentials).split(/\n+/).each do |line|
ENV[$1] = $2 if line =~ /^([^=]+)=(.+)$/
end
end
options = OpenStruct.new
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [options] <message>"
opts.separator ""
opts.separator "Specific options:"
# Mandatory argument.
opts.on("--user USER", "Notifo username") do |user|
options.user = user
end
opts.on("--token TOKEN", "Notifo token") do |token|
options.token = token
end
opts.on("-t", "--title TITLE", "Title of message") do |title|
options.title = title
end
opts.on("--label LABEL", "Label of message") do |label|
options.label = label
end
opts.on("--wait-for-pid PID", "Wait to send until PID is no longer running") do |pid|
options.wait_for_pid = pid
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
opts.parse!(ARGV)
options.user ||= ENV['NOTIFO_USER']
options.token ||= ENV['NOTIFO_TOKEN']
options.message ||= ENV['NOTIFO_MESSAGE'] || ARGV.shift
options.title ||= ENV['NOTIFO_TITLE'] || ENV['HOST']
options.label ||= ENV['NOTIFO_LABEL'] || File.basename($0)
unless options.message
puts opts
exit 1
end
notifo = Notifo.new(options.user, options.token)
if options.wait_for_pid
while `ps -p #{options.wait_for_pid}`.split(/\n/).length > 1
sleep 5
end
end
# We don't really care if this fails
notifo.post('subscribe_user', :username => options.user) rescue nil
notifo.post 'send_notification', :to => options.user,
:msg => options.message, :title => options.title,
:label => options.label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment