livedoorお天気から東京の天気を取る

課題用
http://shokai.ok.sfc.keio.ac.jp/test/ldweather/

#!/usr/bin/ruby
require 'cgi'
require 'open-uri'
require 'rexml/document'

print "Content-Type: text/plain\n\n"

#get xml
url = 'http://weather.livedoor.com/forecast/rss/3.xml';
doc = REXML::Document.new(open(url).read)

telop = ''

#parse xml
REXML::XPath.each(doc, '//item'){ |item|
  title = REXML::XPath.first(item,'title').text
  if title =~ /東京/
    telop = REXML::XPath.first(item,'//telop').text
  end
}

weather = 'n'

#天気判定
if telop =~ //
  weather = 'r' # rainy
elsif telop =~ //
  weather = 'c' # cloudy
else
  weather = 's' # shiny
end

puts weather # 天気1文字表示