Class: TimeTeller

Inherits:
Object
  • Object
show all
Defined in:
lib/time_teller.rb

Instance Method Summary collapse

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_synthObject



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_actionObject



74
75
76
77
# File 'lib/time_teller.rb', line 74

def chance_action
  #puts "Randomizing: #{random}"
  tell_time if randomize == 0
end

#default_actionObject



89
90
91
# File 'lib/time_teller.rb', line 89

def default_action
  tell_time
end

#espeak_synthObject



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_timeObject



59
60
61
# File 'lib/time_teller.rb', line 59

def formatted_time
  "It is #{time}."
end

#mac_synthObject



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

#randomizeObject



63
64
65
# File 'lib/time_teller.rb', line 63

def randomize
  Random.new.rand(@value) unless @value.nil?
end

#runObject

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_actionObject



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_commandObject

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_timeObject



54
55
56
57
# File 'lib/time_teller.rb', line 54

def tell_time
  #puts "Running command: #{synth_command}"
  %x(#{synth_command})
end

#timeObject



48
49
50
51
52
# File 'lib/time_teller.rb', line 48

def time
  time = Time.now.fuzzy
  #puts "Time is: #{time}"
  time
end

#voicesObject



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_actionObject



85
86
87
# File 'lib/time_teller.rb', line 85

def voices_action
  puts "Available voices: #{voices.join ', '}"
end