Skip to content

Commit

Permalink
Move the WeatherCode method to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
colinnuk committed Aug 29, 2024
1 parent 4456015 commit 0ed740f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 98 deletions.
71 changes: 0 additions & 71 deletions OpenMeteo/OpenMeteoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,77 +268,6 @@ public OpenMeteoClient(IOpenMeteoLogger logger, string apiKey)
return null;
}
}

/// <summary>
/// Converts a given weathercode to it's string representation
/// </summary>
/// <param name="weathercode"></param>
/// <returns><see cref="string"/> Weathercode string representation</returns>
public string WeathercodeToString(int weathercode)
{
switch (weathercode)
{
case 0:
return "Clear sky";
case 1:
return "Mainly clear";
case 2:
return "Partly cloudy";
case 3:
return "Overcast";
case 45:
return "Fog";
case 48:
return "Depositing rime Fog";
case 51:
return "Light drizzle";
case 53:
return "Moderate drizzle";
case 55:
return "Dense drizzle";
case 56:
return "Light freezing drizzle";
case 57:
return "Dense freezing drizzle";
case 61:
return "Slight rain";
case 63:
return "Moderate rain";
case 65:
return "Heavy rain";
case 66:
return "Light freezing rain";
case 67:
return "Heavy freezing rain";
case 71:
return "Slight snow fall";
case 73:
return "Moderate snow fall";
case 75:
return "Heavy snow fall";
case 77:
return "Snow grains";
case 80:
return "Slight rain showers";
case 81:
return "Moderate rain showers";
case 82:
return "Violent rain showers";
case 85:
return "Slight snow showers";
case 86:
return "Heavy snow showers";
case 95:
return "Thunderstorm";
case 96:
return "Thunderstorm with light hail";
case 99:
return "Thunderstorm with heavy hail";
default:
return "Invalid weathercode";
}
}

private async Task<WeatherForecast?> GetWeatherForecastAsync(WeatherForecastOptions options)
{
try
Expand Down
44 changes: 44 additions & 0 deletions OpenMeteo/WeatherCodeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace OpenMeteo;
public static class WeatherCodeHelper
{
/// <summary>
/// Converts a given weathercode to it's string representation
/// </summary>
/// <param name="weathercode"></param>
/// <returns><see cref="string"/> Weathercode string representation</returns>
public static string WeathercodeToString(int weathercode)
{
return weathercode switch
{
0 => "Clear sky",
1 => "Mainly clear",
2 => "Partly cloudy",
3 => "Overcast",
45 => "Fog",
48 => "Depositing rime Fog",
51 => "Light drizzle",
53 => "Moderate drizzle",
55 => "Dense drizzle",
56 => "Light freezing drizzle",
57 => "Dense freezing drizzle",
61 => "Slight rain",
63 => "Moderate rain",
65 => "Heavy rain",
66 => "Light freezing rain",
67 => "Heavy freezing rain",
71 => "Slight snow fall",
73 => "Moderate snow fall",
75 => "Heavy snow fall",
77 => "Snow grains",
80 => "Slight rain showers",
81 => "Moderate rain showers",
82 => "Violent rain showers",
85 => "Slight snow showers",
86 => "Heavy snow showers",
95 => "Thunderstorm",
96 => "Thunderstorm with light hail",
99 => "Thunderstorm with heavy hail",
_ => "Invalid weathercode",
};
}
}
27 changes: 0 additions & 27 deletions OpenMeteoTests/OpenMeteoClientTests.cs

This file was deleted.

26 changes: 26 additions & 0 deletions OpenMeteoTests/WeatherCodeHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenMeteo;

namespace OpenMeteoTests
{
[TestClass]
public class WeatherCodeHelperTests
{
[DataTestMethod]
[DataRow(0, "Clear sky")]
[DataRow(1, "Mainly clear")]
[DataRow(2, "Partly cloudy")]
[DataRow(3, "Overcast")]
[DataRow(51, "Light drizzle")]
[DataRow(53, "Moderate drizzle")]
[DataRow(96, "Thunderstorm with light hail")]
[DataRow(99, "Thunderstorm with heavy hail")]
[DataRow(100, "Invalid weathercode")]
public void Weather_Codes_To_String_Tests(int weatherCode, string expectedString)
{
string weatherCodeString = WeatherCodeHelper.WeathercodeToString(weatherCode);
Assert.IsInstanceOfType(weatherCodeString, typeof(string));
Assert.AreEqual(expectedString, weatherCodeString);
}
}
}

0 comments on commit 0ed740f

Please sign in to comment.