2018-09-11 14:52:21 +00:00
|
|
|
//
|
|
|
|
// Card.swift
|
|
|
|
// Pachyderm
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/9/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2022-02-04 04:11:29 +00:00
|
|
|
import WebURL
|
2018-09-11 14:52:21 +00:00
|
|
|
|
2020-10-25 20:05:28 +00:00
|
|
|
public class Card: Codable {
|
2022-02-04 04:11:29 +00:00
|
|
|
public let url: WebURL
|
2018-09-11 14:52:21 +00:00
|
|
|
public let title: String
|
|
|
|
public let description: String
|
2022-02-04 04:11:29 +00:00
|
|
|
public let image: WebURL?
|
2018-09-11 14:52:21 +00:00
|
|
|
public let kind: Kind
|
|
|
|
public let authorName: String?
|
2022-02-04 04:11:29 +00:00
|
|
|
public let authorURL: WebURL?
|
2018-09-11 14:52:21 +00:00
|
|
|
public let providerName: String?
|
2022-02-04 04:11:29 +00:00
|
|
|
public let providerURL: WebURL?
|
2018-09-11 14:52:21 +00:00
|
|
|
public let html: String?
|
|
|
|
public let width: Int?
|
|
|
|
public let height: Int?
|
2020-10-25 20:05:28 +00:00
|
|
|
public let blurhash: String?
|
2022-04-02 15:36:45 +00:00
|
|
|
/// Only present when returned from the trending links endpoint
|
|
|
|
public let history: [History]?
|
2018-09-11 14:52:21 +00:00
|
|
|
|
2020-03-05 02:14:58 +00:00
|
|
|
public required init(from decoder: Decoder) throws {
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
2022-02-04 04:11:29 +00:00
|
|
|
self.url = try container.decode(WebURL.self, forKey: .url)
|
2020-03-05 02:14:58 +00:00
|
|
|
self.title = try container.decode(String.self, forKey: .title)
|
|
|
|
self.description = try container.decode(String.self, forKey: .description)
|
|
|
|
self.kind = try container.decode(Kind.self, forKey: .kind)
|
2022-02-04 04:11:29 +00:00
|
|
|
self.image = try? container.decodeIfPresent(WebURL.self, forKey: .image)
|
2020-10-25 20:05:28 +00:00
|
|
|
self.authorName = try? container.decodeIfPresent(String.self, forKey: .authorName)
|
2022-02-04 04:11:29 +00:00
|
|
|
self.authorURL = try? container.decodeIfPresent(WebURL.self, forKey: .authorURL)
|
2020-10-25 20:05:28 +00:00
|
|
|
self.providerName = try? container.decodeIfPresent(String.self, forKey: .providerName)
|
2022-02-04 04:11:29 +00:00
|
|
|
self.providerURL = try? container.decodeIfPresent(WebURL.self, forKey: .providerURL)
|
2020-10-25 20:05:28 +00:00
|
|
|
self.html = try? container.decodeIfPresent(String.self, forKey: .html)
|
|
|
|
self.width = try? container.decodeIfPresent(Int.self, forKey: .width)
|
|
|
|
self.height = try? container.decodeIfPresent(Int.self, forKey: .height)
|
|
|
|
self.blurhash = try? container.decodeIfPresent(String.self, forKey: .blurhash)
|
2022-04-02 15:36:45 +00:00
|
|
|
self.history = try? container.decodeIfPresent([History].self, forKey: .history)
|
2020-10-25 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public func encode(to encoder: Encoder) throws {
|
|
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
|
|
|
try container.encode(url, forKey: .url)
|
|
|
|
try container.encode(title, forKey: .title)
|
|
|
|
try container.encode(description, forKey: .description)
|
|
|
|
try container.encode(kind, forKey: .kind)
|
|
|
|
try container.encode(image, forKey: .image)
|
|
|
|
try container.encode(blurhash, forKey: .blurhash)
|
2020-03-05 02:14:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 14:52:21 +00:00
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
|
|
case url
|
|
|
|
case title
|
|
|
|
case description
|
|
|
|
case image
|
|
|
|
case kind = "type"
|
|
|
|
case authorName = "author_name"
|
|
|
|
case authorURL = "author_url"
|
|
|
|
case providerName = "provider_name"
|
|
|
|
case providerURL = "provider_url"
|
|
|
|
case html
|
|
|
|
case width
|
|
|
|
case height
|
2020-10-25 20:05:28 +00:00
|
|
|
case blurhash
|
2022-04-02 15:36:45 +00:00
|
|
|
case history
|
2018-09-11 14:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Card {
|
2020-10-25 20:05:28 +00:00
|
|
|
public enum Kind: String, Codable {
|
2018-09-11 14:52:21 +00:00
|
|
|
case link
|
|
|
|
case photo
|
|
|
|
case video
|
|
|
|
}
|
|
|
|
}
|