Class: TimeTeller
- Inherits:
-
Object
- Object
- TimeTeller
- Defined in:
- lib/time_teller.rb
Instance Method Summary collapse
- #aplay_synth ⇒ Object
- #chance_action ⇒ Object
- #default_action ⇒ Object
- #espeak_synth ⇒ Object
- #formatted_time ⇒ Object
-
#initialize(opts = {}) ⇒ TimeTeller
constructor
A new instance of TimeTeller.
- #mac_synth ⇒ Object
- #parse(opts) ⇒ Object
- #randomize ⇒ Object
-
#run ⇒ Object
TODO: Refactor into class.
- #sleep_action ⇒ Object
-
#synth_command ⇒ Object
TODO: Refactor into class.
- #tell_time ⇒ Object
- #time ⇒ Object
- #voices ⇒ Object
- #voices_action ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ TimeTeller
Returns a new instance of TimeTeller.
8 9 10 11 12 |
# File 'lib/time_teller.rb', line 8 def initialize(opts = {}) @action = :default @synth = :mac parse(opts) end |
Instance Method Details
#aplay_synth ⇒ Object
100 101 102 103 104 105 |
# File 'lib/time_teller.rb', line 100 def aplay_synth args = [] args << "-v #{@voice}" unless @voice.nil? #puts "voice: #{@voice}" "espeak '#{formatted_time}' #{args.join ' '} --stdout 2>/dev/null | aplay -q -D 'default'" end |
#chance_action ⇒ Object
74 75 76 77 |
# File 'lib/time_teller.rb', line 74 def chance_action #puts "Randomizing: #{random}" tell_time if randomize == 0 end |
#default_action ⇒ Object
89 90 91 |
# File 'lib/time_teller.rb', line 89 def default_action tell_time end |
#espeak_synth ⇒ Object
107 108 109 110 111 112 |
# File 'lib/time_teller.rb', line 107 def espeak_synth args = [] args << "-v #{@voice}" unless @voice.nil? #puts "voice: #{@voice}" "espeak '#{formatted_time}' #{args.join ' '}" end |
#formatted_time ⇒ Object
59 60 61 |
# File 'lib/time_teller.rb', line 59 def formatted_time "It is #{time}." end |
#mac_synth ⇒ Object
114 115 116 117 118 |
# File 'lib/time_teller.rb', line 114 def mac_synth args = [] args << "--voice #{@voice}" unless @voice.nil? "say '#{formatted_time}' #{args.join ' '}" end |
#parse(opts) ⇒ Object
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 |
# File 'lib/time_teller.rb', line 14 def parse(opts) @voice = opts["--voice"] || 'english' if opts["--synth"] == 'espeak' @synth = :espeak elsif opts["--synth"] == 'aplay' @synth = :aplay else @voice = opts["--voice"] || 'Vicki' end @voice = voices.sample if opts["--random"] unless voices.include?(@voice) raise TimeTellerError.new "Voice not available: #{@voice}" end if opts["--chance"] @action = :chance @value = opts["--chance"].to_i end if opts["--sleep"] @action = :sleep @value = opts["--sleep"].to_i end if opts["--voices"] @action = :voices end end |
#randomize ⇒ Object
63 64 65 |
# File 'lib/time_teller.rb', line 63 def randomize Random.new.rand(@value) unless @value.nil? end |
#run ⇒ Object
TODO: Refactor into class
68 69 70 71 72 |
# File 'lib/time_teller.rb', line 68 def run method = "#{@action}_action".to_sym raise TimeTellerError.new "Action not available: #{@action}" unless respond_to?(method) send(method) end |
#sleep_action ⇒ Object
79 80 81 82 83 |
# File 'lib/time_teller.rb', line 79 def sleep_action #puts "Sleeping #{random}s" sleep randomize tell_time end |
#synth_command ⇒ Object
TODO: Refactor into class
94 95 96 97 98 |
# File 'lib/time_teller.rb', line 94 def synth_command method = "#{@synth}_synth".to_sym raise TimeTellerError.new "Synth not available: #{@synth}" unless respond_to?(method) send(method) end |
#tell_time ⇒ Object
54 55 56 57 |
# File 'lib/time_teller.rb', line 54 def tell_time #puts "Running command: #{synth_command}" %x(#{synth_command}) end |
#time ⇒ Object
48 49 50 51 52 |
# File 'lib/time_teller.rb', line 48 def time time = Time.now.fuzzy #puts "Time is: #{time}" time end |
#voices ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/time_teller.rb', line 40 def voices @voices ||= if [:espeak, :aplay].include? @synth %x(espeak --voices=en | tail -n +2 | sed 's/^ *//' | tr -s ' '| cut -d ' ' -f 4).split else %x(say --voice ? | awk '{print $1;}').split end end |
#voices_action ⇒ Object
85 86 87 |
# File 'lib/time_teller.rb', line 85 def voices_action puts "Available voices: #{voices.join ', '}" end |