Class: Everyx::PeriodicTasks::HTMLMonitor

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

Overview

This class will monitor a normal URI and notify on changes that match a pattern.

Example:

htmlmon = Everyx::PeriodicTasks::HTMLMonitor.new
htmlmon.uri = "http://www.slashdot.org/"
htmlmon.pattern = "//div[@class='title']//a"

The class uses Hpricot to parse HTML, which accepts either XPath or CSS-like selectors to match.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTMLMonitor

Returns a new instance of HTMLMonitor.



141
142
143
144
145
# File 'lib/everyx.rb', line 141

def initialize
  @uri = nil
  @last = nil
  @pattern = nil
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



139
140
141
# File 'lib/everyx.rb', line 139

def pattern
  @pattern
end

#uriObject (readonly)

Returns the value of attribute uri.



138
139
140
# File 'lib/everyx.rb', line 138

def uri
  @uri
end

Instance Method Details

#callObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/everyx.rb', line 147

def call
  doc = Hpricot( open(uri) {|f| f.read } )
  new = []
  new_last = nil
  doc.search( @pattern ).each do |ele|
    if ele.inner_html != @last
      new << ele.inner_html
      new_last = new.last unless new_last
    end
  end        
  @last = new_last if new_last
  if new.size > 0
    msg = NServer::Message.new
    msg.title = "Website updated."
    msg.text = new.collect {|n| "* #{n}"}.join("\n")
    NServer::Client.try_notify( msg )
  end
end