Class: CliTalkyTalk

Inherits:
Object
  • Object
show all
Defined in:
lib/cli-talky-talk.rb,
lib/cli_talky_talk/version.rb

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ CliTalkyTalk

Returns a new instance of CliTalkyTalk.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cli-talky-talk.rb', line 7

def initialize(options=nil)
  @options = options || OpenStruct.new
  begin
    personal_pref_path = Dir.home + '/.cli-talky-talk.yml'
    default_path = File.expand_path('../../', __FILE__) + '/config/preferences.yml'
    [personal_pref_path, default_path].each do |path|
      if File.exist? path
        @preferences = YAML.load_file(path)
        log "using preferences from: #{path}"
        break
      end
    end
    raise "unable to load preferences" unless @preferences 
  rescue Psych::SyntaxError => e
    raise "There was an error parsing the config file: #{e}"
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/cli-talky-talk.rb', line 5

def options
  @options
end

#preferencesObject

Returns the value of attribute preferences.



5
6
7
# File 'lib/cli-talky-talk.rb', line 5

def preferences
  @preferences
end

Class Method Details

.voicesObject



77
78
79
# File 'lib/cli-talky-talk.rb', line 77

def self.voices
  self.new.voices
end

Instance Method Details

#get(key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/cli-talky-talk.rb', line 45

def get(key)
  str = rand preferences['segments'][key.to_s]
  # convert for specific keyword tokens
  case str
  when 'WHOAMI'
    `whoami`.chomp
  else
    str
  end
end

#log(str) ⇒ Object



39
40
41
42
43
# File 'lib/cli-talky-talk.rb', line 39

def log(str)
  if options and options.debug
    puts str
  end
end

#rand(arr) ⇒ Object



25
26
27
# File 'lib/cli-talky-talk.rb', line 25

def rand(arr)
  arr.sample
end

#random_sentenceObject



35
36
37
# File 'lib/cli-talky-talk.rb', line 35

def random_sentence
  "#{get :greetings}, #{get :names}!   #{get :statuses}"
end

#speakObject



29
30
31
32
33
# File 'lib/cli-talky-talk.rb', line 29

def speak
  sentence = options.sentence || random_sentence
  log "sentence: #{sentence}"
  `say -v#{voice} #{sentence}`
end

#voiceObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cli-talky-talk.rb', line 56

def voice
  voice_opts = preferences['options']['voice']
  whitelist = voice_opts['whitelist']
  blacklist = voice_opts['blacklist']

  voice = if options.voice
    options.voice
  elsif whitelist.kind_of?(Array)
    rand whitelist
  else
    avail_voices = voices - blacklist if blacklist.kind_of?(Array)
    rand avail_voices
  end
  log "voice: #{voice}"
  voice
end

#voicesObject



73
74
75
# File 'lib/cli-talky-talk.rb', line 73

def voices
  `say -v \?`.split("\n").select { |v| v =~ /en_/ }.map { |v| v.gsub(/\s+en_[A-Z]{2}.*$/,"")}
end