Module: WatcherInTheWater

Includes:
Jabber
Defined in:
lib/watcher_in_the_water.rb

Constant Summary collapse

VERSION =
'0.1'

Class Method Summary collapse

Class Method Details

.alert(message) ⇒ Object



51
52
53
54
55
# File 'lib/watcher_in_the_water.rb', line 51

def alert(message)
  connect
  @client.send Message.new(@config['recipient'],
                           message).set_type(:normal).set_id('1')
end

.configure(config = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/watcher_in_the_water.rb', line 16

def configure(config = nil)
  config = File.expand_path(config || '~/.watcher/config.yml')
  FileUtils.cd(File.dirname(config))
  @config = YAML.load(File.read(config))
rescue
  abort "You need #{config} to contain a sender jabber ID, password,
a recipient jabber ID, and a list of URLs in YAML format. Example:

---
jid: [email protected]
password: mellon
recipient: [email protected]
urls: ---
http://rubyconf.org
"
end

.connectObject



57
58
59
60
61
62
63
# File 'lib/watcher_in_the_water.rb', line 57

def connect
  return if @client
  jid = JID.new("#{@config['jid']}/#{`hostname`.chomp}-watcher")
  @client = Client.new(jid)
  @client.connect
  @client.auth(@config['password'])
end

.url_transform(url) ⇒ Object



65
66
67
# File 'lib/watcher_in_the_water.rb', line 65

def url_transform(url)
  url.gsub(/[^\w]/, '_')[0 .. 50] + '_' + Digest::SHA1.hexdigest(url)
end

.watchObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/watcher_in_the_water.rb', line 33

def watch
  @config['urls'].each do |url|
    content = open(url).read
    hash = Digest::SHA1.hexdigest(content)
    filename = 'hashes/' + url_transform(url)

    if File.exist?(filename)
      if File.read(filename) != hash
        alert("#{url} changed to #{content[0 .. 200]}")
      end
    else
      alert("Watching #{url} for you!")
    end
    FileUtils.mkdir_p(File.dirname(filename))
    File.open(filename, 'w') { |f| f.write hash }
  end
end