Skip to content

Instantly share code, notes, and snippets.

@johnbintz
Created February 22, 2011 14:56
Show Gist options
  • Save johnbintz/838790 to your computer and use it in GitHub Desktop.
Save johnbintz/838790 to your computer and use it in GitHub Desktop.
Use with watchr to reload pages on a remote site periodically to check for changes.
require 'net/http'
require 'mechanize'
class RemoteSiteWatchr
def initialize(host, urls, &block)
@host = host
@urls = urls
@block = block
@agent = Mechanize.new
end
def start
prior_hashes = {}
Thread.new do
while true
@urls.each do |uri|
target = "http://#{@host}#{uri}"
begin
page = @agent.get(target)
hash = Digest::MD5.hexdigest(page.body)
prior_hashes[uri] = hash if !prior_hashes[uri]
if prior_hashes[uri] != hash
prior_hashes[uri] = hash
puts ">>> #{uri} changed, rerunning..."
@block.call
break
end
rescue StandardError => e
puts "[ERROR] #{target}: #{e.message}"
end
end
sleep 5
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment