Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Henrik Panhans <[email protected]>
  • Loading branch information
henrik-dmg committed Apr 22, 2024
1 parent 1661f96 commit 3f5460b
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 133 deletions.
26 changes: 13 additions & 13 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ appearance, race, religion, or sexual identity and orientation.
Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
- The use of sexualized language or imagery and unwelcome sexual attention or
advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic
address, without explicit permission
- Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "HPOpenWeather",
platforms: [
.iOS(.v15), .tvOS(.v15), .watchOS(.v7), .macOS(.v12),
.iOS(.v15), .tvOS(.v15), .watchOS(.v6), .macOS(.v12),
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
Expand Down
45 changes: 17 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ HPOpenWeather is a cross-platform Swift framework to communicate with the OpenWe

## Installation

HPOpenWeather supports iOS 13.0+, watchOS 7.0+, tvOS 13.0+ and macOS 10.15+.
HPOpenWeather supports iOS 15.0+, watchOS 6.0+, tvOS 15.0+ and macOS 12+.

### SPM

Add `.package(url: "https://github.com/henrik-dmg/HPOpenWeather", from: "5.0.0")` to your `Package.swift` file

### CocoaPods

Add `pod 'HPOpenWeather'` to your `Podfile` and run `pod install`
Add `.package(url: "https://github.com/henrik-dmg/HPOpenWeather", from: "6.0.0")` to your `Package.swift` file

## Usage

To get started, you need an API key from [OpenWeather](https://openweathermap.org). Put this API key in the initialiser, additionally you can also specify a custom temperature format and/or language used in the responses (see list for available languages and units below).
### Configuration

To get started, you need an API key from [OpenWeather](https://openweathermap.org). Configure the `OpenWeather.shared` singleton or create your own instance and configure it with your key and other settings.

```swift
import HPOpenWeather
Expand All @@ -33,42 +31,33 @@ OpenWeather.shared.units = .metric

// Or use options
let settings = OpenWeather.Settings(apiKey: "yourAPIKey", language: .german, units: .metric)
OpenWeather.shared.apply(settings)
let openWeather = OpenWeather(settings: settings)
```

You can also customise the response data units and language by accessing the `language` and `units` propertis.

### Making a request

To make a request, initialize a new request object like this
### Retrieving Weather Information

```swift
let request = WeatherRequest(coordinate: .init(latitude: 40, longitude: 30))
```
To fetch the weather, there are two options: async/await or callback. Both expect a `CLLocationCoordinate2D` for which to fetch the weather.
Additionally, you can specify which fields should be excluded from the response to save bandwidth, or specify a historic date or a date up to 4 days in the future.

Or to request weather data from the past:
#### Async

```swift
let timemachineRequest = WeatherRequest(coordinate: .init(latitude: 40, longitude: 30), date: someDate)
let weather = try await OpenWeather.shared.weather(for: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194))
```

**Note:** the date has to be at least 6 hours in the past

To post a request, call `sendWeatherRequest` on `OpenWeather`:
#### Callback

```swift
// Classic completion handler approach
OpenWeather.shared.schedule(request) { result in
switch result {
case .success(let response):
// do something with weather data here
OpenWeather.shared.requestWeather(for: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194)) { result in
switch result {
case .success(let weather):
print(weather)
case .failure(let error):
// handle error
print(error)
}
}

// Or using the new concurrency features
let response = try await OpenWeather.shared.weatherResponse(request)
```

### Available languages (default in bold)
Expand Down
4 changes: 2 additions & 2 deletions Sources/HPOpenWeather/Models/Temperature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public struct Temperature: Equatable, Hashable {
/// A convertible measurement of the actually measured temperature.
/// - Parameter units: The units to use when formatting the `actual` property
/// - Returns: a measurement in the provided unit
public func actualMeasurement(units: Weather.Units) -> Measurement<UnitTemperature> {
public func actualMeasurement(units: WeatherUnits) -> Measurement<UnitTemperature> {
Measurement(value: actual, unit: units.temperatureUnit)
}

/// A convertible measurement of how the actually measured temperature feels like.
/// - Parameter units: The units to use when formatting the `feelsLike` property
/// - Returns: a measurement in the provided unit
public func feelsLikeMeasurement(units: Weather.Units) -> Measurement<UnitTemperature> {
public func feelsLikeMeasurement(units: WeatherUnits) -> Measurement<UnitTemperature> {
Measurement(value: feelsLike, unit: units.temperatureUnit)
}

Expand Down
98 changes: 47 additions & 51 deletions Sources/HPOpenWeather/Models/Weather+Language.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
import Foundation

extension Weather {

/// The language that should be used in API responses for example for weather condition descriptions.
public enum Language: String, Codable {
case afrikaans = "af"
case arabic = "ar"
case azerbaijani = "az"
case bulgarian = "bg"
case catalan = "ca"
case czech = "cz"
case danish = "da"
case german = "de"
case greek = "el"
case english = "en"
case basque = "eu"
case persian = "fa"
case finnish = "fi"
case french = "fr"
case galician = "gl"
case hebrew = "he"
case hindi = "hi"
case croatian = "hr"
case hungarian = "hu"
case indonesian = "id"
case italian = "it"
case japanese = "ja"
case korean = "kr"
case latvian = "la"
case lithuanian = "lt"
case macedonian = "mk"
case norwegian = "no"
case dutch = "nl"
case polish = "pl"
case portuguese = "pt"
case portugueseBrasil = "pt_br"
case romanian = "ro"
case russian = "ru"
case swedish = "sv"
case slovak = "sk"
case slovenian = "sl"
case spanish = "es"
case serbian = "sr"
case thai = "th"
case turkish = "tr"
case ukrainian = "ua"
case vietnamese = "vi"
case chineseSimplified = "zh_cn"
case chineseTraditional = "zh_tw"
case zulu = "zu"
}

/// The language that should be used in API responses for example for weather condition descriptions.
public enum WeatherLanguage: String, Codable {
case afrikaans = "af"
case arabic = "ar"
case azerbaijani = "az"
case bulgarian = "bg"
case catalan = "ca"
case czech = "cz"
case danish = "da"
case german = "de"
case greek = "el"
case english = "en"
case basque = "eu"
case persian = "fa"
case finnish = "fi"
case french = "fr"
case galician = "gl"
case hebrew = "he"
case hindi = "hi"
case croatian = "hr"
case hungarian = "hu"
case indonesian = "id"
case italian = "it"
case japanese = "ja"
case korean = "kr"
case latvian = "la"
case lithuanian = "lt"
case macedonian = "mk"
case norwegian = "no"
case dutch = "nl"
case polish = "pl"
case portuguese = "pt"
case portugueseBrasil = "pt_br"
case romanian = "ro"
case russian = "ru"
case swedish = "sv"
case slovak = "sk"
case slovenian = "sl"
case spanish = "es"
case serbian = "sr"
case thai = "th"
case turkish = "tr"
case ukrainian = "ua"
case vietnamese = "vi"
case chineseSimplified = "zh_cn"
case chineseTraditional = "zh_tw"
case zulu = "zu"
}
60 changes: 28 additions & 32 deletions Sources/HPOpenWeather/Models/Weather+Units.swift
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
import Foundation

extension Weather {
/// The units that should the data in the API responses should be formatted in.
public enum WeatherUnits: String, Codable {

/// The units that should the data in the API responses should be formatted in.
public enum Units: String, Codable {
/// Temperature in Kelvin and wind speed in meter/sec.
case standard
/// Temperature in Celsius and wind speed in meter/sec.
case metric
/// Temperature in Fahrenheit and wind speed in miles/hour.
case imperial

/// Temperature in Kelvin and wind speed in meter/sec.
case standard
/// Temperature in Celsius and wind speed in meter/sec.
case metric
/// Temperature in Fahrenheit and wind speed in miles/hour.
case imperial

@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
var temperatureUnit: UnitTemperature {
switch self {
case .standard:
return .kelvin
case .metric:
return .celsius
case .imperial:
return .fahrenheit
}
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
var temperatureUnit: UnitTemperature {
switch self {
case .standard:
return .kelvin
case .metric:
return .celsius
case .imperial:
return .fahrenheit
}
}

@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
var windSpeedUnit: UnitSpeed {
switch self {
case .standard:
return .metersPerSecond
case .metric:
return .metersPerSecond
case .imperial:
return .milesPerHour
}
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
var windSpeedUnit: UnitSpeed {
switch self {
case .standard:
return .metersPerSecond
case .metric:
return .metersPerSecond
case .imperial:
return .milesPerHour
}

}

}
}
10 changes: 5 additions & 5 deletions Sources/HPOpenWeather/OpenWeather.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public final class OpenWeather {
/// The API key to use for weather requests.
let apiKey: String
/// The language that will be used in weather responses.
let language: Weather.Language
let language: WeatherLanguage
/// The units that will be used in weather responses.
let units: Weather.Units
let units: WeatherUnits

/// Initialises a new settings instance.
/// - Parameters:
/// - apiKey: The API key to use for weather requests
/// - language: The language that will be used in weather responses
/// - units: The units that will be used in weather responses
public init(apiKey: String, language: Weather.Language = .english, units: Weather.Units = .metric) {
public init(apiKey: String, language: WeatherLanguage = .english, units: WeatherUnits = .metric) {
self.language = language
self.units = units
self.apiKey = apiKey
Expand All @@ -38,9 +38,9 @@ public final class OpenWeather {
/// The OpenWeatherMap API key to authorize requests.
public var apiKey: String?
/// The language that should be used in API responses.
public var language: Weather.Language = .english
public var language: WeatherLanguage = .english
/// The units that should be used to format the API responses.
public var units: Weather.Units = .metric
public var units: WeatherUnits = .metric

// MARK: - Init

Expand Down
17 changes: 16 additions & 1 deletion Sources/HPOpenWeather/OpenWeatherError.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import Foundation

public enum OpenWeatherError: Error, Equatable {
public enum OpenWeatherError: LocalizedError, Equatable {

case invalidRequestTimestamp
case invalidAPIKey
case noCurrentConditionReturned
case invalidTimeZoneIdentifier(_ identifier: String)

public var errorDescription: String? {
switch self {
case .invalidRequestTimestamp:
return "The request timestamp is invalid"
case .invalidAPIKey:
return "The API key is missing or empty"
case .noCurrentConditionReturned:
return "No current condition was returned"
case .invalidTimeZoneIdentifier(let identifier):
return "The timezone identifier '\(identifier)' is invalid"
}
}

}

0 comments on commit 3f5460b

Please sign in to comment.