Skip to content

Instantly share code, notes, and snippets.

@enukane
Created February 18, 2016 02:35
Show Gist options
  • Save enukane/98171ed702bf66ee26ea to your computer and use it in GitHub Desktop.
Save enukane/98171ed702bf66ee26ea to your computer and use it in GitHub Desktop.
cup of tea server
#!/usr/bin/env ruby
require "yaml"
require "optparse"
class CupOFTea
TEAPOT="#{ENV['HOME']}/.tea_pot"
TEAYML = <<LISTOFTEA
assam:
- bokel
- doomni
- mangalam
- halmari
- harmutty
- jalinga
- mokalbari
- seajuli
ceylon:
- bandulla
- dinbula
- kagalle
- kandy
- kurunegala
- nuwaraeliya
- matale
- matara
- ruhuna
- sabaragamuwa
- uva
darjeeling:
- alloobari
- ambiok
- ambootia
- arya
- avongrove
- badamtam
- balasun
- bannockburn
- barnesbeg
- castleton
- chamong
- chongtong
- dalrus
- dhajea
- Dilaram
- dooteriah
- edenvale
- giddapahar
- gielle
- ging
- glenbum
- goomtee
- gopaldhara
- gyabaree
- happyvalley
- hilton
- jogmaya
- jungpana
- kalejvalley
- kanchanview
- kumai
- lingia
- lizahill
- longview
- lopchu
- mahalderam
- makaibari
- margaretshope
- marybong
- millikthong
- mim
- missionhill
- mohanmajhua
- monteviot
- moondakotee
- mullootar
- nagri
- namring
- narbadamajhua
- northtukvar
- nurbong
- oaks
- okayti
- orangevalley
- pandam
- pashok
- phoobsering
- phuguri
- poobong
- potong
- princeton
- pussimbing
- puttabong
- ringtong
- risheehat
- rohini
- rungleerungliot
- rungmook
- samabeong
- seeyok
- selimhill
- selimbong
- sepoydhoorah
- singbulli
- singell
- singla
- singtom
- sivitar
- snowview
- soom
- soureni
- springside
- steinthal
- sungma
- takdah
- teestavalley
- thurbo
- tindharia
- tukdah
- tukvar
- tongsong
- tumsong
- turzum
- upperfagu
- vahtukvar
dooars:
- marionbarie
kangra:
- dharmasala
keemun:
nepali:
- mistvalley
- samalvalley
- antuvalley
nilgiri:
- glendale
sikkim:
- tantalizing
LISTOFTEA
def self.next_cup
tea_selection = YAML.load(TEAYML)
best_cup = tea_selection.flatten.flatten.sample
self.remember_cup(best_cup)
return best_cup
end
def self.another_cup
prev_cup = self.recall_cup
return prev_cup if prev_cup
return self.next_cup
end
def self.remember_cup tea
File.open(TEAPOT, "w") do |f|
f.write(tea)
end
return true
rescue
p "failed to write"
# do nothing for now
return false
end
def self.recall_cup
prev_cup = nil
if File.exists?(TEAPOT) and !File.directory?(TEAPOT)
File.open(TEAPOT) do |f|
prev_cup = f.read
end
end
return prev_cup
end
end
if __FILE__ == $0
opt = OptionParser.new
OPTS={:new => true}
opt.on("-a", "--another-cup", "have another cup?") {|v|
OPTS[:another] = true
}
opt.on("-n", "--new-one-please", "we serve you with new cup of tea") {|v|
OPTS[:new] = true
}
opt.on("-r", "--recall-cup", "want to know previous cup?") {|v|
OPTS[:recall] = true
}
(class<<self;self;end).module_eval do
define_method(:usage) do |msg|
puts opt.to_s
puts "error: #{msg}" if msg
exit 1
end
end
begin
rest = opt.parse(ARGV)
if rest.length != 0
usage nil
end
rescue
usage $!.to_s
end
if OPTS[:another]
print CupOFTea.another_cup
exit 0
end
if OPTS[:recall]
prev_cup = CupOFTea.recall_cup
if prev_cup
print "We served you with finest '#{prev_cup}'\n"
else
print "Sorry, we haven't yet served you with a cup of tea. Whould you like something?\n"
end
exit 0
end
print "#{CupOFTea.next_cup}\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment