Class: Herald::Watcher
- Inherits:
-
Object
- Object
- Herald::Watcher
- 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
-
#items ⇒ Object
Returns the value of attribute items.
-
#keep_alive ⇒ Object
readonly
Returns the value of attribute keep_alive.
-
#keywords ⇒ Object
Returns the value of attribute keywords.
-
#notifiers ⇒ Object
Returns the value of attribute notifiers.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#timer ⇒ Object
Returns the value of attribute timer.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #_for(*keywords) ⇒ Object
-
#action(type = :callback, options = {}, &block) ⇒ Object
assign the Notifier.
-
#every(time) ⇒ Object
parse a hash like { 120 => “seconds” }.
-
#initialize(type, options, &block) ⇒ Watcher
constructor
A new instance of Watcher.
-
#notify(item) ⇒ Object
call the Notifier and pass it a message.
- #start ⇒ Object
- #stop ⇒ Object
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, , &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 = .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 () # eval the block, if given if block_given? instance_eval(&block) end # TODO implement a Watcher::test()? end |
Instance Attribute Details
#items ⇒ Object
Returns the value of attribute items.
9 10 11 |
# File 'lib/herald/watcher.rb', line 9 def items @items end |
#keep_alive ⇒ Object (readonly)
Returns the value of attribute keep_alive.
8 9 10 |
# File 'lib/herald/watcher.rb', line 8 def keep_alive @keep_alive end |
#keywords ⇒ Object
Returns the value of attribute keywords.
9 10 11 |
# File 'lib/herald/watcher.rb', line 9 def keywords @keywords end |
#notifiers ⇒ Object
Returns the value of attribute notifiers.
9 10 11 |
# File 'lib/herald/watcher.rb', line 9 def notifiers @notifiers end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
8 9 10 |
# File 'lib/herald/watcher.rb', line 8 def thread @thread end |
#timer ⇒ Object
Returns the value of attribute timer.
9 10 11 |
# File 'lib/herald/watcher.rb', line 9 def timer @timer end |
#type ⇒ Object (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, = {}, &block) @notifiers << Herald::Watcher::Notifier.new(type, , &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 |
#start ⇒ Object
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 |
#stop ⇒ Object
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 |