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 pathStatsTopPostsTimeIntervalData.swift
More file actions
69 lines (59 loc) · 2.36 KB
/
Copy pathStatsTopPostsTimeIntervalData.swift
File metadata and controls
69 lines (59 loc) · 2.36 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
public struct StatsTopPostsTimeIntervalData {
public let period: StatsPeriodUnit
public let periodEndDate: Date
public let totalViewsCount: Int
public let otherViewsCount: Int
public let topPosts: [StatsTopPost]
public init(period: StatsPeriodUnit,
periodEndDate: Date,
topPosts: [StatsTopPost],
totalViewsCount: Int,
otherViewsCount: Int) {
self.period = period
self.periodEndDate = periodEndDate
self.topPosts = topPosts
self.totalViewsCount = totalViewsCount
self.otherViewsCount = otherViewsCount
}
}
extension StatsTopPostsTimeIntervalData: StatsTimeIntervalData {
public static var pathComponent: String {
return "stats/top-posts"
}
public init?(date: Date, period: StatsPeriodUnit, jsonDictionary: [String: AnyObject]) {
guard
let unwrappedDays = type(of: self).unwrapDaysDictionary(jsonDictionary: jsonDictionary),
let posts = unwrappedDays["postviews"] as? [[String: AnyObject]]
else {
return nil
}
let totalViews = unwrappedDays["total_views"] as? Int ?? 0
let otherViews = unwrappedDays["other_views"] as? Int ?? 0
self.periodEndDate = date
self.period = period
self.totalViewsCount = totalViews
self.otherViewsCount = otherViews
self.topPosts = posts.compactMap { StatsTopPost(topPostsJSONDictionary: $0) }
}
}
private extension StatsTopPost {
// the objects returned from this endpoint are _almost_ the same as the ones from `top-posts`,
// but with keys just subtly different enough that we need a custom init here.
init?(topPostsJSONDictionary jsonDictionary: [String: AnyObject]) {
guard
let url = jsonDictionary["href"] as? String,
let postID = jsonDictionary["id"] as? Int,
let title = jsonDictionary["title"] as? String,
let viewsCount = jsonDictionary["views"] as? Int,
let typeString = jsonDictionary["type"] as? String
else {
return nil
}
self.title = title
self.date = jsonDictionary["date"] as? String
self.postID = postID
self.postURL = URL(string: url)
self.viewsCount = viewsCount
self.kind = type(of: self).kind(from: typeString)
}
}