class AlbaHabla::Commands

Main class used to process commands

Constants

DEFAULT_VOICES

Attributes

voice[R]

Public Class Methods

new(book_path, voice = nil) click to toggle source
# File lib/alba_habla/commands.rb, line 11
def initialize(book_path, voice = nil)
  @voice = voice || DEFAULT_VOICES[executable]
  @book_path = book_path
end

Public Instance Methods

process_command(command) click to toggle source
# File lib/alba_habla/commands.rb, line 16
def process_command(command)
  subcommand = command.split(' ').first
  if available_subcommands.include? subcommand
    send(subcommand, command.split(' ')[1..-1].join(' '))
  else
    talk(command)
  end
end

Private Instance Methods

available_subcommands() click to toggle source
# File lib/alba_habla/commands.rb, line 94
def available_subcommands
  %w[read spell]
end
book_file(book_name) click to toggle source
# File lib/alba_habla/commands.rb, line 57
def book_file(book_name)
  File.join(
    @book_path,
    books.key?(book_name) ? books[book_name] : book_name
  )
end
books() click to toggle source

Convenience mapping of a few books. i.e. typing “read sam” is the same as “read green_eggs_and_ham.txt”

# File lib/alba_habla/commands.rb, line 47
def books
  {
    'sam' => 'green_eggs_and_ham.txt',
    'cat' => 'the_cat_in_the_hat.txt',
    'ladybird' => 'what_the_ladybird_heard.txt',
    'ladybird2' => 'what_the_lady_bird_heard_next.txt',
    'fox' => 'fox_in_socks.txt',
  }
end
cli_options() click to toggle source
# File lib/alba_habla/commands.rb, line 33
def cli_options
  "-v #{voice}"
end
executable() click to toggle source
# File lib/alba_habla/commands.rb, line 27
def executable
  @executable ||= %w[espeak say].reject do |ex|
    `which #{ex}` == ''
  end.first
end
read(book_name) click to toggle source
# File lib/alba_habla/commands.rb, line 76
def read(book_name)
  if File.exist?(book_file(book_name))
    IO.foreach(book_file(book_name)) do |line|
      puts line
      talk line.delete('"')
    end
  else
    talk "Sorry, I don't know that book. Ask Daddy to add it."
  end
end
spell(_) click to toggle source
# File lib/alba_habla/commands.rb, line 87
def spell(_)
  word = word_bag.sample
  puts word.downcase
  talk "Let's spell #{word}."
  talk word.split('').join(' ').to_s
end
talk(string) click to toggle source
# File lib/alba_habla/commands.rb, line 41
def talk(string)
  `#{talk_command} "#{string}"`
end
talk_command() click to toggle source
# File lib/alba_habla/commands.rb, line 37
def talk_command
  "#{executable} #{cli_options}"
end
word_bag() click to toggle source
# File lib/alba_habla/commands.rb, line 64
def word_bag
  @word_bag || begin
    bag = []
    books.values.each do |book_file|
      IO.foreach(@book_path + book_file) do |line|
        bag << line.gsub(/[,'\.!\?]/, '').split(' ')
      end
    end
    bag.flatten.compact.uniq
  end
end