class Forecast::Adapters::YahooAdapter

Constants

URL_RSS
URL_YQL

Public Instance Methods

current(latitude, longitude) click to toggle source
# File lib/forecast/adapters/yahoo_adapter.rb, line 10
def current(latitude, longitude)
  forecast = nil
  doc = get_rss(latitude, longitude)
  if doc
    hash = {}
    doc.elements.each('rss/channel/item/yweather:condition') do |elem|
      elem.attributes.each() do |attr|
        hash[attr[0].to_sym] = attr[1]
      end
    end
    forecast = get_forecast({latitude: latitude, longitude: longitude}.merge(hash))
  end
  return forecast
end
daily(latitude, longitude) click to toggle source
# File lib/forecast/adapters/yahoo_adapter.rb, line 30
def daily(latitude, longitude)
  doc = get_rss(latitude, longitude)
  forecasts = Forecast::Collection.new
  if doc
    doc.elements.each('rss/channel/item/yweather:forecast') do |elem|
      hash = {}
      elem.attributes.each() do |attr|
        hash[attr[0].to_sym] = attr[1]
      end
      forecasts << get_forecast({latitude: latitude, longitude: longitude}.merge(hash))
    end
  end
  return forecasts
end
hourly(latitude, longitude) click to toggle source
# File lib/forecast/adapters/yahoo_adapter.rb, line 25
def hourly(latitude, longitude)
  # not supported
  return []
end

Private Instance Methods

get_forecast(hash) click to toggle source
# File lib/forecast/adapters/yahoo_adapter.rb, line 67
def get_forecast(hash)
  forecast = Forecast.new
  forecast.latitude = hash[:latitude]
  forecast.longitude = hash[:longitude]
  forecast.time = get_time(hash[:date])
  forecast.condition = get_condition(hash[:text])
  forecast.text = get_text(hash[:text])
  forecast.temperature_min = get_temperature(hash[:low])
  forecast.temperature_max = get_temperature(hash[:high])
  forecast.temperature = get_temperature(hash.has_key?(:temp) ? hash[:temp] : [hash[:low], hash[:high]])
  return forecast
end
get_rss(latitude, longitude) click to toggle source
# File lib/forecast/adapters/yahoo_adapter.rb, line 58
def get_rss(latitude, longitude)
  woeid = get_woeid(latitude, longitude)
  if woeid
    doc = Forecast::Utils.get_doc(URL_RSS, {w: woeid})
    return doc
  end
  return nil
end
get_woeid(latitude, longitude) click to toggle source
# File lib/forecast/adapters/yahoo_adapter.rb, line 47
def get_woeid(latitude, longitude)
  woeid = nil
  query = "SELECT * FROM geo.placefinder WHERE text='#{latitude}, #{longitude}' and gflags='R'"
  url = URL_YQL + "?q=" + URI::encode(query)
  doc = get_dom(url)
  doc.elements.each('query/results/Result/woeid') do |elem|
    woeid = elem.text
  end
  return woeid
end