Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit be9948e

Browse files
committed
Update StatsEmailFollowersInsight to use Codable
1 parent 061b616 commit be9948e

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public struct StatsEmailFollowersInsight {
1+
public struct StatsEmailFollowersInsight: Codable {
22
public let emailFollowersCount: Int
33
public let topEmailFollowers: [StatsFollower]
44

@@ -7,6 +7,11 @@ public struct StatsEmailFollowersInsight {
77
self.emailFollowersCount = emailFollowersCount
88
self.topEmailFollowers = topEmailFollowers
99
}
10+
11+
private enum CodingKeys: String, CodingKey {
12+
case emailFollowersCount = "total_email"
13+
case topEmailFollowers = "subscribers"
14+
}
1015
}
1116

1217
extension StatsEmailFollowersInsight: StatsInsightData {
@@ -22,17 +27,12 @@ extension StatsEmailFollowersInsight: StatsInsightData {
2227
}
2328

2429
public init?(jsonDictionary: [String: AnyObject]) {
25-
guard
26-
let subscribersCount = jsonDictionary["total_email"] as? Int,
27-
let subscribers = jsonDictionary["subscribers"] as? [[String: AnyObject]]
28-
else {
29-
return nil
30+
do {
31+
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
32+
let decoder = JSONDecoder()
33+
self = try decoder.decode(StatsEmailFollowersInsight.self, from: jsonData)
34+
} catch {
35+
return nil
3036
}
31-
32-
let followers = subscribers.compactMap { StatsFollower(jsonDictionary: $0) }
33-
34-
self.emailFollowersCount = subscribersCount
35-
self.topEmailFollowers = followers
3637
}
37-
3838
}

0 commit comments

Comments
 (0)