time_teller 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/tell_time +37 -0
  3. data/lib/time_teller.rb +124 -0
  4. metadata +88 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b4129d49f68a0abe669174a42b2ad275ec23dfd3
4
+ data.tar.gz: 2a2110a91577713b2cab6c368f204a116ca375eb
5
+ SHA512:
6
+ metadata.gz: e7941330159919c6a1ced6b07a4788b3b71ab6b0463d99f82270a171998000cfaa96715e5d03abf813037c920ea4285294e6861a35561eb50a292303cfb40d81
7
+ data.tar.gz: fd2475b94c61c9b786c4e2ad7c999250cf167cc51904c3f87e4fddbd791d95daae956d0e96bed4a6aca412bdc68978d6bf5953acbaad3fdb35617c7713a41e51
data/bin/tell_time ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+
7
+ require 'time_teller'
8
+ require 'docopt'
9
+
10
+ doc = <<DOCOPT
11
+ Speak the current time.
12
+
13
+ Usage: #{__FILE__} [options]
14
+
15
+ --chance CHANCE 1 in CHANCE times chance that time is spoken.
16
+ --sleep SECONDS Sleep up to X seconds before speaking.
17
+ --synth SYNTH Change what synthesizer to use (espeak, aplay, say). [default: say].
18
+ --voice VOICE Use VOICE name (for Mac say command)
19
+ --random Use a random voice (for Mac say command)
20
+ --voices Print available voices (for Mac say command)
21
+ -h, --help Print this help message.
22
+ DOCOPT
23
+
24
+ begin
25
+ opts = Docopt::docopt(doc)
26
+ #puts opts.inspect
27
+ rescue Docopt::Exit => e
28
+ $stderr.puts e.message
29
+ exit 1
30
+ end
31
+
32
+ begin
33
+ TimeTeller.new(opts).run
34
+ rescue TimeTellerError => e
35
+ $stderr.puts e.message
36
+ exit 2
37
+ end
@@ -0,0 +1,124 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fuzzy_time'
4
+
5
+ class TimeTellerError < StandardError; end
6
+
7
+ class TimeTeller
8
+ def initialize(opts = {})
9
+ @action = :default
10
+ @synth = :mac
11
+ parse(opts)
12
+ end
13
+
14
+ def parse(opts)
15
+ @voice = opts["--voice"] || 'english'
16
+ if opts["--synth"] == 'espeak'
17
+ @synth = :espeak
18
+ elsif opts["--synth"] == 'aplay'
19
+ @synth = :aplay
20
+ else
21
+ @voice = opts["--voice"] || 'Vicki'
22
+ end
23
+ @voice = voices.sample if opts["--random"]
24
+ unless voices.include?(@voice)
25
+ raise TimeTellerError.new "Voice not available: #{@voice}"
26
+ end
27
+ if opts["--chance"]
28
+ @action = :chance
29
+ @value = opts["--chance"].to_i
30
+ end
31
+ if opts["--sleep"]
32
+ @action = :sleep
33
+ @value = opts["--sleep"].to_i
34
+ end
35
+ if opts["--voices"]
36
+ @action = :voices
37
+ end
38
+ end
39
+
40
+ def voices
41
+ @voices ||= if [:espeak, :aplay].include? @synth
42
+ %x(espeak --voices=en | tail -n +2 | sed 's/^ *//' | tr -s ' '| cut -d ' ' -f 4).split
43
+ else
44
+ %x(say --voice ? | awk '{print $1;}').split
45
+ end
46
+ end
47
+
48
+ def time
49
+ time = Time.now.fuzzy
50
+ #puts "Time is: #{time}"
51
+ time
52
+ end
53
+
54
+ def tell_time
55
+ #puts "Running command: #{synth_command}"
56
+ %x(#{synth_command})
57
+ end
58
+
59
+ def formatted_time
60
+ "It is #{time}."
61
+ end
62
+
63
+ def randomize
64
+ Random.new.rand(@value) unless @value.nil?
65
+ end
66
+
67
+ # TODO: Refactor into class
68
+ def run
69
+ method = "#{@action}_action".to_sym
70
+ raise TimeTellerError.new "Action not available: #{@action}" unless respond_to?(method)
71
+ send(method)
72
+ end
73
+
74
+ def chance_action
75
+ #puts "Randomizing: #{random}"
76
+ tell_time if randomize == 0
77
+ end
78
+
79
+ def sleep_action
80
+ #puts "Sleeping #{random}s"
81
+ sleep randomize
82
+ tell_time
83
+ end
84
+
85
+ def voices_action
86
+ puts "Available voices: #{voices.join ', '}"
87
+ end
88
+
89
+ def default_action
90
+ tell_time
91
+ end
92
+
93
+ # TODO: Refactor into class
94
+ def synth_command
95
+ method = "#{@synth}_synth".to_sym
96
+ raise TimeTellerError.new "Synth not available: #{@synth}" unless respond_to?(method)
97
+ send(method)
98
+ end
99
+
100
+ def aplay_synth
101
+ args = []
102
+ args << "-v #{@voice}" unless @voice.nil?
103
+ #puts "voice: #{@voice}"
104
+ "espeak '#{formatted_time}' #{args.join ' '} --stdout 2>/dev/null | aplay -q -D 'default'"
105
+ end
106
+
107
+ def espeak_synth
108
+ args = []
109
+ args << "-v #{@voice}" unless @voice.nil?
110
+ #puts "voice: #{@voice}"
111
+ "espeak '#{formatted_time}' #{args.join ' '}"
112
+ end
113
+
114
+ def mac_synth
115
+ args = []
116
+ args << "--voice #{@voice}" unless @voice.nil?
117
+ "say '#{formatted_time}' #{args.join ' '}"
118
+ end
119
+
120
+ end
121
+
122
+ if __FILE__ == $0
123
+ TimeTeller.new(ARGV).run
124
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_teller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Olle Johansson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fuzzy_time
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: docopt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ description: Speaks the time in English.
56
+ email: Olle@Johansson.com
57
+ executables:
58
+ - tell_time
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - bin/tell_time
63
+ - lib/time_teller.rb
64
+ homepage: http://rubygems.org/gems/time_teller
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.2.2
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Tell time
88
+ test_files: []