Exercise-1-1
# Exercise: Take this file and add some RSS parsing to it. # Perhaps revealing which items are new since the last time you checked. require 'open-uri' url = 'http://scoops.totallyrule.com/xml/rss/feed.xml' # pick an rss feed to track lastfile = 'lastmod.log' # file to save/retrieve the time we checked # let's check to see when we last hit this feed lastcheck = File.open(lastfile, 'r').read.chomp # now convert the time to a Time object with the correct net standard lastcheck = Time.parse(lastcheck).rfc2822 begin # we're passing in a simple hash to apply our header # If-Modified-Since will compare our time to the modified time of the file # on the server and if it's newer than lastcheck it will give us the feed # otherwise we get nothing and the rescue part kicks in open(url, { 'If-Modified-Since' => lastcheck }) do |feed| puts "The feed has changed.\n" # Try something more interesting here with the rss module # Tutorial: http://www.cozmixng.org/~rwiki/?cmd=view;name=RSS+Parser%3A%3ATutorial.en end rescue OpenURI::HTTPError # the exception open-uri throws on http errors puts "No file available or file has not changed.\n" ensure # save the time of this check # this part happens good or bad File.open(lastfile, 'w') do |f| f.write(Time.now.to_s + "\n") end end