text-to-noise 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/text_to_noise +3 -0
- data/lib/text_to_noise.rb +18 -4
- data/lib/text_to_noise/command_line.rb +5 -0
- data/lib/text_to_noise/log_reader.rb +1 -1
- data/lib/text_to_noise/version.rb +1 -1
- data/spec/text_to_noise/command_line_spec.rb +14 -0
- data/spec/text_to_noise/log_reader_spec.rb +6 -1
- metadata +1 -1
data/bin/text_to_noise
CHANGED
@@ -23,6 +23,9 @@ arg_parser = OptionParser.new do |opts|
|
|
23
23
|
"Read input to sound mapping configuration from MAPPING_CONFIG" ) { |c| options[:config] = c }
|
24
24
|
opts.on( "-m", "--mute",
|
25
25
|
"Don't play any sounds, just print what matched" ) { options[:mute] = true }
|
26
|
+
opts.on( "-t", "--throttle DELAY",
|
27
|
+
"Wait DELAY milliseconds before reading the next line of input." ) { |ms| options[:throttle] = ms.to_i }
|
28
|
+
opts.on( "--debug", "Turn on debug logging" ) { options[:debug] = true }
|
26
29
|
opts.on_tail("-h", "--help", "Show this message") do
|
27
30
|
puts opts
|
28
31
|
exit
|
data/lib/text_to_noise.rb
CHANGED
@@ -10,20 +10,34 @@ Dir["#{LIB_DIR}/text_to_noise/*.rb"].each { |lib|
|
|
10
10
|
}
|
11
11
|
|
12
12
|
module TextToNoise
|
13
|
-
|
13
|
+
include Logging
|
14
|
+
|
15
|
+
def player
|
14
16
|
@player ||= Player.new
|
15
17
|
end
|
16
18
|
|
17
|
-
def
|
19
|
+
def player=( player )
|
18
20
|
@player = player
|
19
21
|
end
|
20
22
|
|
21
|
-
def
|
23
|
+
def logger
|
22
24
|
@logger ||= Logger.new( STDOUT ).tap { |l| l.level = Logger::INFO }
|
23
25
|
end
|
24
26
|
|
25
|
-
def
|
27
|
+
def logger=( logger )
|
26
28
|
@logger = logger
|
27
29
|
@logger
|
28
30
|
end
|
31
|
+
|
32
|
+
def throttle_delay=( ms_delay )
|
33
|
+
@delay = ms_delay
|
34
|
+
end
|
35
|
+
|
36
|
+
def throttle!
|
37
|
+
return unless @delay
|
38
|
+
debug "waiting #{@delay}ms (#{@delay / 1_000.0}s)"
|
39
|
+
sleep @delay / 1_000.0
|
40
|
+
end
|
41
|
+
|
42
|
+
extend self
|
29
43
|
end
|
@@ -4,6 +4,8 @@ module TextToNoise
|
|
4
4
|
class CommandLine
|
5
5
|
include Logging
|
6
6
|
attr_reader :options, :mapping
|
7
|
+
|
8
|
+
DEFAULT_FILE_DELAY = 100
|
7
9
|
|
8
10
|
def initialize( options={} )
|
9
11
|
@options = {
|
@@ -14,6 +16,9 @@ module TextToNoise
|
|
14
16
|
|
15
17
|
@mapping = Mapper.parse File.read( @options[:config] )
|
16
18
|
TextToNoise.player = self.player
|
19
|
+
TextToNoise.throttle_delay = @options[:throttle] if @options[:throttle]
|
20
|
+
TextToNoise.throttle_delay = @options[:throttle] || DEFAULT_FILE_DELAY if @options[:input] != $stdin
|
21
|
+
TextToNoise.logger.level = Logger::DEBUG if @options[:debug]
|
17
22
|
rescue Errno::ENOENT => e
|
18
23
|
raise ArgumentError, "Could not locate configuration file: '#{@options[:config]}'"
|
19
24
|
end
|
@@ -44,6 +44,20 @@ module TextToNoise
|
|
44
44
|
CommandLine.new :mute => true, :config => "sound_map.rb"
|
45
45
|
end
|
46
46
|
end
|
47
|
+
|
48
|
+
context "when given the 'throttle' option" do
|
49
|
+
it "sets the throttle_delay attribute on TextToNoise" do
|
50
|
+
TextToNoise.should_receive( :throttle_delay= ).with 100
|
51
|
+
CommandLine.new :config => "sound_map.rb", :throttle => 100
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "when given a 'file' option" do
|
56
|
+
it "sets a default throttle_delay" do
|
57
|
+
TextToNoise.should_receive( :throttle_delay= ).with 100
|
58
|
+
CommandLine.new :config => "sound_map.rb", :input => "sample.log"
|
59
|
+
end
|
60
|
+
end
|
47
61
|
end
|
48
62
|
|
49
63
|
describe "#run" do
|
@@ -4,7 +4,7 @@ require 'stringio'
|
|
4
4
|
module TextToNoise
|
5
5
|
describe LogReader do
|
6
6
|
let( :io ) { StringIO.new "phantasm" }
|
7
|
-
let( :mapper ) { double( "LineToSoundMapper" ) }
|
7
|
+
let( :mapper ) { double( "LineToSoundMapper", :dispatch => nil ) }
|
8
8
|
|
9
9
|
subject { LogReader.new io, mapper }
|
10
10
|
|
@@ -18,6 +18,11 @@ module TextToNoise
|
|
18
18
|
mapper.should_receive( :dispatch ).with "phantasm"
|
19
19
|
subject.call
|
20
20
|
end
|
21
|
+
|
22
|
+
it "calls TextToNoise.throttle! after each line is processed" do
|
23
|
+
TextToNoise.should_receive( :throttle! )
|
24
|
+
subject.call
|
25
|
+
end
|
21
26
|
end
|
22
27
|
end
|
23
28
|
end
|