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

Commit 7d6e0e7

Browse files
staskusPaul Von Schrottky
authored andcommitted
Conform to StatsTimeIntervalData
1 parent 3197069 commit 7d6e0e7

File tree

3 files changed

+31
-38
lines changed

3 files changed

+31
-38
lines changed
Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import Foundation
22
import WordPressShared
33

4-
public struct StatsSubscribersSummaryData: Decodable, Equatable {
4+
public struct StatsSubscribersSummaryData: Equatable {
55
public let history: [SubscriberData]
6+
public let period: StatsPeriodUnit
7+
public let periodEndDate: Date
68

7-
public init(history: [SubscriberData]) {
9+
public init(history: [SubscriberData], period: StatsPeriodUnit, periodEndDate: Date) {
810
self.history = history
11+
self.period = period
12+
self.periodEndDate = periodEndDate
913
}
1014
}
1115

12-
extension StatsSubscribersSummaryData {
16+
extension StatsSubscribersSummaryData: StatsTimeIntervalData {
1317
public static var pathComponent: String {
1418
return "stats/subscribers"
1519
}
@@ -21,7 +25,14 @@ extension StatsSubscribersSummaryData {
2125
return df
2226
}()
2327

24-
public struct SubscriberData: Decodable, Equatable {
28+
static var weeksDateFormatter: DateFormatter = {
29+
let df = DateFormatter()
30+
df.locale = Locale(identifier: "en_US_POS")
31+
df.dateFormat = "yyyy'W'MM'W'dd"
32+
return df
33+
}()
34+
35+
public struct SubscriberData: Equatable {
2536
public let date: Date
2637
public let count: Int
2738

@@ -31,7 +42,7 @@ extension StatsSubscribersSummaryData {
3142
}
3243
}
3344

34-
public init?(jsonDictionary: [String: AnyObject]) {
45+
public init?(date: Date, period: StatsPeriodUnit, jsonDictionary: [String: AnyObject]) {
3546
guard
3647
let fields = jsonDictionary["fields"] as? [String],
3748
let data = jsonDictionary["data"] as? [[Any]],
@@ -43,9 +54,9 @@ extension StatsSubscribersSummaryData {
4354

4455
let history: [SubscriberData?] = data.map { elements in
4556
guard elements.indices.contains(dateIndex) && elements.indices.contains(countIndex),
46-
let dateString = elements[dateIndex] as? String,
47-
let date = StatsSubscribersSummaryData.dateFormatter.date(from: dateString),
48-
let count = elements[countIndex] as? Int
57+
let dateString = elements[dateIndex] as? String,
58+
let date = StatsSubscribersSummaryData.parsedDate(from: dateString, for: period),
59+
let count = elements[countIndex] as? Int
4960
else {
5061
return nil
5162
}
@@ -54,14 +65,20 @@ extension StatsSubscribersSummaryData {
5465
}
5566

5667
let sorted = history.compactMap { $0 }.sorted(by: { $0.date.compare($1.date) == .orderedAscending })
57-
self = .init(history: sorted)
68+
69+
self = .init(history: sorted, period: period, periodEndDate: date)
5870
}
5971

60-
public static func queryProperties(quantity: Int, unit: StatsSubscribersSummaryData.Unit) -> [String: String] {
61-
return ["quantity": String(quantity), "unit": unit.rawValue]
72+
private static func parsedDate(from dateString: String, for period: StatsPeriodUnit) -> Date? {
73+
switch period {
74+
case .week:
75+
return self.weeksDateFormatter.date(from: dateString)
76+
case .day, .month, .year:
77+
return self.dateFormatter.date(from: dateString)
78+
}
6279
}
6380

64-
public enum Unit: String {
65-
case day = "day"
81+
public static func queryProperties(with date: Date, period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
82+
return ["quantity": String(maxCount), "unit": period.stringValue]
6683
}
6784
}

Sources/WordPressKit/Services/StatsServiceRemoteV2.swift

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -316,30 +316,6 @@ private extension StatsServiceRemoteV2 {
316316
}
317317
}
318318

319-
// MARK: - Subscribers Data
320-
321-
public extension StatsServiceRemoteV2 {
322-
func getSubscribers(unit: StatsSubscribersSummaryData.Unit,
323-
completion: @escaping ((Result<StatsSubscribersSummaryData, Error>) -> Void)) {
324-
let pathComponent = StatsSubscribersSummaryData.pathComponent
325-
let path = self.path(forEndpoint: "sites/\(siteID)/\(pathComponent)/", withVersion: ._1_1)
326-
let properties = StatsSubscribersSummaryData.queryProperties(quantity: 30, unit: unit) as [String: AnyObject]
327-
328-
wordPressComRESTAPI.get(path, parameters: properties, success: { (response, _) in
329-
guard let jsonResponse = response as? [String: AnyObject],
330-
let subscribersSummaryData = StatsSubscribersSummaryData(jsonDictionary: jsonResponse)
331-
else {
332-
completion(.failure(ResponseError.decodingFailure))
333-
return
334-
}
335-
336-
completion(.success(subscribersSummaryData))
337-
}, failure: { (error, _) in
338-
completion(.failure(error))
339-
})
340-
}
341-
}
342-
343319
// MARK: - Emails Summary
344320

345321
public extension StatsServiceRemoteV2 {

Tests/WordPressKitTests/Tests/Models/Stats/V2/StatsSubscribersSummaryDataTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ final class StatsSubscribersSummaryDataTests: XCTestCase {
55
func testEmailsSummaryDecoding() throws {
66
let json = getJSON("stats-subscribers")
77

8-
let summary = StatsSubscribersSummaryData(jsonDictionary: json)
8+
let summary = StatsSubscribersSummaryData(date: Date(), period: .day, jsonDictionary: json)
99
XCTAssertNotNil(summary, "StatsSubscribersSummaryData not decoded as expected")
1010
let history = summary!.history
1111
let mostRecentDay = history.last!

0 commit comments

Comments
 (0)