Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
May 9, 2023

Creating a PowerShell Weather Command Part 2

Test-Driven Development

In the last article, I started the process of creating a new PowerShell command based on the Open-Meteo.com free weather API. For this project, I want to follow the test-driven development model, where the unit test is written first. I’ll then write the function to pass the test. Now, there is a bit of a chicken and egg thing going on here. I don’t think I can write the test without knowing what I want to achieve. In other words, I need a prototype. Ultimately, I want to write a function that will invoke this command using the API.

$r = Invoke-RestMethod 'https://api.open-meteo.com/v1/forecast?latitude=47.67&longitude=-122.12&daily=weathercode,temperature_2m_max,temperature_2m_min,sunrise,sunset,precipitation_sum&temperature_unit=fahrenheit&precipitation_unit=inch&forecast_days=1&timezone=America%2FLos_Angeles'
[PSCustomObject]@{
    Location        = 'Redmond,WA'
    Date            = $r.daily.time[0]
    WeatherCode     = $r.daily.weathercode[0]
    TemperatureLow  = $r.daily.temperature_2m_min[0]
    TemperatureHigh = $r.daily.temperature_2m_max[0]
    Precipitation   = $r.daily.precipitation_sum[0]
    Sunrise         = $r.daily.sunrise[0] -as [datetime]
    Sunset          = $r.daily.sunset[0] -as [datetime]
}

This gives me a result like this:

Location        : Redmond,WA
Date            : 2023-05-01
WeatherCode     : 51
TemperatureLow  : 46.5
TemperatureHigh : 61.6
Precipitation   : 0.016
Sunrise         : 5/1/2023 5:49:00 AM
Sunset          : 5/1/2023 8:21:00 PM
Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.