Skip to content

Commit 0c5dc57

Browse files
authored
Merge pull request #32 from ribilynn/expression_improvement
コード文言とコメントの更新
2 parents 612a23f + f70966b commit 0c5dc57

File tree

7 files changed

+211
-97
lines changed

7 files changed

+211
-97
lines changed

Documentation/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
`YumemiWeather`のAPIを使用して、天気予報を取得しましょう。
33

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

88
## 課題

Documentation/Error.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
`YumemiWeather`のAPIがエラーをthrowしたときの実装をしましょう。
33

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

88
エラーが発生したときにどのように実装するか...

Sources/YumemiWeather/YumemiDisaster.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
public protocol YumemiDisasterHandleDelegate: class {
10+
public protocol YumemiDisasterHandleDelegate: AnyObject {
1111
func handle(disaster: String)
1212
}
1313

Sources/YumemiWeather/YumemiWeather.swift

Lines changed: 111 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ struct Request: Decodable {
66
}
77

88
struct Response: Codable, Equatable {
9-
let weather: String
10-
let maxTemp: Int
11-
let minTemp: Int
9+
let weatherCondition: String
10+
let maxTemperature: Int
11+
let minTemperature: Int
1212
let date: Date
1313
}
1414

15-
enum Weather: String, CaseIterable {
15+
enum WeatherCondition: String, CaseIterable {
1616
case sunny
1717
case cloudy
1818
case rainy
@@ -48,61 +48,72 @@ final public class YumemiWeather {
4848

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

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

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

6969
return Response(
70-
weather: weather.rawValue,
71-
maxTemp: maxTemp,
72-
minTemp: minTemp,
70+
weatherCondition: weatherCondition.rawValue,
71+
maxTemperature: maxTemperature,
72+
minTemperature: minTemperature,
7373
date: date
7474
)
7575
}
7676

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

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

93-
return self.makeRandomResponse().weather
93+
return self.makeRandomResponse().weatherCondition
9494
}
9595

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

122-
/// 擬似 天気予報API Sync ver
123-
/// - Parameter jsonString: 地域と日付を含むJson文字列
124-
/// example:
125-
/// {
126-
/// "area": "tokyo",
127-
/// "date": "2020-04-01T12:00:00+09:00"
128-
/// }
133+
/// 擬似 天気予報 API Sync ver
134+
///
135+
/// API に請求する JSON 文字列の例:
136+
///
137+
/// {
138+
/// "area": "tokyo",
139+
/// "date": "2020-04-01T12:00:00+09:00"
140+
/// }
141+
///
142+
/// 返された天気 JSON 文字列の例:
143+
///
144+
/// {
145+
/// "max_temperature":25,
146+
/// "date":"2020-04-01T12:00:00+09:00",
147+
/// "min_temperature":7,
148+
/// "weather_condition":"cloudy"
149+
/// }
150+
///
129151
/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する
130-
/// - Returns: Json文字列
152+
/// - Parameter jsonString: 地域と日付を含む JSON 文字列
153+
/// - Returns: Weather レスポンスの JSON 文字列
131154
public static func syncFetchWeather(_ jsonString: String) throws -> String {
132155
Thread.sleep(forTimeInterval: apiDuration)
133156
return try self.fetchWeather(jsonString)
134157
}
135158

136-
/// 擬似 天気予報API Callback ver
159+
160+
/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する
161+
/// - Parameter jsonString: 地域と日付を含む JSON 文字列
162+
/// - Returns: Weather レスポンスの JSON 文字列
163+
164+
/// 擬似 天気予報 API Callback ver
165+
///
166+
/// API に請求する JSON 文字列の例:
167+
///
168+
/// {
169+
/// "area": "tokyo",
170+
/// "date": "2020-04-01T12:00:00+09:00"
171+
/// }
172+
///
173+
/// 成功に返された天気 Result の中に JSON 文字列の例:
174+
///
175+
/// {
176+
/// "max_temperature":25,
177+
/// "date":"2020-04-01T12:00:00+09:00",
178+
/// "min_temperature":7,
179+
/// "weather_condition":"cloudy"
180+
/// }
181+
///
182+
/// また、YumemiWeatherError パラメータが正常でもランダムにエラーが発生する
137183
/// - Parameters:
138-
/// - jsonString: 地域と日付を含むJson文字列
139-
/// example:
140-
/// {
141-
/// "area": "tokyo",
142-
/// "date": "2020-04-01T12:00:00+09:00"
143-
/// }
184+
/// - jsonString: 地域と日付を含む JSON 文字列
144185
/// - completion: 完了コールバック
145186
public static func callbackFetchWeather(_ jsonString: String, completion: @escaping (Result<String, YumemiWeatherError>) -> Void) {
146187
DispatchQueue.global().asyncAfter(deadline: .now() + apiDuration) {
@@ -157,15 +198,27 @@ final public class YumemiWeather {
157198
}
158199
}
159200

160-
/// 擬似 天気予報API Async ver
161-
/// - Parameter jsonString: 地域と日付を含むJson文字列
162-
/// example:
163-
/// {
164-
/// "area": "tokyo",
165-
/// "date": "2020-04-01T12:00:00+09:00"
166-
/// }
201+
/// 擬似 天気予報 API Async ver
202+
///
203+
/// API に請求する JSON 文字列の例:
204+
///
205+
/// {
206+
/// "area": "tokyo",
207+
/// "date": "2020-04-01T12:00:00+09:00"
208+
/// }
209+
///
210+
/// 返された天気 JSON 文字列の例:
211+
///
212+
/// {
213+
/// "max_temperature":25,
214+
/// "date":"2020-04-01T12:00:00+09:00",
215+
/// "min_temperature":7,
216+
/// "weather_condition":"cloudy"
217+
/// }
218+
///
167219
/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する
168-
/// - Returns: Json文字列
220+
/// - Parameter jsonString: 地域と日付を含む JSON 文字列
221+
/// - Returns: Weather レスポンスの JSON 文字列
169222
@available(iOS 13, macOS 10.15, *)
170223
public static func asyncFetchWeather(_ jsonString: String) async throws -> String {
171224
return try await withCheckedThrowingContinuation { continuation in

0 commit comments

Comments
 (0)