Simplify icon path parsing logic
This commit is contained in:
parent
2bb20832f8
commit
9cf338f2d9
|
@ -80,11 +80,19 @@ public final class RedditFeedProvider: FeedProvider {
|
|||
}
|
||||
|
||||
public func iconURL(_ urlComponents: URLComponents, completion: @escaping (Result<String, Error>) -> Void) {
|
||||
guard urlComponents.path.hasPrefix("/r/"), let secondElement = extractSecondElement(path: urlComponents.path) else {
|
||||
guard urlComponents.path.hasPrefix("/r/") else {
|
||||
completion(.failure(RedditFeedProviderError.unknown))
|
||||
return
|
||||
}
|
||||
|
||||
let splitPath = urlComponents.path.split(separator: "/")
|
||||
guard splitPath.count > 1 else {
|
||||
completion(.failure(RedditFeedProviderError.unknown))
|
||||
return
|
||||
}
|
||||
|
||||
let secondElement = String(splitPath[1])
|
||||
|
||||
let api = "/r/\(secondElement)/about.json"
|
||||
|
||||
fetch(api: api, parameters: [:], resultType: RedditSubreddit.self) { result in
|
||||
|
@ -315,18 +323,7 @@ private extension RedditFeedProvider {
|
|||
completion(error)
|
||||
}
|
||||
}
|
||||
|
||||
func extractSecondElement(path: String) -> String? {
|
||||
let scanner = Scanner(string: path)
|
||||
if let _ = scanner.scanString("/"),
|
||||
let _ = scanner.scanUpToString("/"),
|
||||
let _ = scanner.scanString("/"),
|
||||
let secondElement = scanner.scanUpToString("/") {
|
||||
return secondElement
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
static func storeCredentials(username: String, oauthToken: String, oauthRefreshToken: String) throws {
|
||||
let tokenCredentials = Credentials(type: .oauthAccessToken, username: username, secret: oauthToken)
|
||||
try CredentialsManager.storeCredentials(tokenCredentials, server: Self.server)
|
||||
|
|
Loading…
Reference in New Issue