Add hashtag support

This commit is contained in:
Maurice Parker 2020-04-20 15:42:22 -05:00
parent 5f8249ec54
commit 77fa966a23

View File

@ -127,7 +127,9 @@ public struct TwitterFeedProvider: FeedProvider {
} }
default: default:
if let screenName = deriveScreenName(urlComponents) { if let hashtag = deriveHashtag(urlComponents) {
completion(.success("#\(hashtag)"))
} else if let screenName = deriveScreenName(urlComponents) {
retrieveUser(screenName: screenName) { result in retrieveUser(screenName: screenName) { result in
switch result { switch result {
case .success(let user): case .success(let user):
@ -170,13 +172,19 @@ public struct TwitterFeedProvider: FeedProvider {
} }
isSearch = true isSearch = true
default: default:
api = "statuses/user_timeline.json" if let hashtag = deriveHashtag(urlComponents) {
parameters["exclude_replies"] = true api = "search/tweets.json"
if let screenName = deriveScreenName(urlComponents) { parameters["q"] = "#\(hashtag)"
parameters["screen_name"] = screenName isSearch = true
} else { } else {
completion(.failure(TwitterFeedProviderError.unknown)) api = "statuses/user_timeline.json"
return parameters["exclude_replies"] = true
if let screenName = deriveScreenName(urlComponents) {
parameters["screen_name"] = screenName
} else {
completion(.failure(TwitterFeedProviderError.unknown))
return
}
} }
} }
@ -213,6 +221,14 @@ extension TwitterFeedProvider: OAuth1SwiftProvider {
private extension TwitterFeedProvider { private extension TwitterFeedProvider {
func deriveHashtag(_ urlComponents: URLComponents) -> String? {
let path = urlComponents.path
if path.starts(with: "/hashtag/"), let startIndex = path.index(path.startIndex, offsetBy: 9, limitedBy: path.endIndex), startIndex < path.endIndex {
return String(path[startIndex..<path.endIndex])
}
return nil
}
func deriveScreenName(_ urlComponents: URLComponents) -> String? { func deriveScreenName(_ urlComponents: URLComponents) -> String? {
let path = urlComponents.path let path = urlComponents.path
guard !Self.reservedPaths.contains(path) else { return nil } guard !Self.reservedPaths.contains(path) else { return nil }