Skip to content

Commit

Permalink
Merge pull request #32 from ribilynn/expression_improvement
Browse files Browse the repository at this point in the history
コード文言とコメントの更新
  • Loading branch information
el-hoshino authored Jun 12, 2022
2 parents 612a23f + f70966b commit 0c5dc57
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Documentation/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
`YumemiWeather`のAPIを使用して、天気予報を取得しましょう。

Simple ver
`public static func fetchWeather() -> String`
`public static func fetchWeatherCondition() -> String`
[APIの概要](YumemiWeather.md)

## 課題
Expand Down
2 changes: 1 addition & 1 deletion Documentation/Error.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
`YumemiWeather`のAPIがエラーをthrowしたときの実装をしましょう。

Throws ver
`static func fetchWeather(at area: String) throws -> String`
`static func fetchWeatherCondition(at area: String) throws -> String`
[APIの概要](YumemiWeather.md)

エラーが発生したときにどのように実装するか...
Expand Down
2 changes: 1 addition & 1 deletion Sources/YumemiWeather/YumemiDisaster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public protocol YumemiDisasterHandleDelegate: class {
public protocol YumemiDisasterHandleDelegate: AnyObject {
func handle(disaster: String)
}

Expand Down
169 changes: 111 additions & 58 deletions Sources/YumemiWeather/YumemiWeather.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ struct Request: Decodable {
}

struct Response: Codable, Equatable {
let weather: String
let maxTemp: Int
let minTemp: Int
let weatherCondition: String
let maxTemperature: Int
let minTemperature: Int
let date: Date
}

enum Weather: String, CaseIterable {
enum WeatherCondition: String, CaseIterable {
case sunny
case cloudy
case rainy
Expand Down Expand Up @@ -48,61 +48,72 @@ final public class YumemiWeather {

/// 引数の値でResponse構造体を作成する。引数がnilの場合はランダムに値を作成する。
/// - Parameters:
/// - weather: 天気を表すenum
/// - maxTemp: 最高気温
/// - minTemp: 最低気温
/// - weatherCondition: 天気状況を表すenum
/// - maxTemperature: 最高気温
/// - minTemperature: 最低気温
/// - date: 日付
/// - seed: シード値
/// - Returns: Response構造体

static func makeRandomResponse(weather: Weather? = nil, maxTemp: Int? = nil, minTemp: Int? = nil, date: Date? = nil, seed: Int? = nil) -> Response {
return makeRandomResponse(weather: weather, maxTemp: maxTemp, minTemp: minTemp, date: date, seed: seed ?? Int.random(in: Int.min...Int.max))
static func makeRandomResponse(weatherCondition: WeatherCondition? = nil, maxTemperature: Int? = nil, minTemperature: Int? = nil, date: Date? = nil, seed: Int? = nil) -> Response {
return makeRandomResponse(weatherCondition: weatherCondition, maxTemperature: maxTemperature, minTemperature: minTemperature, date: date, seed: seed ?? Int.random(in: Int.min...Int.max))
}

private static func makeRandomResponse(weather: Weather?, maxTemp: Int?, minTemp: Int?, date: Date?, seed seedValue: Int) -> Response {
var seed = SeedRandomNumberGenerator(seed: seedValue)
let weather = weather ?? Weather.allCases.randomElement(using: &seed)!
let maxTemp = maxTemp ?? Int.random(in: 10...40, using: &seed)
let minTemp = minTemp ?? Int.random(in: -40..<maxTemp, using: &seed)
private static func makeRandomResponse(weatherCondition: WeatherCondition?, maxTemperature: Int?, minTemperature: Int?, date: Date?, seed seedValue: Int) -> Response {
var generator = SeedRandomNumberGenerator(seed: seedValue)
let weatherCondition = weatherCondition ?? WeatherCondition.allCases.randomElement(using: &generator)!
let maxTemperature = maxTemperature ?? Int.random(in: 10...40, using: &generator)
let minTemperature = minTemperature ?? Int.random(in: -40..<maxTemperature, using: &generator)
let date = date ?? Date()

return Response(
weather: weather.rawValue,
maxTemp: maxTemp,
minTemp: minTemp,
weatherCondition: weatherCondition.rawValue,
maxTemperature: maxTemperature,
minTemperature: minTemperature,
date: date
)
}

/// 擬似 天気予報API Simple ver
/// - Returns: 天気を表す文字列 "sunny" or "cloudy" or "rainy"
public static func fetchWeather() -> String {
return self.makeRandomResponse().weather
/// 擬似 天気予報 API Simple ver
/// - Returns: 天気状況を表す文字列 "sunny" or "cloudy" or "rainy"
public static func fetchWeatherCondition() -> String {
return self.makeRandomResponse().weatherCondition
}

/// 擬似 天気予報API Throws ver
/// 擬似 天気予報 API Throws ver
/// - Throws: YumemiWeatherError
/// - Parameters:
/// - area: 天気予報を取得する対象地域 example: "tokyo"
/// - Throws: YumemiWeatherError
/// - Returns: 天気を表す文字列 "sunny" or "cloudy" or "rainy"
public static func fetchWeather(at area: String) throws -> String {
/// - Returns: 天気状況を表す文字列 "sunny" or "cloudy" or "rainy"
public static func fetchWeatherCondition(at area: String) throws -> String {
if Int.random(in: 0...4) == 4 {
throw YumemiWeatherError.unknownError
}

return self.makeRandomResponse().weather
return self.makeRandomResponse().weatherCondition
}

/// 擬似 天気予報API Json ver
/// - Parameter jsonString: 地域と日付を含むJson文字列
/// example:
/// {
/// "area": "tokyo",
/// "date": "2020-04-01T12:00:00+09:00"
/// }
/// 擬似 天気予報 API JSON ver
///
/// API に請求する JSON 文字列の例:
///
/// {
/// "area": "tokyo",
/// "date": "2020-04-01T12:00:00+09:00"
/// }
///
/// 返された天気 JSON 文字列の例:
///
/// {
/// "max_temperature":25,
/// "date":"2020-04-01T12:00:00+09:00",
/// "min_temperature":7,
/// "weather_condition":"cloudy"
/// }
///
/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する
/// - Returns: Json文字列
/// example: {"max_temp":25,"date":"2020-04-01T12:00:00+09:00","min_temp":7,"weather":"cloudy"}
/// - Parameter jsonString: 地域と日付を含む JSON 文字列
/// - Returns: Weather レスポンスの JSON 文字列
public static func fetchWeather(_ jsonString: String) throws -> String {
guard let requestData = jsonString.data(using: .utf8),
let request = try? decoder.decode(Request.self, from: requestData) else {
Expand All @@ -119,28 +130,58 @@ final public class YumemiWeather {
return String(data: responseData, encoding: .utf8)!
}

/// 擬似 天気予報API Sync ver
/// - Parameter jsonString: 地域と日付を含むJson文字列
/// example:
/// {
/// "area": "tokyo",
/// "date": "2020-04-01T12:00:00+09:00"
/// }
/// 擬似 天気予報 API Sync ver
///
/// API に請求する JSON 文字列の例:
///
/// {
/// "area": "tokyo",
/// "date": "2020-04-01T12:00:00+09:00"
/// }
///
/// 返された天気 JSON 文字列の例:
///
/// {
/// "max_temperature":25,
/// "date":"2020-04-01T12:00:00+09:00",
/// "min_temperature":7,
/// "weather_condition":"cloudy"
/// }
///
/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する
/// - Returns: Json文字列
/// - Parameter jsonString: 地域と日付を含む JSON 文字列
/// - Returns: Weather レスポンスの JSON 文字列
public static func syncFetchWeather(_ jsonString: String) throws -> String {
Thread.sleep(forTimeInterval: apiDuration)
return try self.fetchWeather(jsonString)
}

/// 擬似 天気予報API Callback ver

/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する
/// - Parameter jsonString: 地域と日付を含む JSON 文字列
/// - Returns: Weather レスポンスの JSON 文字列

/// 擬似 天気予報 API Callback ver
///
/// API に請求する JSON 文字列の例:
///
/// {
/// "area": "tokyo",
/// "date": "2020-04-01T12:00:00+09:00"
/// }
///
/// 成功に返された天気 Result の中に JSON 文字列の例:
///
/// {
/// "max_temperature":25,
/// "date":"2020-04-01T12:00:00+09:00",
/// "min_temperature":7,
/// "weather_condition":"cloudy"
/// }
///
/// また、YumemiWeatherError パラメータが正常でもランダムにエラーが発生する
/// - Parameters:
/// - jsonString: 地域と日付を含むJson文字列
/// example:
/// {
/// "area": "tokyo",
/// "date": "2020-04-01T12:00:00+09:00"
/// }
/// - jsonString: 地域と日付を含む JSON 文字列
/// - completion: 完了コールバック
public static func callbackFetchWeather(_ jsonString: String, completion: @escaping (Result<String, YumemiWeatherError>) -> Void) {
DispatchQueue.global().asyncAfter(deadline: .now() + apiDuration) {
Expand All @@ -157,15 +198,27 @@ final public class YumemiWeather {
}
}

/// 擬似 天気予報API Async ver
/// - Parameter jsonString: 地域と日付を含むJson文字列
/// example:
/// {
/// "area": "tokyo",
/// "date": "2020-04-01T12:00:00+09:00"
/// }
/// 擬似 天気予報 API Async ver
///
/// API に請求する JSON 文字列の例:
///
/// {
/// "area": "tokyo",
/// "date": "2020-04-01T12:00:00+09:00"
/// }
///
/// 返された天気 JSON 文字列の例:
///
/// {
/// "max_temperature":25,
/// "date":"2020-04-01T12:00:00+09:00",
/// "min_temperature":7,
/// "weather_condition":"cloudy"
/// }
///
/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する
/// - Returns: Json文字列
/// - Parameter jsonString: 地域と日付を含む JSON 文字列
/// - Returns: Weather レスポンスの JSON 文字列
@available(iOS 13, macOS 10.15, *)
public static func asyncFetchWeather(_ jsonString: String) async throws -> String {
return try await withCheckedThrowingContinuation { continuation in
Expand Down
Loading

0 comments on commit 0c5dc57

Please sign in to comment.