Skip to content

Commit

Permalink
add wind calendar events
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhawksley committed Feb 3, 2025
1 parent 9c9e001 commit fba3691
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 26 deletions.
47 changes: 42 additions & 5 deletions app/apis/weather_kit_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,58 @@ def hourly_calendar_events

next if !weather_hour.present?

wind_suffix = if weather_hour[:windGust].to_f * 0.621371 > 20
" / #{(weather_hour[:windGust] * 0.621371).round}mph"
end

CalendarEvent.new(
id: "_weather_hour_#{hour.to_i}",
starts_at: hour,
ends_at: hour,
icon: icon_for(weather_hour[:conditionCode]),
summary: "#{celsius_fahrenheit(weather_hour[:temperature])}°#{wind_suffix}".html_safe
summary: "#{celsius_fahrenheit(weather_hour[:temperature])}°".html_safe
)
end.compact
end
end

def wind_calendar_events
events = []

hours_forecast = data.dig(:forecastHourly, :hours)

return events unless hours_forecast.present?

hours = data[:forecastHourly][:hours]

hours.each_with_index do |hour, index|
next if hour[:windGust].to_f * 0.621371 < 20

hour_i = DateTime.parse(hour[:forecastStart]).to_i

existing_event =
events.find { _1[:end_i] == hour_i }

if existing_event
existing_event[:end_i] += 3600
existing_event[:wind_max] = [existing_event[:wind_max], hour[:windGust]].max
else
events <<
{
start_i: hour_i,
end_i: (DateTime.parse(hour[:forecastStart]) + 1.hour).to_i,
wind_max: hour[:windGust].to_f * 0.621371
}
end
end

events.select { _1[:precipitation_type] != "clear" }.map do
CalendarEvent.new(
id: "#{_1[:start_i]}_wind",
starts_at: _1[:start_i],
ends_at: _1[:end_i],
icon: "wind",
summary: "Gusts up to #{_1[:wind_max].round}mph"
)
end
end

def precip_calendar_events
events = []

Expand Down
1 change: 1 addition & 0 deletions app/models/display_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def call(
weather_kit_api.daily_calendar_events +
weather_kit_api.hourly_calendar_events +
weather_kit_api.precip_calendar_events +
weather_kit_api.wind_calendar_events +
weather_kit_api.weather_alert_calendar_events
)

Expand Down
266 changes: 245 additions & 21 deletions test/apis/weather_kit_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1088,27 +1088,6 @@ def test_calendar_events_example
end
end

def test_wind_hourly_calendar_event
weather = {
forecastHourly: {
hours: [
{temperature: 20.83,
conditionCode: "Clear",
forecastStart: "2023-08-27T18:00:00Z",
precipitationType: "clear",
windGust: 51.15}
]
}
}

api = WeatherKitApi.new
api.stub :data, weather do
travel_to DateTime.new(2023, 8, 27, 16, 20, 0, "-0600") do
assert(api.hourly_calendar_events.last.summary.include?("32mph"))
end
end
end

def test_precip_calendar_events_no_data
api = WeatherKitApi.new
api.stub :data, {} do
Expand Down Expand Up @@ -2147,6 +2126,251 @@ def test_precip_calendar_events_example
end
end

def test_weather_wind_calendar_events_example
weather = {
forecastHourly: {
hours: [{"forecastStart" => "2025-02-03T06:00:00Z",
"cloudCover" => 0.26,
"conditionCode" => "MostlyClear",
"daylight" => false,
"humidity" => 0.22,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1002.91,
"pressureTrend" => "steady",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 16.31,
"temperatureApparent" => 11.89,
"temperatureDewPoint" => -5.47,
"uvIndex" => 0,
"visibility" => 60440.0,
"windDirection" => 42,
"windGust" => 35.45,
"windSpeed" => 16.23},
{"forecastStart" => "2025-02-03T07:00:00Z",
"cloudCover" => 0.27,
"conditionCode" => "MostlyClear",
"daylight" => false,
"humidity" => 0.21,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1003.23,
"pressureTrend" => "steady",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 16.14,
"temperatureApparent" => 11.71,
"temperatureDewPoint" => -6.22,
"uvIndex" => 0,
"visibility" => 60730.0,
"windDirection" => 168,
"windGust" => 32.23,
"windSpeed" => 16.12},
{"forecastStart" => "2025-02-03T08:00:00Z",
"cloudCover" => 0.15,
"conditionCode" => "MostlyClear",
"daylight" => false,
"humidity" => 0.21,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1003.48,
"pressureTrend" => "steady",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 16.01,
"temperatureApparent" => 11.47,
"temperatureDewPoint" => -6.33,
"uvIndex" => 0,
"visibility" => 61250.0,
"windDirection" => 86,
"windGust" => 32.24,
"windSpeed" => 15.96},
{"forecastStart" => "2025-02-03T09:00:00Z",
"cloudCover" => 0.55,
"conditionCode" => "PartlyCloudy",
"daylight" => false,
"humidity" => 0.22,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1003.74,
"pressureTrend" => "rising",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 15.19,
"temperatureApparent" => 13.0,
"temperatureDewPoint" => -6.41,
"uvIndex" => 0,
"visibility" => 57456.0,
"windDirection" => 294,
"windGust" => 28.68,
"windSpeed" => 9.01},
{"forecastStart" => "2025-02-03T10:00:00Z",
"cloudCover" => 0.53,
"conditionCode" => "PartlyCloudy",
"daylight" => false,
"humidity" => 0.23,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1004.37,
"pressureTrend" => "rising",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 14.73,
"temperatureApparent" => 12.56,
"temperatureDewPoint" => -6.21,
"uvIndex" => 0,
"visibility" => 56062.0,
"windDirection" => 264,
"windGust" => 41.76,
"windSpeed" => 8.76},
{"forecastStart" => "2025-02-03T11:00:00Z",
"cloudCover" => 0.52,
"conditionCode" => "PartlyCloudy",
"daylight" => false,
"humidity" => 0.24,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1004.57,
"pressureTrend" => "rising",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 14.59,
"temperatureApparent" => 10.76,
"temperatureDewPoint" => -5.77,
"uvIndex" => 0,
"visibility" => 57800.0,
"windDirection" => 38,
"windGust" => 29.3,
"windSpeed" => 14.06},
{"forecastStart" => "2025-02-03T12:00:00Z",
"cloudCover" => 0.5,
"conditionCode" => "PartlyCloudy",
"daylight" => false,
"humidity" => 0.24,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1005.15,
"pressureTrend" => "rising",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 14.64,
"temperatureApparent" => 11.81,
"temperatureDewPoint" => -5.73,
"uvIndex" => 0,
"visibility" => 55635.0,
"windDirection" => 293,
"windGust" => 24.27,
"windSpeed" => 10.7},
{"forecastStart" => "2025-02-03T13:00:00Z",
"cloudCover" => 0.56,
"conditionCode" => "PartlyCloudy",
"daylight" => false,
"humidity" => 0.24,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1006.02,
"pressureTrend" => "rising",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 14.39,
"temperatureApparent" => 11.82,
"temperatureDewPoint" => -5.94,
"uvIndex" => 0,
"visibility" => 55366.0,
"windDirection" => 281,
"windGust" => 23.4,
"windSpeed" => 10.02},
{"forecastStart" => "2025-02-03T14:00:00Z",
"cloudCover" => 0.56,
"conditionCode" => "PartlyCloudy",
"daylight" => false,
"humidity" => 0.28,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1006.96,
"pressureTrend" => "rising",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 13.52,
"temperatureApparent" => 10.95,
"temperatureDewPoint" => -4.66,
"uvIndex" => 0,
"visibility" => 50904.0,
"windDirection" => 20,
"windGust" => 23.18,
"windSpeed" => 9.81},
{"forecastStart" => "2025-02-03T15:00:00Z",
"cloudCover" => 0.74,
"conditionCode" => "MostlyCloudy",
"daylight" => true,
"humidity" => 0.26,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1007.84,
"pressureTrend" => "rising",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 14.65,
"temperatureApparent" => 12.49,
"temperatureDewPoint" => -4.67,
"uvIndex" => 0,
"visibility" => 53492.0,
"windDirection" => 146,
"windGust" => 22.49,
"windSpeed" => 10.21},
{"forecastStart" => "2025-02-03T16:00:00Z",
"cloudCover" => 0.7,
"conditionCode" => "MostlyCloudy",
"daylight" => true,
"humidity" => 0.24,
"precipitationAmount" => 0.0,
"precipitationIntensity" => 0.0,
"precipitationChance" => 0.0,
"precipitationType" => "clear",
"pressure" => 1008.53,
"pressureTrend" => "rising",
"snowfallIntensity" => 0.0,
"snowfallAmount" => 0.0,
"temperature" => 16.74,
"temperatureApparent" => 15.34,
"temperatureDewPoint" => -3.95,
"uvIndex" => 1,
"visibility" => 55763.0,
"windDirection" => 300,
"windGust" => 17.84,
"windSpeed" => 9.2}].map(&:with_indifferent_access)
}
}

api = WeatherKitApi.new
api.stub :data, weather do
travel_to DateTime.new(2025, 2, 3, 15, 20, 0, "-0600") do
assert_equal(2, api.wind_calendar_events.length)
end
end
end

# This failure was hidden by a caught error, commenting out for now
# def test_fetch_raises_no_errors
# VCR.use_cassette("weatherkit_fetch") do
Expand Down

0 comments on commit fba3691

Please sign in to comment.