module ForecastIo::Forecasts
Public Instance Methods
ansi_humidity_forecast(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 25 def ansi_humidity_forecast(forecast) do_the_humidity_thing(forecast, ansi_chars, 'humidity') #, 'probability', get_rain_range_colors) end
ansi_rain_forecast(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 12 def ansi_rain_forecast(forecast) (str, precip_type) = do_the_rain_chance_thing(forecast, ansi_chars, 'precipProbability') #, 'probability', get_rain_range_colors) max = get_max_by_data_key(forecast, 'minutely', 'precipProbability') agg = get_max_by_data_key(forecast, 'minutely', 'precipIntensity') "1hr #{precip_type} probability #{(Time.now).strftime('%H:%M').to_s}|#{str}|#{(Time.now + 3600).strftime('%H:%M').to_s} max #{(max.to_f * 100).round(2)}%, #{get_accumulation agg} accumulation" end
ansi_rain_intensity_forecast(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 19 def ansi_rain_intensity_forecast(forecast) (str, precip_type) = do_the_rain_intensity_thing(forecast, ansi_chars, 'precipIntensity') #, 'probability', get_rain_range_colors) agg = get_max_by_data_key(forecast, 'minutely', 'precipIntensity') "1hr #{precip_type} intensity #{(Time.now).strftime('%H:%M').to_s}|#{str}|#{(Time.now + 3600).strftime('%H:%M').to_s}, #{get_accumulation agg} accumulation" end
ansi_temp_apparent_forecast(forecast, hours = 24)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 151 def ansi_temp_apparent_forecast(forecast, hours = 24) str, temperature_data = do_the_temp_thing(forecast, 'apparentTemperature', ansi_chars, hours) "#{hours} hr apparent temps: #{get_temperature temperature_data.first.round(1)} |#{str}| #{get_temperature temperature_data.last.round(1)} Range: #{get_temperature temperature_data.min.round(1)} - #{get_temperature temperature_data.max.round(1)}" end
ansi_temp_forecast(forecast, hours = 24)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 143 def ansi_temp_forecast(forecast, hours = 24) str, temperature_data = do_the_temp_thing(forecast, 'temperature', ansi_chars, hours) resp = "#{hours} hr temps: #{get_temperature temperature_data.first.round(1)} " resp += "(feels like #{get_temperature get_current_apparent_temp(forecast)}) |#{str}| " resp += "#{get_temperature temperature_data.last.round(1)} Range: " resp += "#{get_temperature temperature_data.min.round(1)} - #{get_temperature temperature_data.max.round(1)}" end
ansi_wind_direction_forecast(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 221 def ansi_wind_direction_forecast(forecast) str, wind_speed, wind_gust = do_the_wind_direction_thing(forecast, ansi_wind_arrows) "48h wind direction #{get_speed wind_speed.first}|#{str}|#{get_speed wind_speed.last} Range: #{get_speed(wind_speed.min)} - #{get_speed(wind_speed.max)}, gusting to #{get_speed wind_gust.max}" end
ansi_windchill_forecast(forecast, hours = 24)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 156 def ansi_windchill_forecast(forecast, hours = 24) str, temperature_data = do_the_windchill_temp_thing(forecast, ansi_chars, hours) "#{hours} hr windchill temps: #{get_temperature temperature_data.first.round(1)} |#{str}| #{get_temperature temperature_data.last.round(1)} Range: #{get_temperature temperature_data.min.round(1)} - #{get_temperature temperature_data.max.round(1)}" end
ascii_rain_forecast(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 5 def ascii_rain_forecast(forecast) (str, precip_type) = do_the_rain_chance_thing(forecast, ascii_chars, 'precipProbability') max = get_max_by_data_key(forecast, 'minutely', 'precipProbability') agg = get_max_by_data_key(forecast, 'minutely', 'precipIntensity') "1hr #{precip_type} probability #{(Time.now).strftime('%H:%M').to_s}|#{str}|#{(Time.now + 3600).strftime('%H:%M').to_s} max #{(max.to_f * 100).round(2)}%, #{get_accumulation agg} accumulation" end
ascii_temp_forecast(forecast, hours = 24)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 161 def ascii_temp_forecast(forecast, hours = 24) str, temperature_data = do_the_temp_thing(forecast, 'temperature', ascii_chars, hours) resp = "#{hours} hr temps: #{get_temperature temperature_data.first.round(1)} " resp += "(feels like #{get_temperature get_current_apparent_temp(forecast)}) " resp += "|#{str}| #{get_temperature temperature_data.last.round(1)} Range: " resp += "#{get_temperature temperature_data.min.round(1)} - #{get_temperature temperature_data.max.round(1)}" end
ascii_wind_direction_forecast(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 226 def ascii_wind_direction_forecast(forecast) str, wind_speed, wind_gust = do_the_wind_direction_thing(forecast, ascii_wind_arrows) "48h wind direction #{get_speed wind_speed.first}|#{str}|#{get_speed wind_speed.last} Range: #{get_speed(wind_speed.min)} - #{get_speed(wind_speed.max)}, gusting to #{get_speed wind_gust.max}" end
calculate_windchill(temp_c, wind)
click to toggle source
Temp must be F.
# File lib/lita/handlers/forecasts.rb, line 215 def calculate_windchill(temp_c, wind) #temp_f = fahrenheit(temp_c) #35.74 + (0.6215 * temp_f) - (35.75 * wind ** 0.16) + (0.4275 * temp_f * wind ** 0.16) 13.12 + (0.6215 * temp_c) - (11.37 * (wind ** 0.16)) + (0.3965 * (temp_c * (wind ** 0.16))) end
conditions(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 355 def conditions(forecast) temp_str, temps = do_the_temp_thing(forecast, 'temperature', ansi_chars, 8) wind_str, winds = do_the_wind_direction_thing(forecast, ansi_wind_arrows, 8) rain_str, rains = do_the_rain_chance_thing(forecast, ansi_chars, 'precipProbability', config.colors, 15) sun_chance = ((1 - forecast['daily']['data'][0]['cloudCover']) * 100).round "#{get_temperature temps.first.round(2)} |#{temp_str}| #{get_temperature temps.last.round(2)} "\ "/ #{get_speed(winds.first)} |#{wind_str}| #{get_speed(winds.last)} "\ "/ #{sun_chance}% chance of sun / 60m precip |#{rain_str}|" end
do_the_cloud_thing(forecast, chars)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 300 def do_the_cloud_thing(forecast, chars) # O ◎ ] data = forecast['hourly']['data'].slice(0,23) max = 0 min = 1 data.each do |datum| if datum['cloudCover'] > max max = datum['cloudCover'] end if datum['cloudCover'] < min min = datum['cloudCover'] end end str = get_dot_str(chars, data, 0, 1, 'cloudCover') "24h cloud cover |#{str}| range #{min * 100}% - #{max * 100}%" end
do_the_daily_humidity_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 464 def do_the_daily_humidity_thing(forecast) humidities = [] data = forecast['daily']['data'] data.each do |day| humidities.push day['humidity'] end str = get_dot_str(ansi_chars, data, 0, 1, 'humidity') if config.colors str = get_colored_string(data, 'humidity', str, get_wind_range_colors) end "7day humidity #{get_humidity humidities.first}|#{str}|#{get_humidity humidities.last} "\ "range #{get_humidity humidities.min}-#{get_humidity humidities.max}" end
do_the_daily_pressure_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 505 def do_the_daily_pressure_thing(forecast) # O ◎ ] data = forecast['daily']['data'] key = 'pressure' boiled_data = [] data.each do |d| boiled_data.push d[key] end str = get_dot_str(ansi_chars, data, boiled_data.min, boiled_data.max - boiled_data.min, key) "pressure #{data.first[key]} hPa |#{str}| #{data.last[key]} hPa range: #{boiled_data.min}-#{boiled_data.max} hPa [8 day forecast]" end
do_the_daily_rain_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 415 def do_the_daily_rain_thing(forecast) precip_type = 'rain' rains = [] data = forecast['hourly']['data'] data.each do |day| if day['precipType'] == 'snow' precip_type = 'snow' end rains.push day['precipProbability'] end str = get_dot_str(ansi_chars, data, 0, 1, 'precipProbability') if 'snow' == precip_type data.each_with_index do |datum, index| if datum['precipType'] == 'snow' str[index] = get_snowman config end end end if config.colors str = get_colored_string(data, 'precipProbability', str, get_rain_range_colors) end max = get_max_by_data_key(forecast, 'hourly', 'precipProbability') agg = get_aggregate_by_data_key(forecast, 'hourly', 'precipIntensity') "48 hr #{precip_type}s |#{str}| max #{(max.to_f * 100).round}%, #{get_accumulation agg} accumulation" end
do_the_daily_sun_thing(forecast, chars)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 277 def do_the_daily_sun_thing(forecast, chars) key = 'cloudCover' data_points = [] data = forecast['daily']['data'] data.each do |datum| data_points.push (1 - datum[key]).to_f # It's a cloud cover percentage, so let's inverse it to give us sun cover. datum[key] = (1 - datum[key]).to_f # Mod the source data for the get_dot_str call below. end differential = data_points.max - data_points.min str = get_dot_str(chars, data, data_points.min, differential, key) if config.colors str = get_colored_string(data, key, str, get_sun_range_colors) end max = 1 - get_min_by_data_key(forecast, 'daily', key) "8 day sun forecast |#{str}| max #{(max * 100).to_i}%" end
do_the_daily_wind_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 447 def do_the_daily_wind_thing(forecast) winds = [] data = forecast['daily']['data'] data.each do |day| winds.push day['windSpeed'] end str = get_dot_str(ansi_chars, data, 0, winds.max, 'windSpeed') if config.colors str = get_colored_string(data, 'windSpeed', str, get_wind_range_colors) end "7day winds #{get_speed winds.first}|#{str}|#{get_speed winds.last} range #{get_speed winds.min}-#{get_speed winds.max}" end
do_the_fog_thing(forecast, chars)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 319 def do_the_fog_thing(forecast, chars) key = 'visibility' data_points = [] data = forecast['hourly']['data'].slice(0,23) cap = 16 max = 0 min = 16 data.each do |datum| datum[key] = 16 if datum[key] > 16 max = datum[key] if datum[key] > max min = datum[key] if datum[key] < min data_points.push (cap - datum[key]).to_f # It's a visibility number, so let's inverse it to give us fog. datum[key] = (cap - datum[key]).to_f # Mod the source data for the get_dot_str call below. end #differential = data_points.max - data_points.min str = get_dot_str(chars, data, 0, cap, key) "24h fog report |#{str}| visibility #{get_distance min, @scale} - #{get_distance max, @scale}" end
do_the_humidity_thing(forecast, chars, key)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 127 def do_the_humidity_thing(forecast, chars, key) #, type, range_colors = nil) data_points = [] data = forecast['hourly']['data'] data.each do |datum| data_points.push datum[key] end str = get_dot_str(chars, data, 0, 1, key) if config.colors str = get_colored_string(data, key, str, get_humidity_range_colors) end "#{get_humidity data_points.first}|#{str}|#{get_humidity data_points.last} range: #{get_humidity data_points.min}-#{get_humidity data_points.max}" end
do_the_nearest_storm_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 529 def do_the_nearest_storm_thing(forecast) return forecast['currently']['nearestStormDistance'], forecast['currently']['nearestStormBearing'] end
do_the_ozone_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 482 def do_the_ozone_thing(forecast) # O ◎ ] data = forecast['hourly']['data'] str = get_dot_str(ozone_chars, data, 280, 350-280, 'ozone') "ozones #{data.first['ozone']} |#{str}| #{data.last['ozone']} [24h forecast]" end
do_the_pressure_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 491 def do_the_pressure_thing(forecast) data = forecast['hourly']['data'] key = 'pressure' boiled_data = [] data.each do |d| boiled_data.push d[key] end str = get_dot_str(ansi_chars, data, boiled_data.min, boiled_data.max - boiled_data.min, key) "pressure #{data.first[key]} hPa |#{str}| #{data.last[key]} hPa range: #{boiled_data.min}-#{boiled_data.max} hPa [48h forecast]" end
do_the_rain_chance_thing(forecast, chars, key, use_color = config.colors, minute_limit = nil)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 60 def do_the_rain_chance_thing(forecast, chars, key, use_color = config.colors, minute_limit = nil) if forecast['minutely'].nil? return 'No minute-by-minute data available.' end i_can_has_snow = false data_points = [] data = forecast['minutely']['data'] data.each do |datum| data_points.push datum[key] if datum['precipType'] == 'snow' i_can_has_snow = true end end if minute_limit data = condense_data(data, minute_limit) end str = get_dot_str(chars, data, 0, 1, key) if i_can_has_snow data.each_with_index do |datum, index| if datum['precipType'] == 'snow' str[index] = get_snowman config end end end if use_color str = get_colored_string(data, key, str, get_rain_range_colors) end precip_type = i_can_has_snow ? 'snow' : 'rain' return str, precip_type end
do_the_rain_intensity_thing(forecast, chars, key)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 99 def do_the_rain_intensity_thing(forecast, chars, key) #, type, range_colors = nil) if forecast['minutely'].nil? return 'No minute-by-minute data available.' # The "Middle of Nowhere" case. end i_can_has_snow = false data_points = [] data = forecast['minutely']['data'] data.each do |datum| data_points.push datum[key] if datum['precipType'] == 'snow' i_can_has_snow = true end end # Fixed range graph- 0-0.11. str = get_dot_str(chars, data, 0, 0.11, key) if config.colors str = get_colored_string(data, key, str, get_rain_intensity_range_colors) end precip_type = i_can_has_snow ? 'snow' : 'rain' return str, precip_type end
do_the_seven_day_rain_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 392 def do_the_seven_day_rain_thing(forecast) precip_type = 'rain' rains = [] data = forecast['daily']['data'] data.each do |day| if day['precipType'] == 'snow' precip_type = 'snow' end rains.push day['precipProbability'] end str = get_dot_str(ansi_chars, data, 0, 1, 'precipProbability') if config.colors str = get_colored_string(data, 'precipProbability', str, get_rain_range_colors) end max = get_max_by_data_key(forecast, 'daily', 'precipProbability') "7day #{precip_type}s |#{str}| max #{max * 100}%" end
do_the_seven_day_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 366 def do_the_seven_day_thing(forecast) mintemps = [] maxtemps = [] data = forecast['daily']['data'] data.each do |day| mintemps.push day['temperatureMin'] maxtemps.push day['temperatureMax'] end differential = maxtemps.max - maxtemps.min max_str = get_dot_str(ansi_chars, data, maxtemps.min, differential, 'temperatureMax') differential = mintemps.max - mintemps.min min_str = get_dot_str(ansi_chars, data, mintemps.min, differential, 'temperatureMin') if config.colors max_str = get_colored_string(data, 'temperatureMax', max_str, get_temp_range_colors) min_str = get_colored_string(data, 'temperatureMin', min_str, get_temp_range_colors) end "7day high/low temps #{get_temperature maxtemps.first.to_f.round(1)} |#{max_str}| #{get_temperature maxtemps.last.to_f.round(1)} "\ "/ #{get_temperature mintemps.first.to_f.round(1)} |#{min_str}| #{get_temperature mintemps.last.to_f.round(1)} "\ "Range: #{get_temperature mintemps.min} - #{get_temperature maxtemps.max}" end
do_the_sun_thing(forecast, chars)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 253 def do_the_sun_thing(forecast, chars) key = 'cloudCover' data_points = [] data = forecast['hourly']['data'] sun_mod_data = [] data.each do |datum| data_points.push (1 - datum[key]).to_f # It's a cloud cover percentage, so let's inverse it to give us sun cover. sun_mod_data << {key => (1 - datum[key]).to_f} # Mod the source data for the get_dot_str call below. end differential = data_points.max - data_points.min str = get_dot_str(chars, sun_mod_data, data_points.min, differential, key) if config.colors str = get_colored_string(sun_mod_data, key, str, get_sun_range_colors) end max = 1 - get_min_by_data_key(forecast, 'hourly', key) "48hr sun forecast |#{str}| max #{(max * 100).to_i}%" end
do_the_sunrise_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 345 def do_the_sunrise_thing(forecast) t = Time.at(fix_time(forecast['daily']['data'][0]['sunriseTime'], forecast['offset'])) t.strftime("%H:%M:%S") end
do_the_sunset_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 350 def do_the_sunset_thing(forecast) t = Time.at(fix_time(forecast['daily']['data'][0]['sunsetTime'], forecast['offset'])) t.strftime("%H:%M:%S") end
do_the_temp_thing(forecast, key, chars, hours)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 169 def do_the_temp_thing(forecast, key, chars, hours) temps = [] data = forecast['hourly']['data'].slice(0,hours - 1) data.each_with_index do |datum, index| temps.push datum[key] break if index == hours - 1 # We only want (hours) 24hrs of data. end differential = temps.max - temps.min # Hmm. There's a better way. dot_str = get_dot_str(chars, data, temps.min, differential, key) if config.colors dot_str = get_colored_string(data, key, dot_str, get_temp_range_colors) end return dot_str, temps end
do_the_today_thing(forecast, yesterday)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 533 def do_the_today_thing(forecast, yesterday) Lita.logger.info "Basing today on today - yesterday: #{yesterday['daily']['data'][0]['temperatureMax']} - #{forecast['daily']['data'][0]['temperatureMax']}" temp_diff = yesterday['daily']['data'][0]['temperatureMax'] - forecast['daily']['data'][0]['temperatureMax'] get_daily_comparison_text(temp_diff, forecast['daily']['data'][0]['temperatureMax']) end
do_the_tomorrow_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 539 def do_the_tomorrow_thing(forecast) Lita.logger.info "Basing tomorrow on today - tomorrow: #{forecast['daily']['data'][0]['temperatureMax']} - #{forecast['daily']['data'][1]['temperatureMax']}" temp_diff = forecast['daily']['data'][0]['temperatureMax'] - forecast['daily']['data'][1]['temperatureMax'] get_daily_comparison_text(temp_diff, forecast['daily']['data'][0]['temperatureMax']) end
do_the_uvindex_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 671 def do_the_uvindex_thing(forecast) uvs = [] forecast['hourly']['data'].each do |hour| uvs.push hour['uvIndex'] end data = forecast['hourly']['data'] str = get_dot_str(ansi_chars, data, uvs.min, uvs.max - uvs.min, key = 'uvIndex') if config.colors str = get_colored_string(data, 'uvIndex', str, get_uvindex_colors) end "#{uvs.first} |#{str}| #{uvs.last} max: #{uvs.max}" end
do_the_wind_direction_thing(forecast, wind_arrows, hours = 48)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 231 def do_the_wind_direction_thing(forecast, wind_arrows, hours = 48) key = 'windBearing' data = forecast['hourly']['data'].slice(0,hours - 1) str = '' data_points = [] gust_data = [] data.each_with_index do |datum, index| wind_arrow_index = get_cardinal_direction_from_bearing(datum[key]) str << wind_arrows[wind_arrow_index].to_s data_points.push datum['windSpeed'] gust_data.push datum['windGust'] break if index == hours - 1 # We only want (hours) of data. end if config.colors str = get_colored_string(data, 'windSpeed', str, get_wind_range_colors) end return str, data_points, gust_data end
do_the_windchill_temp_thing(forecast, chars, hours)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 190 def do_the_windchill_temp_thing(forecast, chars, hours) temps = [] wind = [] data = forecast['hourly']['data'].slice(0,hours - 1) key = 'temperature' wind_key = 'windSpeed' data.each_with_index do |datum, index| temps.push calculate_windchill(datum[key], datum[wind_key]) break if index == hours - 1 # We only want (hours) 24hrs of data. end differential = temps.max - temps.min # Hmm. There's a better way. dot_str = get_dot_str(chars, data, temps.min, differential, key) if config.colors dot_str = get_colored_string(data, key, dot_str, get_temp_range_colors) end return dot_str, temps end
do_the_windows_data_thing(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 616 def do_the_windows_data_thing(forecast) time_to_close_the_windows = nil time_to_open_the_windows = nil window_close_temp = 0 high_temp = 0 last_temp = 0 forecast['hourly']['data'].each_with_index do |hour, index| if hour['temperature'] > high_temp high_temp = hour['temperature'].to_i end if !time_to_close_the_windows and hour['temperature'].to_i >= 71 if index == 0 time_to_close_the_windows = 'now' else time_to_close_the_windows = hour['time'] end window_close_temp = hour['temperature'] end if !time_to_open_the_windows and time_to_close_the_windows and hour['temperature'] < last_temp and hour['temperature'].to_i <= 75 time_to_open_the_windows = hour['time'] end last_temp = hour['temperature'] break if index > 18 end # Return some meta here and let the caller decide the text. if time_to_close_the_windows.nil? "Leave 'em open, no excess heat today(#{get_temperature high_temp})." else # Todo: base timezone on requested location. timezone = TZInfo::Timezone.get('America/Los_Angeles') if time_to_close_the_windows == 'now' output = "Close the windows now! It is #{get_temperature window_close_temp}. " else time_at = Time.at(time_to_close_the_windows).to_datetime local_time = timezone.utc_to_local(time_at) output = "Close the windows at #{local_time.strftime('%k:%M')}, it will be #{get_temperature window_close_temp}. " end if time_to_open_the_windows open_time = timezone.utc_to_local(Time.at(time_to_open_the_windows).to_datetime) output += "Open them back up at #{open_time.strftime('%k:%M')}. " end output += "The high today will be #{get_temperature high_temp}." datas = { 'timeToClose': local_time.strftime('%k:%M'), 'timeToOpen': open_time.strftime('%k:%M'), 'tempMax': high_temp, 'temp': window_close_temp } end end
do_the_windows_thing(forecast)
click to toggle source
Check for the time of day when it will hit 72F.
# File lib/lita/handlers/forecasts.rb, line 561 def do_the_windows_thing(forecast) time_to_close_the_windows = nil time_to_open_the_windows = nil window_close_temp = 0 high_temp = 0 last_temp = 0 forecast['hourly']['data'].each_with_index do |hour, index| if hour['temperature'] > high_temp high_temp = hour['temperature'].to_i end if !time_to_close_the_windows and hour['temperature'].to_i >= 71 if index == 0 time_to_close_the_windows = 'now' else time_to_close_the_windows = hour['time'] end window_close_temp = hour['temperature'] end if !time_to_open_the_windows and time_to_close_the_windows and hour['temperature'] < last_temp and hour['temperature'].to_i <= 75 time_to_open_the_windows = hour['time'] end last_temp = hour['temperature'] break if index > 18 end # Return some meta here and let the caller decide the text. if time_to_close_the_windows.nil? "Leave 'em open, no excess heat today(#{get_temperature high_temp})." if high_temp <= 68 and high_temp > 62 "Open them up mid-day, high temp #{get_temperature high_temp}." elsif high_temp <= 62 "Best leave 'em shut, high temp #{get_temperature high_temp}." end else # Todo: base timezone on requested location. timezone = TZInfo::Timezone.get('America/Los_Angeles') if time_to_close_the_windows == 'now' output = "Close the windows now! It is #{get_temperature window_close_temp}. " else time_at = Time.at(time_to_close_the_windows).to_datetime local_time = timezone.utc_to_local(time_at) output = "Close the windows at #{local_time.strftime('%k:%M')}, it will be #{get_temperature window_close_temp}. " end if time_to_open_the_windows open_time = timezone.utc_to_local(Time.at(time_to_open_the_windows).to_datetime) output += "Open them back up at #{open_time.strftime('%k:%M')}. " end output += "The high today will be #{get_temperature high_temp}." end end
get_aggregate_by_data_key(forecast, key, datum)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 49 def get_aggregate_by_data_key(forecast, key, datum) unless forecast[key].nil? sum = 0 forecast[key]['data'].each do |data_point| Lita.logger.debug "Adding #{data_point[datum]} to #{sum}" sum += data_point[datum].to_f end sum.round(3) end end
get_alerts(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 520 def get_alerts(forecast) str = '' forecast['alerts'].each do |alert| alert['description'].match /\.\.\.(\w+)\.\.\./ str += "#{alert['uri']}\n" end str end
get_daily_comparison_text(temp_diff, high)
click to toggle source
If the temperature difference is positive,
# File lib/lita/handlers/forecasts.rb, line 546 def get_daily_comparison_text(temp_diff, high) if temp_diff <= 1 and temp_diff >= -1 'about the same as' elsif temp_diff > 1 and temp_diff <= 5 'cooler than' elsif temp_diff > 5 (high > 70)? 'much cooler than' : 'much colder than' elsif temp_diff < -1 and temp_diff >= -5 'warmer than' elsif temp_diff < -5 (high < 70)? 'much warmer than' : 'much hotter than' end end
get_max_by_data_key(forecast, key, datum)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 29 def get_max_by_data_key(forecast, key, datum) unless forecast[key].nil? data_points = [] forecast[key]['data'].each do |data_point| data_points.push data_point[datum] end data_points.max end end
get_min_by_data_key(forecast, key, datum)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 39 def get_min_by_data_key(forecast, key, datum) unless forecast[key].nil? data_points = [] forecast[key]['data'].each do |data_point| data_points.push data_point[datum] end data_points.min end end
Private Instance Methods
get_current_apparent_temp(forecast)
click to toggle source
# File lib/lita/handlers/forecasts.rb, line 689 def get_current_apparent_temp(forecast) forecast['hourly']['data'][0]['apparentTemperature'] end