-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
既存コードをAPI Guidelineに準拠させる #72
Merged
Merged
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8ba2766
edit: rename callbackFetchWeatherList to fetchWeather
daikiumehara 9520e85
edit: remove unnecessary makeResponse code
daikiumehara d385448
edit: refactor casting process
daikiumehara ebd0419
Example のテストコードで WeatherModel プロトコルの要求を満たせていなかったのを修正しました。
es-kumagai 50276c9
Example テストコード内で、loadWeather メソッドにその呼び出し主が渡されていないのを修正しました。
es-kumagai 70f228f
WeatherViewController に DisasterModelMock が設定されていないことが原因で、テストが強制終了するの…
es-kumagai 2d2da35
WeatherImageView への反映を待たずに XCTAssertEqual で結果判定をして失敗していた箇所を修正しました。
es-kumagai f8d13ae
テストコード内の typo を修正しました。
es-kumagai 81f35eb
テストコードでの View の準備は、ビューへのプロパティーアクセスを捨てる方法ではなく 'loadViewIfNeeded` を呼び出す…
es-kumagai c1fb6be
少なくとも現状のテストコードでは WeatherModelMock を 'setUpWithError' まで待たなくても準備できるため、…
es-kumagai acb3dda
単独で存在していた、不必要なドキュメントコメントを削除しました。
es-kumagai ee6cf6d
self. は省略される傾向があるため、それが明らかに不要な箇所については省略しました。
es-kumagai 17daa5f
ThreadBlock の課題文に、使用する API の特徴説明を追記しました。
es-kumagai b7bf09c
YumemiWeather クラスの見通しを良くするために、外部公開する API の機能とそれ以外とを区別して定義するようにしました。
es-kumagai 4a081da
Revert "edit: rename callbackFetchWeatherList to fetchWeather"
es-kumagai 6965ed4
不必要な名前空間の指定を削除しました。
es-kumagai ddef528
エンコードされたものが適切にデコードできるように修正しました。
es-kumagai 6911774
乱数生成器が独自定義されていることが想像しやすい名前 ControllableGenerator に変更し、その渡し方を標準ライブラリーに…
es-kumagai 121b753
標準ライブラリーの作法に基づいて、WeatherCondition をランダムで生成するコードを変更しました。
es-kumagai 3193e27
不必要な名前空間指定を削除しました。
es-kumagai 718cc61
ドキュメントコメントの誤字を修正しました。
es-kumagai feee440
不必要な名前空間の明記を省略しました。
es-kumagai 6d986e1
YumemiWeather の List 取得用 API で、地域に空を指定すると全地域の予報が取得できる旨をドキュメントコメントに記載し…
yumemi-kumagai 9cfb85b
YumemiWeather API が無作為にエラーを返す頻度をコントロール可能にしました。
es-kumagai 95aa9c9
Merge pull request #81 from yumemi-inc/feature/controllable_api_error
es-kumagai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,59 +25,10 @@ public enum YumemiWeatherError: Swift.Error { | |
|
||
final public class YumemiWeather { | ||
|
||
static let apiDuration: TimeInterval = 2 | ||
|
||
private static let dateFormatter: DateFormatter = { | ||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ" | ||
return dateFormatter | ||
}() | ||
|
||
static let decoder: JSONDecoder = { | ||
let decoder = JSONDecoder() | ||
decoder.dateDecodingStrategy = .formatted(dateFormatter) | ||
return decoder | ||
}() | ||
|
||
static let encoder: JSONEncoder = { | ||
let encoder = JSONEncoder() | ||
encoder.keyEncodingStrategy = .convertToSnakeCase | ||
encoder.dateEncodingStrategy = .formatted(dateFormatter) | ||
return encoder | ||
}() | ||
|
||
/// 引数の値でResponse構造体を作成する。引数がnilの場合はランダムに値を作成する。 | ||
/// - Parameters: | ||
/// - weatherCondition: 天気状況を表すenum | ||
/// - maxTemperature: 最高気温 | ||
/// - minTemperature: 最低気温 | ||
/// - date: 日付 | ||
/// - seed: シード値 | ||
/// - Returns: Response構造体 | ||
|
||
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(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( | ||
weatherCondition: weatherCondition.rawValue, | ||
maxTemperature: maxTemperature, | ||
minTemperature: minTemperature, | ||
date: date | ||
) | ||
} | ||
|
||
/// 擬似 天気予報 API Simple ver | ||
/// - Returns: 天気状況を表す文字列 "sunny" or "cloudy" or "rainy" | ||
public static func fetchWeatherCondition() -> String { | ||
return self.makeRandomResponse().weatherCondition | ||
return makeRandomResponse().weatherCondition | ||
} | ||
|
||
/// 擬似 天気予報 API Throws ver | ||
|
@@ -90,7 +41,7 @@ final public class YumemiWeather { | |
throw YumemiWeatherError.unknownError | ||
} | ||
|
||
return self.makeRandomResponse().weatherCondition | ||
return makeRandomResponse().weatherCondition | ||
} | ||
|
||
/// 擬似 天気予報 API JSON ver | ||
|
@@ -153,13 +104,8 @@ final public class YumemiWeather { | |
/// - Returns: Weather レスポンスの JSON 文字列 | ||
public static func syncFetchWeather(_ jsonString: String) throws -> String { | ||
Thread.sleep(forTimeInterval: apiDuration) | ||
return try self.fetchWeather(jsonString) | ||
return try fetchWeather(jsonString) | ||
} | ||
|
||
|
||
/// - Throws: YumemiWeatherError パラメータが正常でもランダムにエラーが発生する | ||
/// - Parameter jsonString: 地域と日付を含む JSON 文字列 | ||
/// - Returns: Weather レスポンスの JSON 文字列 | ||
|
||
/// 擬似 天気予報 API Callback ver | ||
/// | ||
|
@@ -187,10 +133,10 @@ final public class YumemiWeather { | |
DispatchQueue.global().asyncAfter(deadline: .now() + apiDuration) { | ||
do { | ||
let response = try fetchWeather(jsonString) | ||
completion(Result.success(response)) | ||
completion(.success(response)) | ||
} | ||
catch let error where error is YumemiWeatherError { | ||
completion(Result.failure(error as! YumemiWeatherError)) | ||
catch let error as YumemiWeatherError { | ||
completion(.failure(error)) | ||
} | ||
catch { | ||
fatalError() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
@@ -228,3 +174,57 @@ final public class YumemiWeather { | |
} | ||
} | ||
} | ||
|
||
extension YumemiWeather { | ||
|
||
static let apiDuration: TimeInterval = 2 | ||
|
||
private static let dateFormatter: DateFormatter = { | ||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ" | ||
return dateFormatter | ||
}() | ||
|
||
static let decoder: JSONDecoder = { | ||
let decoder = JSONDecoder() | ||
decoder.dateDecodingStrategy = .formatted(dateFormatter) | ||
return decoder | ||
}() | ||
|
||
static let encoder: JSONEncoder = { | ||
let encoder = JSONEncoder() | ||
encoder.keyEncodingStrategy = .convertToSnakeCase | ||
encoder.dateEncodingStrategy = .formatted(dateFormatter) | ||
return encoder | ||
}() | ||
|
||
/// 引数の値でResponse構造体を作成する。引数がnilの場合はランダムに値を作成する。 | ||
/// - Parameters: | ||
/// - weatherCondition: 天気状況を表すenum | ||
/// - maxTemperature: 最高気温 | ||
/// - minTemperature: 最低気温 | ||
/// - date: 日付 | ||
/// - seed: シード値 | ||
/// - Returns: Response構造体 | ||
|
||
static func makeRandomResponse( | ||
weatherCondition: WeatherCondition? = nil, | ||
maxTemperature: Int? = nil, | ||
minTemperature: Int? = nil, | ||
date: Date? = nil, | ||
seed: Int? = nil | ||
) -> Response { | ||
var generator = SeedRandomNumberGenerator(seed: seed ?? Int.random(in: Int.min...Int.max)) | ||
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( | ||
weatherCondition: weatherCondition.rawValue, | ||
maxTemperature: maxTemperature, | ||
minTemperature: minTemperature, | ||
date: date | ||
) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
エラー処理のキャスト処理をコンパクトにしました
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上手な型キャストパターンの扱い方です。