Class: CliTalkyTalk
- Inherits:
-
Object
- Object
- CliTalkyTalk
- Defined in:
- lib/cli-talky-talk.rb,
lib/cli_talky_talk/version.rb
Constant Summary collapse
- VERSION =
"0.2.1"
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#preferences ⇒ Object
Returns the value of attribute preferences.
Class Method Summary collapse
Instance Method Summary collapse
- #get(key) ⇒ Object
-
#initialize(options = nil) ⇒ CliTalkyTalk
constructor
A new instance of CliTalkyTalk.
- #log(str) ⇒ Object
- #rand(arr) ⇒ Object
- #random_sentence ⇒ Object
- #speak ⇒ Object
- #voice ⇒ Object
- #voices ⇒ Object
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(=nil) @options = || OpenStruct.new begin personal_pref_path = Dir.home + '/.cli-talky-talk.yml' default_path = File.('../../', __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
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/cli-talky-talk.rb', line 5 def @options end |
#preferences ⇒ Object
Returns the value of attribute preferences.
5 6 7 |
# File 'lib/cli-talky-talk.rb', line 5 def preferences @preferences end |
Class Method Details
.voices ⇒ Object
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 and .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_sentence ⇒ Object
35 36 37 |
# File 'lib/cli-talky-talk.rb', line 35 def random_sentence "#{get :greetings}, #{get :names}! #{get :statuses}" end |
#speak ⇒ Object
29 30 31 32 33 |
# File 'lib/cli-talky-talk.rb', line 29 def speak sentence = .sentence || random_sentence log "sentence: #{sentence}" `say -v#{voice} #{sentence}` end |
#voice ⇒ Object
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 .voice .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 |
#voices ⇒ Object
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 |