|
| 1 | +import Foundation |
| 2 | + |
| 3 | + |
| 4 | +/// Represents a WordPress.com Site. |
| 5 | +/// |
| 6 | +public struct Site: Decodable { |
| 7 | + |
| 8 | + /// WordPress.com Site Identifier. |
| 9 | + /// |
| 10 | + let siteID: Int |
| 11 | + |
| 12 | + /// Site's Name. |
| 13 | + /// |
| 14 | + let name: String |
| 15 | + |
| 16 | + /// Site's Description. |
| 17 | + /// |
| 18 | + let description: String |
| 19 | + |
| 20 | + /// Site's URL. |
| 21 | + /// |
| 22 | + let url: String |
| 23 | + |
| 24 | + /// Indicates if this site hosts a WordPress Store. |
| 25 | + /// |
| 26 | + let isWordPressStore: Bool |
| 27 | + |
| 28 | + |
| 29 | + /// Designated Initializer. |
| 30 | + /// |
| 31 | + public init(from decoder: Decoder) throws { |
| 32 | + let siteContainer = try decoder.container(keyedBy: SiteKeys.self) |
| 33 | + |
| 34 | + siteID = try siteContainer.decode(Int.self, forKey: .siteID) |
| 35 | + name = try siteContainer.decode(String.self, forKey: .name) |
| 36 | + description = try siteContainer.decode(String.self, forKey: .description) |
| 37 | + url = try siteContainer.decode(String.self, forKey: .url) |
| 38 | + |
| 39 | + let optionsContainer = try siteContainer.nestedContainer(keyedBy: OptionKeys.self, forKey: .options) |
| 40 | + isWordPressStore = try optionsContainer.decode(Bool.self, forKey: .isWordPressStore) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +/// Defines all of the Site CodingKeys. |
| 46 | +/// |
| 47 | +private extension Site { |
| 48 | + |
| 49 | + enum SiteKeys: String, CodingKey { |
| 50 | + case siteID = "ID" |
| 51 | + case name = "name" |
| 52 | + case description = "description" |
| 53 | + case url = "URL" |
| 54 | + case options = "options" |
| 55 | + } |
| 56 | + |
| 57 | + enum OptionKeys: String, CodingKey { |
| 58 | + case isWordPressStore = "is_wpcom_store" |
| 59 | + } |
| 60 | +} |
0 commit comments