Skip to content

Commit

Permalink
Allow API customers to set the UnitTemperature to get either US or SI…
Browse files Browse the repository at this point in the history
… (metric) results from the API
  • Loading branch information
roblabs committed Apr 11, 2022
1 parent aa648de commit 5449a64
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ import Foundation
extension NationalWeatherService {
public typealias ForecastHandler = (Result<Forecast, Error>) -> Void

/// Allow API customers to set the [UnitTemperature](https://developer.apple.com/documentation/foundation/unittemperature)
/// to get ["*US customary or SI (metric) units in textual output*"](https://www.weather.gov/documentation/services-web-api#/default/gridpoint_forecast)
public static var units: UnitTemperature = .celsius

fileprivate func loadForecast(at url: URL, then handler: @escaping ForecastHandler) {
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!

let units: String = NationalWeatherService.units == .celsius ? "si" : "us"
components.queryItems = [
URLQueryItem(name: "units", value: "si")
URLQueryItem(name: "units", value: units)
]

self.load(at: components.url!, as: Forecast.self, then: handler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import XCTest
@testable import NationalWeatherService

final class GetForecastIntegrationTests: XCTestCase {
/// Get forecast without setting `NationalWeatherService.units`
func testGetForecastForLocation() throws {
let forecastExpectation = self.expectation(description: "get forecast expectation")
nws.forecast(latitude: 47.6174, longitude: -122.2017) { result in
Expand All @@ -15,7 +16,44 @@ final class GetForecastIntegrationTests: XCTestCase {

wait(for: [forecastExpectation], timeout: 5)
}

/// Get forecast by setting `NationalWeatherService.units` to `.fahrenheit`
func testGetForecastFahrenheit() throws {
let forecastExpectation = self.expectation(description: "get forecast expectation")

NationalWeatherService.units = .fahrenheit
nws.forecast(latitude: 47.6174, longitude: -122.2017) { result in
XCTAssertSuccess(result)

let forecast = try! result.get()
print(forecast)
XCTAssertFalse(forecast.periods.isEmpty)

forecastExpectation.fulfill()
}

wait(for: [forecastExpectation], timeout: 5)
}

/// Get forecast by setting `NationalWeatherService.units` to `.celsius`
func testGetForecastCelsius() throws {
let forecastExpectation = self.expectation(description: "get forecast expectation")

NationalWeatherService.units = .celsius
nws.forecast(latitude: 47.6174, longitude: -122.2017) { result in
XCTAssertSuccess(result)

let forecast = try! result.get()

print(forecast)
XCTAssertFalse(forecast.periods.isEmpty)

forecastExpectation.fulfill()
}

wait(for: [forecastExpectation], timeout: 5)
}

func testGetHourlyForecast() throws {
let hourlyForecastExpectation = self.expectation(description: "get hourly forecast expectation")
nws.hourlyForecast(latitude: 47.6174, longitude: -122.2017) { result in
Expand Down

0 comments on commit 5449a64

Please sign in to comment.