Self-Modifying Ruby Script
There’s a county road between my house and my church that I like to use to avoid going through town and several traffic lights. Going the “back way” will cause me to drive over some frequently-used railroad tracks, and over a one-lane bridge that spans a large creek. So I may have to wait for a train, but they’re short, and it’s only happened a couple times in the six years I’ve been living at this house. However, the bridge has a large, low section of road in front of it that frequently washes out when it rains hard north of us.
As of a couple years ago, the local law enforcement would just put up some old-style construction signs to “close the road.” Being as I was driving a Durango, I’d usually drive through the barrier, and cross the water anyway. The last time I tried this, I passed the sheriff coming out of the road. He turned around and floored it, and caught up with me right at the water. I said I just wanted to see how high it was, which was true enough, because the water was really high that time. He told me he could throw me in jail for ignoring the signs. Thankfully, he didn’t even ticket me.
Since then, they’ve installed huge blockades which really do close off the road. They have signs at either end of the road to indicate that the barriers are in place (after all of the local-traffic cross-streets), but, being as this is a short trip, I’m about half-way to my destination before I ever see these signs.
So I looked, and I think I’ve found a web page for our local radio station that shows the status of road closings in the county. I wrote up a script that scrapes this page, and texts me if the road has closed, and when it opens back up. It pulls together several interesting pieces, and I made it self-modifying, to keep track of the status of the road. You’ll need to change the return address and make sure you have the right cell phone text-to-email address. I fully expect that this could be much improved, but it seems to work for the moment. I just let it run through cron every hour.
#!/usr/bin/env ruby
closed = 1
require 'net/http'
require 'net/smtp'
require 'fileutils'
def send_mail(status)
Net::SMTP.start('exeter', 25) do |smtp|
smtp.open_message_stream('you.need@real.address.com', 'XXXXXXXXXX@txt.att.net') do |f|
# Other lines could go here if you wanted to send a
# real email or something.
f.puts "Road 400N is #{status}."
end
end
end
url = URI.parse('http://wcsi.whiterivernews.com/6_7_08/localnews.htm')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
if res.body.index("Tinkey") != nil && closed == 0
status = "CLOSED"
closed = 1
send_mail(status)
elsif res.body.index("Tinkey") == nil && closed == 1
status = "OPEN"
closed = 0
send_mail(status)
end
f1 = File.open('check_road.rb', 'r')
f2 = File.open('backup.rb', 'w')
f2.write "#!/usr/bin/env ruby\n"
f2.write "\n"
f2.write "closed = #{closed}\n"
f2.write "\n"
counter = 0
while (line = f1.gets)
if counter >= 4
f2.write line
end
counter += 1
end
FileUtils.move 'backup.rb', 'check_road.rb'