Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env ruby
require 'optparse'
require 'yaml'
require 'date'
class Parser
def self.parse_options(args)
options = {}
opt_parser = OptionParser.new do |opts|
opts.banner = "Greets whoever is identified by data.yml."
opts.separator "Will use the cowsay program, if it is installed."
opts.separator "Usage: hello.rb [options]"
opts.on("-c", "--cow COW", "Show message using cow type COW") do |c|
options["cow"] = c
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end
opt_parser.parse!(args)
return options
end
end
class Greeting
def self.greet
case DateTime.now.hour
when 0..11
"Good morning"
when 12..16
"Good afternoon"
else
"Good evening"
end
end
end
class MaybeCow
def self.command?(command)
system("which #{command} > /dev/null 2>&1")
end
def self.program(options)
primary = "/usr/games/cowsay"
alt = "/usr/bin/tee"
if command?(primary)
options["cow"] ? "#{primary} -f #{options["cow"]}" : primary
else
alt
end
end
end
options = Parser.parse_options(ARGV)
data = YAML.load_file('data.yml')
name =
if data["first"]
"#{data["first"]} #{data["last"]} (#{ENV["USER"]})"
else
end
IO.popen(MaybeCow.program(options), "w") do |c|
c.puts "#{Greeting.greet} #{name}!"
c.close
puts
end