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

Commit 09a00e2

Browse files
committed
Make JSON decoding logic reusable for StatsInsightData type
1 parent be9948e commit 09a00e2

11 files changed

+12
-102
lines changed

WordPressKit/Insights/StatsAllAnnualInsight.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,4 @@ extension StatsAllAnnualInsight: StatsInsightData {
8181
public static var pathComponent: String {
8282
return "stats/insights"
8383
}
84-
85-
public init?(jsonDictionary: [String: AnyObject]) {
86-
do {
87-
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
88-
let decoder = JSONDecoder()
89-
self = try decoder.decode(StatsAllAnnualInsight.self, from: jsonData)
90-
} catch {
91-
return nil
92-
}
93-
}
9484
}

WordPressKit/Insights/StatsAllTimesInsight.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,6 @@ public struct StatsAllTimesInsight: Codable {
3131
}
3232

3333
extension StatsAllTimesInsight: StatsInsightData {
34-
35-
// MARK: - StatsInsightData Conformance
36-
public init?(jsonDictionary: [String: AnyObject]) {
37-
do {
38-
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
39-
let decoder = JSONDecoder()
40-
self = try decoder.decode(StatsAllTimesInsight.self, from: jsonData)
41-
} catch {
42-
return nil
43-
}
44-
}
45-
4634
public init (from decoder: Decoder) throws {
4735
let rootContainer = try decoder.container(keyedBy: RootKeys.self)
4836
let container = try rootContainer.nestedContainer(keyedBy: CodingKeys.self, forKey: .stats)

WordPressKit/Insights/StatsAnnualAndMostPopularTimeInsight.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ extension StatsAnnualAndMostPopularTimeInsight: StatsInsightData {
4848
public static var pathComponent: String {
4949
return "stats/insights"
5050
}
51-
52-
public init?(jsonDictionary: [String: AnyObject]) {
53-
do {
54-
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
55-
let decoder = JSONDecoder()
56-
self = try decoder.decode(StatsAnnualAndMostPopularTimeInsight.self, from: jsonData)
57-
} catch {
58-
return nil
59-
}
60-
}
6151
}
6252

6353
extension StatsAnnualAndMostPopularTimeInsight {

WordPressKit/Insights/StatsCommentsInsight.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@ extension StatsCommentsInsight: StatsInsightData {
2020
public static var pathComponent: String {
2121
return "stats/comments"
2222
}
23-
24-
public init?(jsonDictionary: [String: AnyObject]) {
25-
do {
26-
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
27-
let decoder = JSONDecoder()
28-
self = try decoder.decode(StatsCommentsInsight.self, from: jsonData)
29-
} catch {
30-
return nil
31-
}
32-
}
3323
}
3424

3525
public struct StatsTopCommentsAuthor: Codable {

WordPressKit/Insights/StatsDotComFollowersInsight.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ extension StatsDotComFollowersInsight: StatsInsightData {
2626
return "stats/followers"
2727
}
2828

29-
public init?(jsonDictionary: [String: AnyObject]) {
30-
do {
31-
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
32-
let decoder = JSONDecoder()
33-
self = try decoder.decode(StatsDotComFollowersInsight.self, from: jsonData)
34-
} catch {
35-
return nil
36-
}
37-
}
38-
3929
fileprivate static let dateFormatter = ISO8601DateFormatter()
4030
}
4131

WordPressKit/Insights/StatsEmailFollowersInsight.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,4 @@ extension StatsEmailFollowersInsight: StatsInsightData {
2525
public static var pathComponent: String {
2626
return "stats/followers"
2727
}
28-
29-
public init?(jsonDictionary: [String: AnyObject]) {
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
36-
}
37-
}
3828
}

WordPressKit/Insights/StatsPostingStreakInsight.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ public struct StatsPostingStreakInsight: Codable {
2929
case postingEvents = "data"
3030
}
3131

32-
public init?(jsonDictionary: [String: AnyObject]) {
33-
do {
34-
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
35-
let decoder = JSONDecoder()
36-
self = try decoder.decode(StatsPostingStreakInsight.self, from: jsonData)
37-
} catch {
38-
return nil
39-
}
40-
}
41-
4232
public init(from decoder: any Decoder) throws {
4333
let container = try decoder.container(keyedBy: CodingKeys.self)
4434
self.streaks = try container.decode(PostingStreaks.self, forKey: .streaks)

WordPressKit/Insights/StatsPublicizeInsight.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ extension StatsPublicizeInsight: StatsInsightData {
1616
public static var pathComponent: String {
1717
return "stats/publicize"
1818
}
19-
20-
public init?(jsonDictionary: [String: AnyObject]) {
21-
do {
22-
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
23-
let decoder = JSONDecoder()
24-
self = try decoder.decode(StatsPublicizeInsight.self, from: jsonData)
25-
} catch {
26-
return nil
27-
}
28-
}
2919
}
3020

3121
public struct StatsPublicizeService: Codable {

WordPressKit/Insights/StatsTagsAndCategoriesInsight.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ extension StatsTagsAndCategoriesInsight: StatsInsightData {
1010
public static var pathComponent: String {
1111
return "stats/tags"
1212
}
13-
14-
public init?(jsonDictionary: [String: AnyObject]) {
15-
do {
16-
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
17-
let decoder = JSONDecoder()
18-
self = try decoder.decode(StatsTagsAndCategoriesInsight.self, from: jsonData)
19-
} catch {
20-
return nil
21-
}
22-
}
2313
}
2414

2515
public struct StatsTagAndCategory: Codable {

WordPressKit/Insights/StatsTodayInsight.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ extension StatsTodayInsight: StatsInsightData {
2222
return "stats/summary"
2323
}
2424

25-
public init?(jsonDictionary: [String: AnyObject]) {
26-
do {
27-
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
28-
let decoder = JSONDecoder()
29-
self = try decoder.decode(StatsTodayInsight.self, from: jsonData)
30-
} catch {
31-
return nil
32-
}
33-
}
34-
3525
private enum CodingKeys: String, CodingKey {
3626
case viewsCount = "views"
3727
case visitorsCount = "visitors"

0 commit comments

Comments
 (0)