This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathStatsArchiveTimeIntervalData.swift
More file actions
82 lines (71 loc) · 2.42 KB
/
Copy pathStatsArchiveTimeIntervalData.swift
File metadata and controls
82 lines (71 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import Foundation
public struct StatsArchiveTimeIntervalData {
public let period: StatsPeriodUnit
public let unit: StatsPeriodUnit?
public let periodEndDate: Date
public let summary: [String: [StatsArchiveItem]]
public init(period: StatsPeriodUnit,
unit: StatsPeriodUnit? = nil,
periodEndDate: Date,
summary: [String: [StatsArchiveItem]]) {
self.period = period
self.unit = unit
self.periodEndDate = periodEndDate
self.summary = summary
}
}
public struct StatsArchiveItem {
public let href: String
public let value: String
public let views: Int
public init(href: String, value: String, views: Int) {
self.href = href
self.value = value
self.views = views
}
}
extension StatsArchiveTimeIntervalData: StatsTimeIntervalData {
public static var pathComponent: String {
return "stats/archives"
}
public static func queryProperties(with date: Date, period: StatsPeriodUnit, maxCount: Int) -> [String: String] {
return ["max": String(maxCount)]
}
public init?(date: Date, period: StatsPeriodUnit, jsonDictionary: [String: AnyObject]) {
self.init(date: date, period: period, unit: nil, jsonDictionary: jsonDictionary)
}
public init?(date: Date, period: StatsPeriodUnit, unit: StatsPeriodUnit?, jsonDictionary: [String: AnyObject]) {
guard let summary = jsonDictionary["summary"] as? [String: AnyObject] else {
return nil
}
self.period = period
self.unit = unit
self.periodEndDate = date
self.summary = {
var map: [String: [StatsArchiveItem]] = [:]
for (key, value) in summary {
let items = (value as? [[String: AnyObject]])?.compactMap {
StatsArchiveItem(jsonDictionary: $0)
} ?? []
if !items.isEmpty {
map[key] = items
}
}
return map
}()
}
}
private extension StatsArchiveItem {
init?(jsonDictionary: [String: AnyObject]) {
guard
let href = jsonDictionary["href"] as? String,
let value = jsonDictionary["value"] as? String,
let views = jsonDictionary["views"] as? Int
else {
return nil
}
self.href = href
self.value = value
self.views = views
}
}