Class: Sogger::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/sogger/runner.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sogger/runner.rb', line 13

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 = []
  @growler = Meow.new("Sogger")
end

Class Attribute Details

.growl_intervalObject

Returns the value of attribute growl_interval.



4
5
6
# File 'lib/sogger/runner.rb', line 4

def growl_interval
  @growl_interval
end

.loggingObject

Returns the value of attribute logging.



4
5
6
# File 'lib/sogger/runner.rb', line 4

def logging
  @logging
end

.update_intervalObject

Returns the value of attribute update_interval.



4
5
6
# File 'lib/sogger/runner.rb', line 4

def update_interval
  @update_interval
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



7
8
9
# File 'lib/sogger/runner.rb', line 7

def filter
  @filter
end

Instance Method Details

#growl(question) ⇒ Object



82
83
84
85
86
# File 'lib/sogger/runner.rb', line 82

def growl(question)
  @growler.notify(question.tags.join(', '), question.title) do
    system "open", question.url
  end
end

#log(text) ⇒ Object



88
89
90
# File 'lib/sogger/runner.rb', line 88

def log(text)
  puts text if Runner.logging
end

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sogger/runner.rb', line 29

def run
  updater = Thread.new("updater") do
    while true
      log "Updater: Downloading feed..."
      @remote_sogger.download_feed!
  
      new_questions = []
      unless @cached_sogger.questions.empty?
        log "Updater: Comparing feeds..."
        new_questions = (@remote_sogger - @cached_sogger).questions
      end
  
      unless new_questions.empty?
        log "Updater: Adding #{new_questions.size} questions to buffer..."
        @questions_buffer += new_questions
      end
  
      log "Updater: Caching feed..."
      @remote_sogger.save!
  
      log "Updater: Loading cached feed..."
      @cached_sogger.load!
  
      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?
        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 Runner.growl_interval
    end
  end
  
  trap("INT") do
    puts "  Exiting, please wait..."
    [updater, notifier].map(&:kill)
  end
  
  [updater, notifier].map(&:join)
end