Class: Herald::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/herald/watcher.rb,
lib/herald/notifier.rb,
lib/herald/watchers/rss.rb,
lib/herald/notifiers/ping.rb,
lib/herald/notifiers/post.rb,
lib/herald/notifiers/growl.rb,
lib/herald/notifiers/stdout.rb,
lib/herald/watchers/twitter.rb,
lib/herald/watchers/website.rb,
lib/herald/notifiers/callback.rb

Defined Under Namespace

Modules: Rss, Twitter, Website Classes: Notifier

Constant Summary collapse

DEFAULT_TIMER =
60
@@watcher_types =
[:rss, :twitter, :website]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, options, &block) ⇒ Watcher

Returns a new instance of Watcher.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/herald/watcher.rb', line 11

def initialize(type, options, &block)
  @type = type.to_sym
  # check watcher type
  unless @@watcher_types.include?(@type)
    raise ArgumentError, "#{@type} is not a valid Watcher type"
  end
  @keywords = []
  @notifiers = []
  @items = []
  @keep_alive = options.delete(:keep_alive)
  @timer = Watcher::DEFAULT_TIMER
  Herald.lazy_load_module("watchers/#{@type}")
  # include module
  @@type = @type
  class << self
    send(:include, eval(@@type.to_s.capitalize))
  end
  # each individual Watcher will handle their options
  parse_options(options)
  # eval the block, if given
  if block_given?
    instance_eval(&block)
  end
  # TODO implement a Watcher::test()?
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



9
10
11
# File 'lib/herald/watcher.rb', line 9

def items
  @items
end

#keep_aliveObject (readonly)

Returns the value of attribute keep_alive.



8
9
10
# File 'lib/herald/watcher.rb', line 8

def keep_alive
  @keep_alive
end

#keywordsObject

Returns the value of attribute keywords.



9
10
11
# File 'lib/herald/watcher.rb', line 9

def keywords
  @keywords
end

#notifiersObject

Returns the value of attribute notifiers.



9
10
11
# File 'lib/herald/watcher.rb', line 9

def notifiers
  @notifiers
end

#threadObject (readonly)

Returns the value of attribute thread.



8
9
10
# File 'lib/herald/watcher.rb', line 8

def thread
  @thread
end

#timerObject

Returns the value of attribute timer.



9
10
11
# File 'lib/herald/watcher.rb', line 9

def timer
  @timer
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/herald/watcher.rb', line 8

def type
  @type
end

Instance Method Details

#_for(*keywords) ⇒ Object



37
38
39
40
# File 'lib/herald/watcher.rb', line 37

def _for(*keywords)
  @keywords += keywords.flatten
  self
end

#action(type = :callback, options = {}, &block) ⇒ Object

assign the Notifier



43
44
45
46
# File 'lib/herald/watcher.rb', line 43

def action(type = :callback, options = {}, &block)
  @notifiers << Herald::Watcher::Notifier.new(type, options, &block)
  self
end

#every(time) ⇒ Object

parse a hash like { 120 => “seconds” }



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/herald/watcher.rb', line 49

def every(time); 
  quantity = time.keys.first.to_i
  unit = case time.values.first
  when /^second/
    1
  when /^minute/
    60
  when /^hour/
    3600
  when /^day/
    86400
  else
    raise ArgumentError, "Invalid time unit for every. (Use seconds, minutes, hours or days)"
  end
  @timer = quantity * unit
  self
end

#notify(item) ⇒ Object

call the Notifier and pass it a message



68
69
70
71
72
73
# File 'lib/herald/watcher.rb', line 68

def notify(item)
  @notifiers.each do |notifier|
    notifier.notify(item)
  end
  self
end

#startObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/herald/watcher.rb', line 75

def start
  # set a default Notifier for this Watcher
  action(Watcher::Notifier::DEFAULT_NOTIFIER) if @notifiers.empty?
  # prepare() is defined in the individual Watcher modules.
  # any pre-tasks are performed before
  prepare()
  # begin loop, which will execute at least once (like a do-while loop)
  @thread = Thread.new {
    begin
      activities
      sleep @timer if @keep_alive
    end while @keep_alive
  }
  self
end

#stopObject



91
92
93
94
95
96
97
# File 'lib/herald/watcher.rb', line 91

def stop
  # stop looping
  @keep_alive = false
  # cleanup() is defined in the individual Watcher modules
  cleanup()
  self
end