Hermann, I followed Sebatian's advice to use print instead of puts and
that solved my problem. But I would still like to understand how to
remove the newline characters. Here's my code as it currently stands
(with "rssfile.print line" in place of "rssfile.puts line"), hopefully
it will help you see how I was trying to tackle the problem.
require("rubygems")
require("scrubyt")
require ("open-uri")
require 'time'
require 'date'
psi = Scrubyt::Extractor.define do
fetch("
http://app.nea.gov.sg/psi/")
record("/html/body/div/table/tr/td/table/tbody/tr/td/div",
{ :generalize => true }) do
title("/strong[1]/font[1]")
item("/table/tbody/tr/td/table/tbody/tr", { :generalize => true })
do
region("/td[1]")
psi("/td[7]")
aqd("/td[8]")
end
end
end
f = open("psiregions.xml", File::CREAT|File::TRUNC|File::RDWR) {|f|
psi.to_xml.write(f, 1)
}
# Create the RSS file.
rssfile = File.new("sgpsi.xml", "w")
rssfile.puts('<?xml version="1.0" encoding="UTF-8"?>')
rssfile.puts('<rss version="2.0">')
rssfile.puts(' <channel>')
rssfile.puts(' <link>
http://app.nea.gov.sg/psi/</link>')
rssfile.puts(' <description>Singapore PSI Readings</description>')
#rssfile.puts(' <title>Singapore PSI Readings' + Time.now.rfc2822
+ '</title>')
rssfile.puts(' <lastBuildDate>' + Time.now.rfc2822 + '</
lastBuildDate>')
rssfile.puts(' <webMaster>
[email protected]</webMaster>')
File.open('psiregions.xml', 'r') do |f1|
while line = f1.gets
line=line.strip
line.gsub!(/<root>/, "")
line.gsub!(/<\/root>/, "")
line.gsub!(/<record>/, "")
line.gsub!(/<\/record>/, "")
line.gsub!("24-hr", "Singapore 24-hr")
line.gsub!("<region>Region</region>", "")
line.gsub!("<region>Sulphur Dioxide</region>", "")
line.gsub!(/<region>/, "<title>")
line.gsub!(/<\/region>/,":")
line.gsub!(/<psi>/, " PSI Level ")
line.gsub!(/<\/psi>/, "")
line.gsub!(/<aqd>/, " - ")
line.gsub!(/<\/aqd>/, "</title>")
line.gsub!(/<item>/, "<item><pubDate>" + Time.now.rfc2822 + "</
pubDate>")
line.gsub!(/<\/item>/, "</item>\n")
rssfile.print line
end
end
rssfile.puts('')
rssfile.puts('</channel>')
rssfile.puts('</rss>')
rssfile.close