Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Commit

Permalink
Add options for logging and filtering. Accept filter tags from comman…
Browse files Browse the repository at this point in the history
…d line.
  • Loading branch information
maxim committed Dec 8, 2009
1 parent 9d49d86 commit c1e5dec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
3 changes: 2 additions & 1 deletion bin/sogger
Expand Up @@ -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
53 changes: 40 additions & 13 deletions lib/sogger/runner.rb
@@ -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 = []
Expand All @@ -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

Expand All @@ -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.