Skip to content

Instantly share code, notes, and snippets.

@liuxd
Last active September 7, 2017 01:33
Show Gist options
  • Save liuxd/0f9442f7eb9ed6fc8e72c1a4a28d37e5 to your computer and use it in GitHub Desktop.
Save liuxd/0f9442f7eb9ed6fc8e72c1a4a28d37e5 to your computer and use it in GitHub Desktop.
[currency_rate_monitor.rb] Monitoring NZD rate and notify you while the rate drops down the limit you set.
#!/usr/bin/env ruby
require 'net/http'
require 'nokogiri'
# Get the rate data from boc page.
def getRate
url = 'http://www.boc.cn/sourcedb/whpj/'
data = Nokogiri::HTML.parse(Net::HTTP.get(URI(url)))
nz_line = data.css('tr')[18].css('td')
rate = nz_line[4].text
end
# Notify in the iterm2.
def notify rate, limit = 460
if rate.to_f < limit
title = "It is the time!"
message = "Rate : " + rate.to_s
exec 'noti -t ' + title + ' -m ' + message
end
end
# Record message.
def log message
f = File.new "output.log", 'a+'
f.write message + "\n"
f.close
end
# Let's go!
loop do
rate = getRate
notify rate
log rate
sleep 240
end
# end of this file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment