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 pathReaderFeed.swift
More file actions
75 lines (62 loc) · 2.49 KB
/
Copy pathReaderFeed.swift
File metadata and controls
75 lines (62 loc) · 2.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
import Foundation
/// ReaderFeed
/// Encapsulates details of a single feed returned by the Reader feed search API
/// (read/feed?q=query)
///
public struct ReaderFeed: Decodable {
public let url: URL
public let title: String
public let feedDescription: String?
public let feedID: String?
public let blogID: String?
public let blavatarURL: URL?
private enum CodingKeys: String, CodingKey {
case url = "URL"
case title = "title"
case feedID = "feed_ID"
case blogID = "blog_ID"
case meta = "meta"
}
private enum MetaKeys: CodingKey {
case data
}
private enum DataKeys: CodingKey {
case site
}
private enum SiteKeys: CodingKey {
case description
case icon
}
private enum IconKeys: CodingKey {
case img
}
public init(from decoder: Decoder) throws {
// We have to manually decode the feed from the JSON, for a couple of reasons:
// - Some feeds have no `icon` dictionary
// - Some feeds have no `data` dictionary
// - We want to decode whatever we can get, and not fail if neither of those exist
let rootContainer = try decoder.container(keyedBy: CodingKeys.self)
url = try rootContainer.decode(URL.self, forKey: .url)
title = try rootContainer.decode(String.self, forKey: .title)
feedID = try? rootContainer.decode(String.self, forKey: .feedID)
blogID = try? rootContainer.decode(String.self, forKey: .blogID)
var feedDescription: String?
var blavatarURL: URL?
do {
let metaContainer = try rootContainer.nestedContainer(keyedBy: MetaKeys.self, forKey: .meta)
let dataContainer = try metaContainer.nestedContainer(keyedBy: DataKeys.self, forKey: .data)
let siteContainer = try dataContainer.nestedContainer(keyedBy: SiteKeys.self, forKey: .site)
feedDescription = try? siteContainer.decode(String.self, forKey: .description)
let iconContainer = try siteContainer.nestedContainer(keyedBy: IconKeys.self, forKey: .icon)
blavatarURL = try? iconContainer.decode(URL.self, forKey: .img)
} catch {
}
self.feedDescription = feedDescription
self.blavatarURL = blavatarURL
}
}
extension ReaderFeed: CustomStringConvertible {
public var description: String {
return "<Feed | URL: \(url), title: \(title), feedID: \(String(describing: feedID)), blogID: \(String(describing: blogID))>"
}
}