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 pathStatsLastPostInsight.swift
More file actions
98 lines (85 loc) · 3.49 KB
/
Copy pathStatsLastPostInsight.swift
File metadata and controls
98 lines (85 loc) · 3.49 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import Foundation
public struct StatsLastPostInsight: Equatable, Decodable {
public let title: String
public let url: URL
public let publishedDate: Date
public let likesCount: Int
public let commentsCount: Int
public private(set) var viewsCount: Int = 0
public let postID: Int
public let featuredImageURL: URL?
public init(title: String,
url: URL,
publishedDate: Date,
likesCount: Int,
commentsCount: Int,
viewsCount: Int,
postID: Int,
featuredImageURL: URL?) {
self.title = title
self.url = url
self.publishedDate = publishedDate
self.likesCount = likesCount
self.commentsCount = commentsCount
self.viewsCount = viewsCount
self.postID = postID
self.featuredImageURL = featuredImageURL
}
}
extension StatsLastPostInsight: StatsInsightData {
// MARK: - StatsInsightData Conformance
public static func queryProperties(with maxCount: Int) -> [String: String] {
return ["order_by": "date",
"number": "1",
"type": "post",
"fields": "ID, title, URL, discussion, like_count, date, featured_image"]
}
public static var pathComponent: String {
return "posts/"
}
public init?(jsonDictionary: [String: AnyObject]) {
self.init(jsonDictionary: jsonDictionary, views: 0)
}
// MARK: -
private static let dateFormatter = ISO8601DateFormatter()
public init?(jsonDictionary: [String: AnyObject], views: Int) {
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
let decoder = JSONDecoder()
self = try decoder.decode(StatsLastPostInsight.self, from: jsonData)
self.viewsCount = views
} catch {
return nil
}
}
}
extension StatsLastPostInsight {
private enum CodingKeys: String, CodingKey {
case title
case url = "URL"
case publishedDate = "date"
case likesCount = "like_count"
case commentsCount
case postID = "ID"
case featuredImageURL = "featured_image"
case discussion
}
private enum DiscussionKeys: String, CodingKey {
case commentsCount = "comment_count"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
title = try container.decode(String.self, forKey: .title).trimmingCharacters(in: .whitespaces).wpkit_stringByDecodingXMLCharacters()
url = try container.decode(URL.self, forKey: .url)
let dateString = try container.decode(String.self, forKey: .publishedDate)
guard let date = StatsLastPostInsight.dateFormatter.date(from: dateString) else {
throw DecodingError.dataCorruptedError(forKey: .publishedDate, in: container, debugDescription: "Date string does not match format expected by formatter.")
}
publishedDate = date
likesCount = (try? container.decodeIfPresent(Int.self, forKey: .likesCount)) ?? 0
postID = try container.decode(Int.self, forKey: .postID)
featuredImageURL = try? container.decodeIfPresent(URL.self, forKey: .featuredImageURL)
let discussionContainer = try container.nestedContainer(keyedBy: DiscussionKeys.self, forKey: .discussion)
commentsCount = (try? discussionContainer.decodeIfPresent(Int.self, forKey: .commentsCount)) ?? 0
}
}