lib/barometer/weather_services/yahoo.rb in barometer-0.6.7 vs lib/barometer/weather_services/yahoo.rb in barometer-0.7.0
- old
+ new
@@ -25,10 +25,11 @@
# - http://weather.yahooapis.com/forecastrss?p=FRXX0076&u=c
#
# where query can be:
# - zipcode (US)
# - Yahoo! Location ID [actually weather.com id] (International)
+ # - Yahoo! 'Where on Earth ID [WOEID] (International)
#
# = Yahoo! terms of use
# The feeds are provided free of charge for use by individuals and non-profit
# organizations for personal, non-commercial uses. We ask that you provide
# attribution to Yahoo! Weather in connection with your use of the feeds.
@@ -40,21 +41,26 @@
# these RSS feeds. Yahoo! also reserves the right to require you to cease
# distributing these feeds at any time for any reason.
#
# == notes
# - the Yahoo! Location ID is a propreitary number (shared with weather.com)
+ # - the Yahoo! WOEID is only used by Yahoo!, and is a 32-bit number. Unfortunately
+ # this number confilcts with US Zipcodes (ie the zipcode=90210 and the
+ # WOEID=90210 cannot be destinguished and do not mean the same thing). To
+ # solve this, any 5 digit number will be dtected as a ZIPCODE. To have a
+ # 5 digit query be detected as a WOEID, prepend it with a 'w' (ie: w90210).
#
class WeatherService::Yahoo < WeatherService
#########################################################################
# PRIVATE
# If class methods could be private, the remaining methods would be.
#
def self._source_name; :yahoo; end
- def self._accepted_formats; [:zipcode, :weather_id]; end
-
+ def self._accepted_formats; [:zipcode, :weather_id, :woe_id]; end
+
def self._wet_icon_codes
codes = [1] + (3..18).to_a + [35] + (37..43).to_a + (45..47).to_a
codes.collect {|c| c.to_s}
end
def self._sunny_icon_codes
@@ -183,12 +189,17 @@
# use HTTParty to get the current weather
def self._fetch(query, metric=true)
return unless query
puts "fetch yahoo: #{query.q}" if Barometer::debug?
+ options = {
+ :p => query.format == :woe_id ? nil : query.q,
+ :w => query.format == :woe_id ? query.q : nil,
+ :u => (metric ? 'c' : 'f')
+ }.delete_if {|k,v| v.nil? }
self.get(
"http://weather.yahooapis.com/forecastrss",
- :query => {:p => query.q, :u => (metric ? 'c' : 'f')},
+ :query => options,
:format => :xml,
:timeout => Barometer.timeout
)['rss']['channel']
end
\ No newline at end of file