Detect a Secret Knock message after a given timeout

#!/usr/bin/env ruby

require 'io/console'
require 'logger'
require 'secret_knock'


c = ' '


sk = SecretKnock.new

sk.detect

def sk.on_timeout(msg) 
  logger = Logger.new 'sk.log'
  puts ':: ' + msg.inspect
  logger.debug  '::' + msg.inspect
end

puts 'press the space bar to knock, or any other key to exit'
sk.knock while (c = $stdin.getch ) == ' ' and sk.listening

puts "Your message was " + sk.message

In the above example a secret message can be knocked out and logged to the local file sk.log. By default the SecretKnock#detect method will wait for 2 seconds before it executes the SecretKnock#on_timemout method and then listens again for more secret knock messages.

Resources