Skip to content

Commit

Permalink
Update StatsAnnualInsight to use Codable
Browse files Browse the repository at this point in the history
  • Loading branch information
staskus committed Mar 22, 2024
1 parent d64f490 commit 793b821
Showing 1 changed file with 43 additions and 23 deletions.
66 changes: 43 additions & 23 deletions WordPressKit/Insights/StatsAllAnnualInsight.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
public struct StatsAllAnnualInsight {
public struct StatsAllAnnualInsight: Codable {
public let allAnnualInsights: [StatsAnnualInsight]

public init(allAnnualInsights: [StatsAnnualInsight]) {
self.allAnnualInsights = allAnnualInsights
}

enum CodingKeys: String, CodingKey {
case allAnnualInsights = "years"
}
}

public struct StatsAnnualInsight {
public struct StatsAnnualInsight: Codable {
public let year: Int
public let totalPostsCount: Int
public let totalWordsCount: Int
Expand Down Expand Up @@ -39,6 +43,38 @@ public struct StatsAnnualInsight {
self.totalImagesCount = totalImagesCount
self.averageImagesCount = averageImagesCount
}

private enum CodingKeys: String, CodingKey {
case year
case totalPostsCount = "total_posts"
case totalWordsCount = "total_words"
case averageWordsCount = "avg_words"
case totalLikesCount = "total_likes"
case averageLikesCount = "avg_likes"
case totalCommentsCount = "total_comments"
case averageCommentsCount = "avg_comments"
case totalImagesCount = "total_images"
case averageImagesCount = "avg_images"
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

if let year = Int(try container.decode(String.self, forKey: .year)) {
self.year = year
} else {
throw DecodingError.dataCorruptedError(forKey: .year, in: container, debugDescription: "Year cannot be parsed into number.")
}
totalPostsCount = (try? container.decodeIfPresent(Int.self, forKey: .totalPostsCount)) ?? 0
totalWordsCount = (try? container.decodeIfPresent(Int.self, forKey: .totalWordsCount)) ?? 0
averageWordsCount = (try? container.decodeIfPresent(Double.self, forKey: .averageWordsCount)) ?? 0
totalLikesCount = (try? container.decodeIfPresent(Int.self, forKey: .totalLikesCount)) ?? 0
averageLikesCount = (try? container.decodeIfPresent(Double.self, forKey: .averageLikesCount)) ?? 0
totalCommentsCount = (try? container.decodeIfPresent(Int.self, forKey: .totalCommentsCount)) ?? 0
averageCommentsCount = (try? container.decodeIfPresent(Double.self, forKey: .averageCommentsCount)) ?? 0
totalImagesCount = (try? container.decodeIfPresent(Int.self, forKey: .totalImagesCount)) ?? 0
averageImagesCount = (try? container.decodeIfPresent(Double.self, forKey: .averageImagesCount)) ?? 0
}
}

extension StatsAllAnnualInsight: StatsInsightData {
Expand All @@ -47,28 +83,12 @@ extension StatsAllAnnualInsight: StatsInsightData {
}

public init?(jsonDictionary: [String: AnyObject]) {
guard let yearlyInsights = jsonDictionary["years"] as? [[String: AnyObject]] else {
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
let decoder = JSONDecoder()
self = try decoder.decode(StatsAllAnnualInsight.self, from: jsonData)
} catch {
return nil
}

let allAnnualInsights: [StatsAnnualInsight] = yearlyInsights.compactMap {
guard let yearString = $0["year"] as? String,
let year = Int(yearString) else {
return nil
}

return StatsAnnualInsight(year: year,
totalPostsCount: $0["total_posts"] as? Int ?? 0,
totalWordsCount: $0["total_words"] as? Int ?? 0,
averageWordsCount: $0["avg_words"] as? Double ?? 0,
totalLikesCount: $0["total_likes"] as? Int ?? 0,
averageLikesCount: $0["avg_likes"] as? Double ?? 0,
totalCommentsCount: $0["total_comments"] as? Int ?? 0,
averageCommentsCount: $0["avg_comments"] as? Double ?? 0,
totalImagesCount: $0["total_images"] as? Int ?? 0,
averageImagesCount: $0["avg_images"] as? Double ?? 0)
}

self.allAnnualInsights = allAnnualInsights
}
}

0 comments on commit 793b821

Please sign in to comment.