Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
[SOLUTION] Current Temperature (#68
email55555 email55555
2/27/2006 9:24:00 PM
require 'uri'
require 'open-uri'
require 'rexml/document'
class Weather
attr_reader :location, :temperature, :unit
def initialize(zip_or_city, unit='f')
raise "Error: Unit must be 'C' or 'F'." unless unit =~ /^[cf]$/i
id = get_id(zip_or_city)
url = "
http://xml.weather.yahoo.com/for...
{id}_#{unit.downcase}.xml"
xml = open(url) { |f| f.read }
doc = REXML::Document.new(xml)
@temperature =
doc.elements['/rss/channel/item/yweather:condition/@temp'].to_s.to_i
@unit = unit.upcase
end
private
def get_id(location)
location = URI.escape(location)
url = "
http://xoap.weather.com/search/sear...
{location}"
xml = open(url) { |f| f.read }
doc = REXML::Document.new(xml)
locations = doc.elements.to_a("/search/loc")
raise "Cannot find the location." if locations.size <= 0
# raise "Please more specific:\n#{locations.map {|e|
e.text}*"\n"}" if locations.size > 1
@location = locations[0].text.sub(/\s*\(\d+\)\s*$/, '')
locations[0].attributes['id']
end
end
if __FILE__ == $0
if ARGV.size <= 0 || (ARGV[1] && ARGV[1] !~ /^[cf]$/i)
puts "Usage: #$0 city_or_zip_code [c|f]"
exit(1)
end
begin
w = Weather.new(ARGV[0], ARGV[1] || 'f')
puts "The temperature in #{w.location} is #{w.temperature} degress
#{w.unit}."
rescue
puts "Information for #{ARGV[0]} was not found or unavailable."
end
end
__END__
www.doublegifts.com
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
[SOLUTION] Current Temperature (#68
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password