Module: WatcherInTheWater
- Includes:
- Jabber
- Defined in:
- lib/watcher_in_the_water.rb
Constant Summary collapse
- VERSION =
'0.1'
Class Method Summary collapse
- .alert(message) ⇒ Object
- .configure(config = nil) ⇒ Object
- .connect ⇒ Object
- .url_transform(url) ⇒ Object
- .watch ⇒ Object
Class Method Details
.alert(message) ⇒ Object
51 52 53 54 55 |
# File 'lib/watcher_in_the_water.rb', line 51 def alert() connect @client.send Message.new(@config['recipient'], ).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.(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 |
.connect ⇒ Object
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 |
.watch ⇒ Object
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 |