text-to-noise 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -10,20 +10,34 @@ Dir["#{LIB_DIR}/text_to_noise/*.rb"].each { |lib|
10
10
  }
11
11
 
12
12
  module TextToNoise
13
- def self.player
13
+ include Logging
14
+
15
+ def player
14
16
  @player ||= Player.new
15
17
  end
16
18
 
17
- def self.player=( player )
19
+ def player=( player )
18
20
  @player = player
19
21
  end
20
22
 
21
- def self.logger
23
+ def logger
22
24
  @logger ||= Logger.new( STDOUT ).tap { |l| l.level = Logger::INFO }
23
25
  end
24
26
 
25
- def self.logger=( logger )
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
@@ -8,7 +8,7 @@ module TextToNoise
8
8
  def call()
9
9
  while line = io.gets
10
10
  @mapper.dispatch line
11
- sleep 0.200 # FIXME: Think of a better way to throttle playback.
11
+ TextToNoise.throttle!
12
12
  end
13
13
  end
14
14
  end
@@ -1,3 +1,3 @@
1
1
  module TextToNoise
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  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
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: text-to-noise
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Toby Tripp