class TimeTeller

Public Class Methods

new(opts = {}) click to toggle source
# File lib/time_teller.rb, line 8
def initialize(opts = {})
  @action = :default
  @synth = :mac
  parse(opts)
end

Public Instance Methods

aplay_synth() click to toggle source
# 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() click to toggle source
# File lib/time_teller.rb, line 74
def chance_action
  #puts "Randomizing: #{random}"
  tell_time if randomize == 0
end
default_action() click to toggle source
# File lib/time_teller.rb, line 89
def default_action
  tell_time
end
espeak_synth() click to toggle source
# 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() click to toggle source
# File lib/time_teller.rb, line 59
def formatted_time
  "It is #{time}."
end
mac_synth() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/time_teller.rb, line 63
def randomize
  Random.new.rand(@value) unless @value.nil?
end
run() click to toggle source

TODO: Refactor into class

# 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() click to toggle source
# File lib/time_teller.rb, line 79
def sleep_action
  #puts "Sleeping #{random}s"
  sleep randomize
  tell_time
end
synth_command() click to toggle source

TODO: Refactor into class

# 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() click to toggle source
# File lib/time_teller.rb, line 54
def tell_time
  #puts "Running command: #{synth_command}"
  %x(#{synth_command})
end
time() click to toggle source
# File lib/time_teller.rb, line 48
def time
  time = Time.now.fuzzy
  #puts "Time is: #{time}"
  time
end
voices() click to toggle source
# 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() click to toggle source
# File lib/time_teller.rb, line 85
def voices_action
  puts "Available voices: #{voices.join ', '}"
end