Skip to content

Instantly share code, notes, and snippets.

@damien
Created April 2, 2009 21:06
Show Gist options
  • Save damien/89486 to your computer and use it in GitHub Desktop.
Save damien/89486 to your computer and use it in GitHub Desktop.
A simple ruby script that will display the weather for a given zip code using Yahoo's weather API.
require 'net/http'
require 'rexml/document'
puts "Enter your zip code to get today's forecast"
print "Zip: "
zip = gets.chomp
if (zip.nil? or zip == 0)
puts "Invalid zip, program exiting."
exit 1
end
feed = Net::HTTP.get 'weather.yahooapis.com', "/forecastrss?p=#{zip}"
if (feed.nil? or feed == 0)
puts "Yahoo! Weather unavailable, program exiting."
exit 1
end
xml = REXML::Document.new feed
city = xml.elements['//yweather:location'].attributes['city']
region = xml.elements['//yweather:location'].attributes['region']
country = xml.elements['//yweather:location'].attributes['country']
condition = xml.elements['//yweather:condition'].attributes['text']
temprature = xml.elements['//yweather:condition'].attributes['temp']
degrees = xml.elements['//yweather:units'].attributes['temperature']
puts
puts "Forcast for #{city}, #{region} #{country}"
puts "#{condition} at #{temprature}#{degrees}"
puts
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment