Swiftpack.co -  Package - mbarnach/WeatherGround
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
mbarnach/WeatherGround
Weather Underground Swift API wrapper.
.package(url: "https://github.com/mbarnach/WeatherGround.git", from: "0.0.3")

WeatherGround

Swift 5.1 Swift PM Compatible Swift

This package is a simple wrapper around the Weather Underground API. It provides a quick access to the current value of the Personal Weather Station (PWS), as well as hourly, and daily history. The features may be extended in the future if there is an interest (PR are welcome!).

The documentation is coming directly from the Weather Underground API.

The wrapper is a pure Swift wrapper, with codable, and use Foundation for the networking. There is no dependencies!

To use the API, simple set the API key from Weather Underground, and specify your station like that:

WeatherGround.measure.apiKey = "1234567890af"
WeatherGround.measure.station = "station_id"

If you want to retrieve the current values for the station:

WeatherGround.measure.current(){ values in 
    guard case .success(let observation) = values else {
        print("Cannot retrieve the current values")
        return
    }
    print("The weather is: \(observation)")
}

If you need to retrieve historical values, you can use one of hourly, daily or all. hourly and all returns a list of observations, whereas daily returns the daily average.

WeatherGround.measure.hourly(for: Date()){ history in 
    guard case .success(let observations) = history else {
        print("Unable to retrieve the observations.")
        return
    }
    print("The weather for \(Date()) was \(observations)")
}

The 5 day forecast is also available through forecast. It doesn't required a station value, only the apiKey.

WeatherGround.measure.forecast(for: Location.geo(latitude: 33.74, longitude: -84.39)){ fiveDay in
    guard case .success(let forecast) = fiveDay else {
        print("Unable to retrieve the forecast")
        return
    }
    print("5 day forecast:")
    for (day, narrative) in zip(forecast.dayOfWeek, forecast.narrative) {
        print("\(day): \(narrative)")
    }
    if let daypart = forecast.daypart.first {
        print("5 day forecast per day:")
        for (day, narrative) in zip(daypart.daypartName, daypart.narrative) {
            if let day = day, let narrative = narrative {
                print("\(day): \(narrative)")
            }
        }
    }
}

You can also retrieve the wind direction, intensity and narrative, as well as the temperature, rain, snow, moon phase, sunrise and sunset, thunder and precipitation chance, humidity and the icons id (list and id available here)

GitHub

link
Stars: 0
Last commit: 42 weeks ago

Release Notes

Typos
42 weeks ago

Fix few typos.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco