class Forecast::Adapters::WundergroundAdapter

Public Instance Methods

current(latitude, longitude) click to toggle source
# File lib/forecast/adapters/wunderground_adapter.rb, line 7
def current(latitude, longitude)
  forecast = nil
  result = get_json(get_action('conditions', latitude, longitude))
  if result.has_key?('current_observation')
    forecast = get_current_forecast(result['current_observation'].merge({latitude: latitude, longitude: longitude}))
  end
  return forecast
end
daily(latitude, longitude) click to toggle source
# File lib/forecast/adapters/wunderground_adapter.rb, line 29
def daily(latitude, longitude)
  forecasts = Forecast::Collection.new
  result = get_json(get_action('forecast', latitude, longitude))
  if result.has_key?('forecast')
    items = result['forecast']['simpleforecast']['forecastday']
    items.each do |hash|
      forecast = get_daily_forecast(hash.merge({latitude: latitude, longitude: longitude}))
      forecasts << forecast
     end
  end
  return forecasts
end
hourly(latitude, longitude) click to toggle source
# File lib/forecast/adapters/wunderground_adapter.rb, line 16
def hourly(latitude, longitude)
  forecasts = Forecast::Collection.new
  result = get_json(get_action('hourly', latitude, longitude))
  if result.has_key?('hourly_forecast')
    items = result['hourly_forecast']
    items.each do |hash|
      forecast = get_hourly_forecast(hash.merge({latitude: latitude, longitude: longitude}))
      forecasts << forecast
     end
  end
  return forecasts
end

Private Instance Methods

get_action(action, latitude, longitude) click to toggle source
# File lib/forecast/adapters/wunderground_adapter.rb, line 44
def get_action(action, latitude, longitude)
  url = "http://api.wunderground.com/api/#{options[:api_key]}/#{action}/q/#{latitude},#{longitude}.json"
end
get_current_forecast(hash = {}) click to toggle source
# File lib/forecast/adapters/wunderground_adapter.rb, line 48
def get_current_forecast(hash = {})
  puts 'forecast hash: ' + hash.to_s
  forecast = Forecast.new()
  forecast.latitude = hash[:latitude]
  forecast.longitude = hash[:longitude]
  forecast.time = get_time(hash['observation_epoch'])
  forecast.temperature = get_temperature(hash['temp_f'], :fahrenheit)
  forecast.condition = get_condition(hash['weather'])
  forecast.text = get_text(hash['weather'])
  return forecast
end
get_daily_forecast(hash = {}) click to toggle source
# File lib/forecast/adapters/wunderground_adapter.rb, line 72
def get_daily_forecast(hash = {})
  puts 'forecast hash: ' + hash.to_s
  forecast = Forecast.new()
  forecast.latitude = hash[:latitude]
  forecast.longitude = hash[:longitude]
  forecast.time = get_time(hash['date']['epoch'])
  forecast.temperature_min = get_temperature(hash['low']['fahrenheit'], :fahrenheit)
  forecast.temperature_max = get_temperature(hash['high']['fahrenheit'], :fahrenheit)
  forecast.temperature = get_temperature([hash['low']['fahrenheit'], hash['high']['fahrenheit']], :fahrenheit)
  forecast.condition = get_condition(hash['conditions'])
  forecast.text = get_text(hash['conditions'])
  return forecast
end
get_hourly_forecast(hash = {}) click to toggle source
# File lib/forecast/adapters/wunderground_adapter.rb, line 60
def get_hourly_forecast(hash = {})
  puts 'forecast hash: ' + hash.to_s
  forecast = Forecast.new()
  forecast.latitude = hash[:latitude]
  forecast.longitude = hash[:longitude]
  forecast.time = get_time(hash['FCTTIME']['epoch'])
  forecast.temperature = get_temperature(hash['temp']['english'], :fahrenheit)
  forecast.condition = get_condition([hash['condition']])
  forecast.text = get_text(hash['condition'])
  return forecast
end