Permalink
Browse files

Finished initial WOEID support.

  • Loading branch information...
1 parent 8eb3560 commit b6d2f68ca4e832d42ddd57ee16d7de8574542a7a Walter Korman committed Dec 26, 2009
Showing with 199 additions and 133 deletions.
  1. +3 −0 CHANGELOG.rdoc
  2. +34 −20 README.rdoc
  3. +10 −7 examples/example.rb
  4. +1 −0 lib/yahoo-weather.rb
  5. +40 −16 lib/yahoo-weather/client.rb
  6. +111 −90 test/test_api.rb
View
@@ -1,5 +1,8 @@
* [shaper] upped library to version 1.2.0 to reflect changes.
+* [shaper] added new client.lookup_by_woeid method which should be
+ used instead of the now-deprecated lookup_location method.
+
* [shaper] use new Yahoo Weather API at
http://weather.yahooapis.com/forecastrss
View
@@ -5,22 +5,33 @@
yahoo-weather provides an object-oriented interface to the Yahoo! Weather XML
RSS feed detailed at http://developer.yahoo.com/weather.
-People care a lot about the weather, which sometimes seems ironic given that
-they can just glance out the window, but we can all understand the fascination
-with details and forecasting.
+People care a lot about the weather. This may seem ironic given they
+can just glance out the window. However, I belive we can all
+understand a fascination with details and forecasting.
-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 every five
-minutes with a link to your friend's PayPal account to deposit money if the
-weather's sunny and you both bet that it would be rainy. And the fun doesn't
-have to stop there.
+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 every five minutes with a link to your friend's PayPal account
+to deposit money if the weather's sunny and you both bet that it would
+be rainy. And the fun doesn't have to stop there.
-The RubyForge project page is at http://rubyforge.org/projects/yahoo-weather.
+The RubyForge project page for this library is at
+http://rubyforge.org/projects/yahoo-weather.
Source code is at http://github.com/shaper/yahoo-weather.
-== Installation
+NOTE: This library was updated as of December 2009 to use a new
+WOEID-based lookup interface. Yahoo has deprecated the older
+non-WOEID-based lookup API. The archived page with the deprecated API
+details is at:
+
+http://developer.yahoo.com/weather/archive.html
+The older interface is still operational but users of this library
+should shift from using YahooWeather::Client::lookup_location to
+YahooWeather::Client::lookup_by_woeid.
+
+== Installation
Install +rubygems+ if you don't already have it. See http://rubyforge.org/projects/rubygems/.
@@ -36,7 +47,10 @@ A simple example program:
require 'yahoo-weather'
@client = YahooWeather::Client.new
- response = @client.lookup_location('98103')
+ # look up WOEID via http://weather.yahoo.com; enter location by city
+ # name or zip and WOEID is at end of resulting page url. herein we use
+ # the WOEID for Santa Monica, CA.
+ response = @client.lookup_by_woeid(2488892)
# straight text output
print <<edoc
@@ -60,17 +74,17 @@ A simple example program:
Produces output as:
- Conditions for Seattle, WA at 2:53 pm PST
- 55 degrees F
- Light Rain
+ Conditions for Santa Monica, CA at 10:51 am PST
+ 56 degrees
+ Partly Cloudy
<div>
- <img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/11.gif"><br/>
- 55 degrees F<br/>
- Light Rain<br>
+ <img src="http://l.yimg.com/a/i/us/nws/th/main_142b.gif"><br/>
+ 56 degrees F<br/>
+ Partly Cloudy<br>
Forecast:<br/>
- Sun - Rain/Wind. High: 56 Low: 54<br/>
- Mon - Heavy Rain/Wind. High: 60 Low: 54<br/>
- More information <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Seattle__WA/*http://xml.weather.yahoo.com/forecast/98103_f.html">here</a>.
+ Sat - Mostly Cloudy. High: 59 Low: 46<br/>
+ Sun - Partly Cloudy. High: 64 Low: 48<br/>
+ More information <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Santa_Monica__CA/*http://weather.yahoo.com/forecast/USCA1024_f.html">here</a>.
</div>
There is a variety of detailed weather information in other attributes of the
View
@@ -4,17 +4,20 @@
require 'yahoo-weather'
@client = YahooWeather::Client.new
-response = @client.lookup_location('98103')
+# look up WOEID via http://weather.yahoo.com; enter location by city
+# name or zip and WOEID is at end of resulting page url. herein we use
+# the WOEID for Santa Monica, CA.
+response = @client.lookup_by_woeid(2488892)
# straight text output
-print <<edoc
+print <<EDOC
#{response.title}
-#{response.condition.temp} degrees #{response.units.temperature}
+#{response.condition.temp} degrees
#{response.condition.text}
-edoc
+EDOC
-# html output
-print <<edoc
+# sample html output
+print <<EDOC
<div>
<img src="#{response.image.url}"><br/>
#{response.condition.temp} degrees #{response.units.temperature}<br/>
@@ -24,4 +27,4 @@
#{response.forecasts[1].day} - #{response.forecasts[1].text}. High: #{response.forecasts[1].high} Low: #{response.forecasts[1].low}<br/>
More information <a href="#{response.page_url}">here</a>.
</div>
-edoc
+EDOC
@@ -16,6 +16,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
require 'net/http'
+require 'cgi'
require 'time'
require 'nokogiri'
@@ -10,6 +10,32 @@ def initialize (api_url = @@API_URL)
# Returns a YahooWeather::Response object detailing the current weather
# information for the specified location.
#
+ # The lookup requires the unique WOEID for the location whose
+ # weather is sought.. To find your WOEID, browse or search for your
+ # city from the Weather (http://weather.yahoo.com/) home page. The
+ # WOEID is in the URL for the forecast page for that city. You can
+ # also get the WOEID by entering your zip code on the home page. For
+ # example, if you search for Los Angeles on the Weather home page,
+ # the forecast page for that city is
+ # http://weather.yahoo.com/united-states/california/los-angeles-2442047/. The
+ # WOEID is 2442047.
+ #
+ # +units+ allows specifying whether to retrieve information in
+ # +Fahrenheit+ as +YahooWeather::Units::FAHRENHEIT+, or +Celsius+ as
+ # +YahooWeather::Units::CELSIUS+, and defaults to fahrenheit.
+ #
+ def lookup_by_woeid (woeid, units = 'f')
+ url = @api_url + '?w=' + CGI.escape(woeid.to_s) + '&u=' + CGI.escape(units)
+ _lookup(woeid, url)
+ end
+
+ # Returns a YahooWeather::Response object detailing the current weather
+ # information for the specified location.
+ #
+ # NOTE: This method is deprecated as Yahoo has deprecated this
+ # non-WOEID-based lookup function. Please use the new
+ # +lookup_by_woeid+ method instead.
+ #
# +location+ can be either a US zip code or a location code. Location
# codes can be looked up at http://weather.yahoo.com, where it will appear
# in the URL that results from searching on the city or zip code. For
@@ -21,26 +47,24 @@ def initialize (api_url = @@API_URL)
# +YahooWeather::Units::CELSIUS+, and defaults to fahrenheit.
#
def lookup_location (location, units = 'f')
- # query the service to grab the xml data
- url = _request_url(location, units)
- begin
- response = Net::HTTP.get_response(URI.parse(url)).body.to_s
-
- rescue
- raise "failed to get weather via '#{url}': " + $!
- end
-
- # create the response object
- doc = Nokogiri::XML.parse(response)
- YahooWeather::Response.new(location, url, doc)
+ url = @api_url + '?p=' + CGI.escape(location) + '&u=' + CGI.escape(units)
+ _lookup(location, url)
end
private
- def _request_url (location, units)
- @api_url + '?p=' + URI.encode(location) + '&u=' + URI.encode(units)
-# TODO: for WOEID lookup, use 'w' rather than 'p' for query param
-# @api_url + '?w=' + URI.encode( location ) + '&u=' + URI.encode( units )
+ def _lookup (src, url)
+ begin
+ response = Net::HTTP.get_response(URI.parse(url)).body.to_s
+
+ rescue => e
+ raise RuntimeError.new("failed to get weather [src=#{src}, " +
+ "url=#{url}, e=#{e}].")
+ end
+
+ # create the response object
+ doc = Nokogiri::XML.parse(response)
+ YahooWeather::Response.new(src, url, doc)
end
end
Oops, something went wrong.

0 comments on commit b6d2f68

Please sign in to comment.