Skip to content

Instantly share code, notes, and snippets.

@evant
Created March 31, 2014 20:45
Show Gist options
  • Save evant/9901831 to your computer and use it in GitHub Desktop.
Save evant/9901831 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'optparse'
require 'net/http'
require 'json'
OS = case RbConfig::CONFIG['host_os']
when /linux/
:linux
when /darwin/
:osx
end
url = URI("http://sapling.willowtreeapps.com/bathroom/")
notify = false
wait = false
OptionParser.new do |opts|
opts.banner = "Usage: bathroom [--notify]"
opts.on("-n", "--notify", "Show info in a notification instead of the terminal") do
begin
case OS
when :linux
require 'libnotify'
when :osx
require ''
end
notify = true
rescue LoadError
case OS
when :linux
warn '"gem install libnotify" for notifications'
when :osx
warn '"gem install terminal-notifier" for notifications'
end
exit 1
end
end
opts.on("-w", "--wait", "Wait for a bathroom to be open before showing") do
wait = true
end
end.parse!
def report(notify, body)
if notify
case OS
when :linux
Libnotify.show(summary: "Bathroom Tree", body: body, urgency: :critical)
when :osx
TerminalNotifier.notify(body, title: "Bathroom Tree")
end
else
puts body
end
end
if wait
loop do
bathrooms = JSON.parse(Net::HTTP.get(url))
room = bathrooms.find { |room| room['available'] }
if room
report(notify, "bathroom #{room['room']} is available")
exit 0
end
sleep 5
end
else
bathrooms = JSON.parse(Net::HTTP.get(url))
report(notify, bathrooms.map do |room|
"room #{room['room']} is #{room['available'] ? 'available' : 'not available'}"
end.join("\n"))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment