Reliable weather data for your next project, and a great way to introduce APIs to new programmers.
YWx provides a Ruby interface to the Yahoo Weather XML RSS feed detailed at developer.yahoo.com/weather.
Weather data is exciting. Its broad in scope, yet very practical in everyday implementation. Most important, it’s a perfect dataset for young programmers to begin interacting with. The Yahoo Weather API is a particularly easy way to start working with outside data.
Log the weather information to your database! Graph it to your heart’s content! Write a widget that emails the weather to your cell phone. There are lots of opportunities to learn and experiment.
Install the gem by executing:
% gem install ywx
Then, apply for a Yahoo GeoPlanet Application ID at developer.yahoo.com/geo/geoplanet/
The application is free, easy and fast.
A simple example program:
require 'ywx' require 'pp' appid = 'INSERT_YAHOO_APPID_HERE' @client = YahooWeather::Client.new(appid) response = @client.lookup_zip(73072) pp "Info" pp response.title pp response.condition.temp pp response.condition.text pp "Conditions" pp response.condition.temp pp response.units.temperature pp response.condition.text pp "Forecast" pp "#{response.forecasts[0].day} - #{response.forecasts[0].text}. High: #{response.forecasts[0].high} Low: #{res pp "#{response.forecasts[1].day} - #{response.forecasts[1].text}. High: #{response.forecasts[1].high} Low: #{res
Produces output as:
"Info" "Conditions for Norman, OK at 4:54 pm CDT" 84 "Mostly Cloudy" "Conditions" 84 "F" "Mostly Cloudy" "Forecast" "Mon - Scattered Thunderstorms. High: 78 Low: 67" "Tue - Partly Cloudy/Wind. High: 85 Low: 69"
There is a variety of detailed weather information in other attributes of the YahooWeather::Response object.
For a full display of all the available data in the request, run the following code
require 'ywx' require 'pp' appid = 'INSERT_YAHOO_APPID_HERE' @client = YahooWeather::Client.new(appid) pp response = @client.lookup_zip(73072)
This produces a readout like the following:
#<YahooWeather::Response:0x007fc55c942090 @astronomy= #<YahooWeather::Astronomy:0x007fc55c93fde0 @sunrise=2012-04-30 06:37:00 -0500, @sunset=2012-04-30 20:14:00 -0500>, @atmosphere= #<YahooWeather::Atmosphere:0x007fc55c91aa90 @barometer="steady", @humidity=58, @pressure=29.88, @visibility=10>, @condition= #<YahooWeather::Condition:0x007fc55c902440 @code=28, @date=2012-04-30 16:54:00 -0500, @temp=84, @text="Mostly Cloudy">, @description= "\n<img src=\"http://l.yimg.com/a/i/us/we/52/28.gif\"/><br />\n<b>Current Conditions:</b><br />\nMostly Cloudy, 84 F<BR />\n<BR /><b>Forecast:</b><BR />\nMon - Scattered Thunderstorms. High: 78 Low: 67<br />\nTue - Partly Cloudy/Wind. High: 85 Low: 69<br />\n<br />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Norman__OK/*http://weather.yahoo.com/forecast/USOK0388_f.html\">Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n", @forecasts= [#<YahooWeather::Forecast:0x007fc55c8e33d8 @code=47, @date=2012-04-30 00:00:00 -0500, @day="Mon", @high=78, @low=67, @text="Scattered Thunderstorms">, #<YahooWeather::Forecast:0x007fc55c8d5a58 @code=24, @date=2012-05-01 00:00:00 -0500, @day="Tue", @high=85, @low=69, @text="Partly Cloudy/Wind">], @image= #<YahooWeather::Image:0x007fc55c9180d8 @height=18, @link="http://weather.yahoo.com", @title="Yahoo! Weather", @url="http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif", @width=142>, @latitude=35.22, @location= #<YahooWeather::Location:0x007fc55c924a40 @city="Norman", @country="United States", @region="OK">, @longitude=-97.51, @page_url= "http://us.rd.yahoo.com/dailynews/rss/weather/Norman__OK/*http://weather.yahoo.com/forecast/USOK0388_f.html", @request_location="12789594", @request_url="http://weather.yahooapis.com/forecastrss?w=12789594&u=f", @title="Conditions for Norman, OK at 4:54 pm CDT", @units= #<YahooWeather::Units:0x007fc55c922d80 @distance="mi", @pressure="in", @speed="mph", @temperature="F">, @wind= #<YahooWeather::Wind:0x007fc55c9207d8 @chill=84, @direction=170, @speed=14>>
The the specific data points are easy to access. For example, if you wanted the forecasted high temperature for tomorrow, use the following line.
response.forecasts[1].high
Or if you wanted to isolate the latitude and longitude of the reading station, simply run the following
response.latitude response.longitude
Its that easy. Start building something.
Thanks, and happy hacking.