feat: add stub categories

This commit is contained in:
CMK 2021-02-19 12:17:12 +08:00
parent c919a724ab
commit 9e0a7bb352
4 changed files with 96 additions and 5 deletions

View File

@ -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)
}
}
}

View File

@ -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

View File

@ -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,
]
}
}

View File

@ -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)
}
}