Permalink
Browse files

Add options for logging and filtering. Accept filter tags from comman…

…d line.
  • Loading branch information...
1 parent 9d49d86 commit c1e5decf5bc7e579007a5e1413d438f52c20a831 @maxim committed Dec 8, 2009
Showing with 42 additions and 14 deletions.
  1. +2 −1 bin/sogger
  2. +40 −13 lib/sogger/runner.rb
View
@@ -2,6 +2,7 @@
require 'rubygems'
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'sogger'))
-sogger = Sogger::Runner.new
+sogger = Sogger::Runner.new(:filter => ARGV)
+sogger.log "Filtering tags: #{sogger.filter.inspect}." unless sogger.filter.empty?
puts "Press CTRL+C to stop..."
sogger.run
View
@@ -1,9 +1,25 @@
module Sogger
class Runner
- UPDATE_INTERVAL = 60
- GROWL_INTERVAL = 20
+ class << self
+ attr_accessor :update_interval, :growl_interval, :logging
+ end
+
+ attr_reader :filter
- def initialize
+ Runner.update_interval = 60
+ Runner.growl_interval = 20
+ Runner.logging = true
+
+ def initialize(options = {})
+ options[:update_interval] ||= Runner.update_interval
+ options[:growl_interval] ||= Runner.growl_interval
+ options[:logging] = options[:logging].nil? ? Runner.logging : options[:logging]
+
+ Runner.update_interval = options[:update_interval]
+ Runner.growl_interval = options[:growl_interval]
+ Runner.logging = options[:logging]
+
+ @filter = [*options[:filter]].compact
@cached_sogger = Sogger.new
@remote_sogger = Sogger.new
@questions_buffer = []
@@ -13,46 +29,53 @@ def initialize
def run
updater = Thread.new("updater") do
while true
- puts "Updater: Downloading feed..."
+ log "Updater: Downloading feed..."
@remote_sogger.download_feed!
new_questions = []
unless @cached_sogger.questions.empty?
- puts "Updater: Comparing feeds..."
+ log "Updater: Comparing feeds..."
new_questions = (@remote_sogger - @cached_sogger).questions
end
unless new_questions.empty?
- puts "Updater: Adding #{new_questions.size} questions to buffer..."
+ log "Updater: Adding #{new_questions.size} questions to buffer..."
@questions_buffer += new_questions
end
- puts "Updater: Caching feed..."
+ log "Updater: Caching feed..."
@remote_sogger.save!
- puts "Updater: Loading cached feed..."
+ log "Updater: Loading cached feed..."
@cached_sogger.load!
- puts "Updater: Sleeping for #{UPDATE_INTERVAL} seconds..."
- sleep UPDATE_INTERVAL
+ log "Updater: Sleeping for #{Runner.update_interval} seconds..."
+ sleep Runner.update_interval
end
end
notifier = Thread.new("notifier") do
while true
unless @questions_buffer.empty?
- puts "\nNotifier: Growling..."
- growl(@questions_buffer.pop)
+ log "\nNotifier: Filtering questions..."
+ while question = @questions_buffer.pop
+ if @filter.empty? || question.tags.any?{ |t| @filter.include?(t) }
+ log "\nNotifier: Growling..."
+ growl(question)
+ break
+ end
+ end
end
- sleep GROWL_INTERVAL
+ sleep Runner.growl_interval
end
end
trap("INT") do
puts " Exiting, please wait..."
[updater, notifier].map(&:kill)
end
+
[updater, notifier].map(&:join)
end
@@ -61,5 +84,9 @@ def growl(question)
system "open", question.url
end
end
+
+ def log(text)
+ puts text if Runner.logging
+ end
end
end

0 comments on commit c1e5dec

Please sign in to comment.