Design and stub out FeedProvider protocol

This commit is contained in:
Maurice Parker 2020-04-15 16:35:09 -05:00
parent d883981afd
commit 266e136787
3 changed files with 63 additions and 8 deletions

View File

@ -8,7 +8,26 @@
import Foundation
import RSCore
import RSParser
public enum FeedProviderAbility {
case owner
case available
case none
}
public protocol FeedProvider {
/// Informs the caller of the ability for this feed provider to service the given URL
func ability(_ url: URLComponents, forUsername: String?) -> FeedProviderAbility
/// Provide the iconURL of the given URL
func iconURL(_ url: URLComponents, completion: @escaping (Result<String, Error>) -> Void)
/// Construct a ParsedFeed that can be used to create and store a new Feed
func provide(_ url: URLComponents, completion: @escaping (Result<ParsedFeed, Error>) -> Void)
/// Refresh all the article entries (ParsedItems)
func refresh(_ url: URLComponents, completion: @escaping (Result<Set<ParsedItem>, Error>) -> Void)
}

View File

@ -9,9 +9,10 @@
import Foundation
import Secrets
import OAuthSwift
import RSParser
public struct TwitterFeedProvider: FeedProvider {
public struct TwitterFeedProvider {
private static let server = "api.twitter.com"
public var userID: String
@ -50,9 +51,34 @@ public struct TwitterFeedProvider {
self.oauthToken = tokenCredentials.secret
self.oauthTokenSecret = tokenSecretCredentials.secret
}
public func ability(_ urlComponents: URLComponents, forUsername username: String?) -> FeedProviderAbility {
guard urlComponents.host?.hasSuffix("twitter.com") ?? false else {
return .none
}
if let username = username, username == userID {
return .owner
}
return .available
}
public func iconURL(_ url: URLComponents, completion: @escaping (Result<String, Error>) -> Void) {
// TODO: Finish implementation
}
public func provide(_ url: URLComponents, completion: @escaping (Result<ParsedFeed, Error>) -> Void) {
// TODO: Finish implementation
}
public func refresh(_ url: URLComponents, completion: @escaping (Result<Set<ParsedItem>, Error>) -> Void) {
// TODO: Finish implementation
}
}
// MARK: OAuth1SwiftProvider
extension TwitterFeedProvider: OAuth1SwiftProvider {
@ -67,9 +93,3 @@ extension TwitterFeedProvider: OAuth1SwiftProvider {
}
}
// MARK: FeedProvider
extension TwitterFeedProvider: FeedProvider {
}

View File

@ -75,6 +75,13 @@ final class ExtensionPointManager {
saveExtensionPointIDs()
}
func bestFeedProvider(for offered: URLComponents, with username: String?) -> FeedProvider? {
if let owner = feedProviderMatching(offered, forUsername: username, ability: .owner) {
return owner
}
return feedProviderMatching(offered, forUsername: username, ability: .available)
}
}
private extension ExtensionPointManager {
@ -123,4 +130,13 @@ private extension ExtensionPointManager {
}
}
func feedProviderMatching(_ offered: URLComponents, forUsername username: String?, ability: FeedProviderAbility) -> FeedProvider? {
for extensionPoint in activeExtensionPoints.values {
if let feedProvider = extensionPoint as? FeedProvider, feedProvider.ability(offered, forUsername: username) == ability {
return feedProvider
}
}
return nil
}
}