Skip to content

Commit

Permalink
Fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
henrik-dmg committed Apr 13, 2020
1 parent fd53850 commit 465b5ee
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
13 changes: 8 additions & 5 deletions Sources/HPOpenWeather/Response/CurrentWeather.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,20 @@ public struct CurrentWeather: BasicWeatherResponse, SunResponse {

try container.encode(temperature.actual, forKey: .temperature)
try container.encode(temperature.feelsLike, forKey: .feelsLike)
try container.encode(snow, forKey: .snow)
try container.encode(rain, forKey: .rain)
try container.encode(timestamp, forKey: .timestamp)
try container.encode(pressure, forKey: .pressure)
try container.encode(humidity, forKey: .humidity)
try container.encode(dewPoint, forKey: .uvIndex)
try container.encode(visibility, forKey: .cloudCoverage)
try container.encode(dewPoint, forKey: .dewPoint)
try container.encode(uvIndex, forKey: .uvIndex)
try container.encode(visibility, forKey: .visibility)
try container.encode(cloudCoverage, forKey: .cloudCoverage)
try container.encode(rain, forKey: .rain)
try container.encode(snow, forKey: .snow)

try container.encode(wind.speed, forKey: .windSpeed)
try container.encode(wind.degrees, forKey: .windDirection)
try container.encode(wind.gust, forKey: .windGust)
try container.encode(sun.sunset, forKey: .sunrise)
try container.encode(sun.sunset, forKey: .sunset)
try container.encode(sun.sunrise, forKey: .sunrise)
try container.encode(weatherArray, forKey: .weatherArray)
}
Expand Down
9 changes: 6 additions & 3 deletions Sources/HPOpenWeather/Response/DailyForecast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ public struct DailyForecast: BasicWeatherResponse, SunResponse {
try container.encode(timestamp, forKey: .timestamp)
try container.encode(pressure, forKey: .pressure)
try container.encode(humidity, forKey: .humidity)
try container.encode(dewPoint, forKey: .uvIndex)
try container.encode(visibility, forKey: .cloudCoverage)
try container.encode(dewPoint, forKey: .dewPoint)
try container.encode(uvIndex, forKey: .uvIndex)
try container.encode(visibility, forKey: .visibility)
try container.encode(cloudCoverage, forKey: .cloudCoverage)

try container.encode(wind.speed, forKey: .windSpeed)
try container.encode(wind.degrees, forKey: .windDirection)
try container.encode(wind.gust, forKey: .windGust)
try container.encode(sun.sunset, forKey: .sunrise)
try container.encode(sun.sunset, forKey: .sunset)
try container.encode(sun.sunrise, forKey: .sunrise)
try container.encode(weatherArray, forKey: .weatherArray)
}
Expand Down
17 changes: 10 additions & 7 deletions Sources/HPOpenWeather/Response/HourlyForecast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct HourlyForecast: BasicWeatherResponse {
}

enum CodingKeys: String, CodingKey {
case temp
case temperature = "temp"
case feelsLike = "feels_like"
case snow
case rain
Expand Down Expand Up @@ -52,7 +52,7 @@ public struct HourlyForecast: BasicWeatherResponse {
visibility = try container.decodeIfPresent(Double.self, forKey: .visibility)
cloudCoverage = try container.decodeIfPresent(Double.self, forKey: .cloudCoverage)

let temp = try container.decode(Double.self, forKey: .temp)
let temp = try container.decode(Double.self, forKey: .temperature)
let feelsLike = try container.decode(Double.self, forKey: .feelsLike)
temperature = Temperature(actual: temp, feelsLike: feelsLike)

Expand All @@ -65,15 +65,18 @@ public struct HourlyForecast: BasicWeatherResponse {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(temperature.actual, forKey: .temp)
try container.encode(temperature.actual, forKey: .temperature)
try container.encode(temperature.feelsLike, forKey: .feelsLike)
try container.encode(snow, forKey: .snow)
try container.encode(rain, forKey: .rain)
try container.encode(timestamp, forKey: .timestamp)
try container.encode(pressure, forKey: .pressure)
try container.encode(humidity, forKey: .humidity)
try container.encode(dewPoint, forKey: .uvIndex)
try container.encode(visibility, forKey: .cloudCoverage)
try container.encode(dewPoint, forKey: .dewPoint)
try container.encode(uvIndex, forKey: .uvIndex)
try container.encode(visibility, forKey: .visibility)
try container.encode(cloudCoverage, forKey: .cloudCoverage)
try container.encode(rain, forKey: .rain)
try container.encode(snow, forKey: .snow)

try container.encode(wind.speed, forKey: .windSpeed)
try container.encode(wind.degrees, forKey: .windDirection)
try container.encode(wind.gust, forKey: .windGust)
Expand Down
2 changes: 1 addition & 1 deletion Sources/HPOpenWeather/Response/OpenWeatherResponse.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct OpenWeatherResponse: Codable {
public struct OpenWeatherResponse: Codable, Equatable, Hashable {

public let timezone: TimeZone
public let current: CurrentWeather
Expand Down
32 changes: 23 additions & 9 deletions Tests/HPOpenWeatherTests/HPOpenWeatherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class HPOpenWeatherTests: XCTestCase {
override class func setUp() {
super.setUp()

HPOpenWeather.shared.apiKey = "fa0355d40a89af27650111cc80667159"
HPOpenWeather.shared.apiKey = TestSecret.apiKey
}

override func tearDown() {
Expand All @@ -22,17 +22,31 @@ final class HPOpenWeatherTests: XCTestCase {

HPOpenWeather.shared.requestWeather(request) { result in
exp.fulfill()
switch result {
case .success(let response):
print(response.hourlyForecasts.first)
print(response.current.condition)
case .failure(let error as NSError):
print(error)
XCTFail(error.localizedDescription)
}
XCTAssertResult(result)
}

wait(for: [exp], timeout: 10)
}

}

extension Encodable {

func encodeAndDecode<T: Decodable>(type: T.Type) throws -> T {
let jsonEncoder = JSONEncoder()
jsonEncoder.dateEncodingStrategy = .secondsSince1970
let encodedData = try jsonEncoder.encode(self)

let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .secondsSince1970
return try jsonDecoder.decode(type.self, from: encodedData)
}

}

/// Asserts that the result is not a failure
func XCTAssertResult<T, E: Error>(_ result: Result<T, E>) {
if case .failure(let error) = result {
XCTFail(error.localizedDescription)
}
}
7 changes: 7 additions & 0 deletions Tests/HPOpenWeatherTests/TestSecret.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

struct TestSecret {

static let apiKey: String! = ProcessInfo.processInfo.environment["API_KEY"]

}

0 comments on commit 465b5ee

Please sign in to comment.