Skip to content
Browse files

configuration via DSL

  • Loading branch information...
1 parent f6c6e6f commit eb16b8335c665a41c46bcb4ee68b6c1f34a90bec @sferik committed
Showing with 74 additions and 81 deletions.
  1. +2 −4 README.markdown
  2. +5 −6 bin/event_simulator
  3. +53 −71 bin/racket
  4. +14 −0 config/matcher.rb
View
6 README.markdown
@@ -11,8 +11,7 @@ log file, a la gltail.
Sounds live in the "sounds" directory; wav, mp3, aiff and even wacky formats like OggVorbis are supported.
-Events and corresponding reactions are defined in the events hash;
-this should be yaml-ified, prolly.
+Events and corresponding reactions are defined in `config/matcher.rb`
## Installing:
@@ -29,8 +28,7 @@ this should be yaml-ified, prolly.
# bask in the insanity of your app's racket!
## TODO:
- * implement tailing over ssh, ala GlTail
- * yaml-ize sound reaction mapping
+ * implement tailing over ssh, a la GlTail
* implement light control via [arduino](http://rad.rubyforge.org/)
* figure out if syslog is what we want for aggregate logfiles from
multiple apps/servers into a single stream
View
11 bin/event_simulator
@@ -2,16 +2,15 @@
# generate a fake stream of events for testing
events = [
- 'tweet_share',
+ 'tweet',
'signup',
+ 'invite',
'login',
- 'tweet_invite'
+ 'error',
]
-users = [ 'jm3', 'sferik', 'sferik', 'vanessa', 'jon', 'kevin' ]
-
while true
- print "#{Time.now}, #{events[rand(events.size)]}, #{users[rand(users.size)]}\n"
+ puts "#{Time.now}, #{events[rand(events.size)]}"
$stdout.flush
- sleep(rand(5))
+ sleep(rand(3))
end
View
124 bin/racket
@@ -2,99 +2,81 @@
require 'rubygems'
require 'rubygame'
-#require 'net/ssh'
require 'file/tail'
+require 'singleton'
include Rubygame
-class Event
- attr_accessor :name, :desc, :reaction
- def initialize(name, desc, reaction)
- @name = name
- @desc = desc
- @reaction = reaction
+class Racket
+ include Singleton
+ attr_accessor :events
+
+ def initialize
+ @events = []
end
- def to_s
- "Event: #{name}: #{desc}\n" + reaction.to_s
+ # Push an event to the bottom of the stack.
+ def push(event)
+ @events.push(event)
+ end
+
+ # Removes the top-most event.
+ def pop
+ @events.pop
end
end
-class Reaction
- attr_accessor :sound, :light
+class Event
+ attr_accessor :regex, :description, :reaction
- def initialize(sound, light)
- @sound = sound
- @light = light
+ def initialize(regex, description)
+ @regex = regex
+ @description = description
+ end
+
+ def play(sound)
+ @reaction = Reaction.new(sound)
end
def to_s
- " sound: #{sound}\n" +
- " light color: #{light[:color]}\n light duration: #{light[:duration]}"
+ "#{description}\n"
+ end
+end
+
+class Reaction
+ attr_accessor :sound
+
+ def initialize(sound)
+ @sound = "sounds/#{sound}"
end
end
Rubygame.init
-def handle_events
+def match(regex, description)
+ @racket = Racket.instance
+ @event = Event.new(regex, description)
+ @racket.push(@event)
+ @event
+end
+def run
# puts "Using audio driver:" + Rubygame.audio_driver
- Sound.autoload_dirs = [ "sounds" ]
-
- events = {}
- responders = []
-
- # TODO: yaml-ize me
- events[:tweet_share] =
- Event.new('Share Tweet',
- 'A user just tweeted something about us to a friend',
- Reaction.new(
- 'sounds/notify_cardinal1.aiff',
- {:color => :red, :duration => 1}))
-
- events[:tweet_invite] =
- Event.new('Invite Tweet',
- 'A user just invited XXX people to the app',
- Reaction.new(
- 'sounds/squeaks.wav',
- {:color => :blue, :duration => 2}))
-
- events[:signup] =
- Event.new('Invite Tweet',
- 'Someone just signed up',
- Reaction.new(
- 'sounds/notify_robin.aiff',
- {:color => :green, :duration => 3}))
-
- events[:login] =
- Event.new('Log In',
- 'A user just logged in',
- Reaction.new(
- 'sounds/savage.wav',
- {:color => :yellow, :duration => 4}))
-
- # keep running so long as sounds are playing
- responders.each_with_index do |responder,i|
- while responder.playing? or responder.fading? == :out do Thread.pass end
- responders.delete(i)
- end
+ Sound.autoload_dirs = ["sounds"]
+
+ require 'config/matcher'
- @log = File.open('dummy.log')
- @log.extend(File::Tail)
- @log.tail { |line|
- ruby, isweird, event, actor, time = /^(.+), (.+), (.+)$/.match(line).to_a
- if event
- puts "responding to #{event}"
- if events.has_key?(event.to_sym)
- responders << Sound.load(events[event.to_sym].reaction.sound)
- responders.last.play
- else
- puts 'EPIC FAIL: reaction definition missing for event ' + event
+ @file = File.open(ARGV.first)
+ @file.extend(File::Tail)
+ @file.tail do |line|
+ @racket.events.each do |event|
+ if line.match(event.regex)
+ puts event
+ Sound.load(event.reaction.sound).play
end
end
- }
-
+ end
end
-event_thread = Thread.new do handle_events end
+event_thread = Thread.new{run}
event_thread.join
-Rubygame.quit()
+Rubygame.quit
View
14 config/matcher.rb
@@ -0,0 +1,14 @@
+match(/tweet/, ":) Someone just tweeted!").
+ play("bird_1.aiff")
+
+match(/signup/, ":) Someone just signed up!").
+ play("bird_2.aiff")
+
+match(/invite/, ":) Someone just sent an invite!").
+ play("bird_3.aiff")
+
+match(/login/, ":) Someone just logged in!").
+ play("bird_4.aiff")
+
+match(/error/, ":( Something bad happened!").
+ play("cat.aiff")

0 comments on commit eb16b83

Please sign in to comment.
Something went wrong with that request. Please try again.