diff --git a/Mastodon/Service/APIService/APIService+Onboarding.swift b/Mastodon/Service/APIService/APIService+Onboarding.swift index 588366f8..450dc141 100644 --- a/Mastodon/Service/APIService/APIService+Onboarding.swift +++ b/Mastodon/Service/APIService/APIService+Onboarding.swift @@ -23,4 +23,10 @@ extension APIService { return Mastodon.API.Onboarding.categories(session: session) } + func stubCategories() -> [Mastodon.Entity.Category] { + return Mastodon.Entity.Category.Kind.allCases.map { kind in + return Mastodon.Entity.Category(category: kind.rawValue, serversCount: 0) + } + } + } diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift index d6ae7fef..3f7f69a6 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Onboarding.swift @@ -44,9 +44,9 @@ extension Mastodon.API.Onboarding { .eraseToAnyPublisher() } - /// Fetch server catagories + /// Fetch server categories /// - /// Using this endpoint to fetch booked catagories + /// Using this endpoint to fetch booked categories /// /// - Since: 3.3.0 /// - Version: 3.3.0 diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Category.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Category.swift index 614e2a75..d1fe7c73 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Category.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Category.swift @@ -10,13 +10,94 @@ import Foundation extension Mastodon.Entity { public struct Category: Codable { - public let category: String + public let category: Kind public let serversCount: Int enum CodingKeys: String, CodingKey { case category case serversCount = "servers_count" } + + public init(category: String, serversCount: Int) { + self.category = Kind(rawValue: category) ?? ._other(category) + self.serversCount = serversCount + } + + /// # Reference + /// https://joinmastodon.org/communities + public enum Kind: RawRepresentable, Codable { + + case general + case regional + case art + case music + case journalism + case activism + case lgbt + case games + case tech + case academia + case furry + case food + + case _other(String) + + public init?(rawValue: String) { + switch rawValue { + case "general": self = .general + case "regional": self = .regional + case "art": self = .art + case "music": self = .music + case "journalism": self = .journalism + case "activism": self = .activism + case "lgbt": self = .lgbt + case "games": self = .games + case "tech": self = .tech + case "academia": self = .academia + case "furry": self = .furry + case "food": self = .food + default: self = ._other(rawValue) + } + } + + public var rawValue: String { + switch self { + case .general: return "general" + case .regional: return "regional" + case .art: return "art" + case .music: return "music" + case .journalism: return "journalism" + case .activism: return "activism" + case .lgbt: return "lgbt" + case .games: return "games" + case .tech: return "tech" + case .academia: return "academia" + case .furry: return "furry" + case .food: return "food" + case ._other(let value): return value + + } + } + } } } + +extension Mastodon.Entity.Category.Kind: CaseIterable { + public static var allCases: [Mastodon.Entity.Category.Kind] { + return [ + .general, + .regional, + .art, + .music, + .journalism, + .activism, + .lgbt, + .games, + .tech, + .academia, + .furry, + .food, + ] + } +} diff --git a/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+OnboardingTests.swift b/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+OnboardingTests.swift index f13c38e1..0590888f 100644 --- a/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+OnboardingTests.swift +++ b/MastodonSDK/Tests/MastodonSDKTests/API/MastodonSDK+API+OnboardingTests.swift @@ -40,8 +40,8 @@ extension MastodonSDKTests { wait(for: [theExpectation], timeout: 10.0) } - func testCatagories() throws { - let theExpectation = expectation(description: "Fetch Server Catagories") + func testCategories() throws { + let theExpectation = expectation(description: "Fetch Server Categories") Mastodon.API.Onboarding.categories( session: session ) @@ -62,5 +62,9 @@ extension MastodonSDKTests { wait(for: [theExpectation], timeout: 10.0) } + func testCategoryKind() { + XCTAssertEqual(Mastodon.Entity.Category.Kind.allCases.count, 12) + } + }