2017-07-01 17:22:19 -07:00
|
|
|
//
|
|
|
|
// Author.swift
|
2019-07-08 22:58:19 -07:00
|
|
|
// NetNewsWire
|
2017-07-01 17:22:19 -07:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/1/17.
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2018-09-15 11:16:05 -07:00
|
|
|
public struct Author: Codable, Hashable {
|
2017-07-01 17:22:19 -07:00
|
|
|
|
2017-08-20 15:56:58 -07:00
|
|
|
public let authorID: String // calculated
|
2017-07-01 17:22:19 -07:00
|
|
|
public let name: String?
|
|
|
|
public let url: String?
|
|
|
|
public let avatarURL: String?
|
|
|
|
public let emailAddress: String?
|
2017-07-03 10:29:44 -07:00
|
|
|
|
2017-08-20 17:46:15 -07:00
|
|
|
public init?(authorID: String?, name: String?, url: String?, avatarURL: String?, emailAddress: String?) {
|
2017-07-01 17:22:19 -07:00
|
|
|
if name == nil && url == nil && emailAddress == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.name = name
|
|
|
|
self.url = url
|
|
|
|
self.avatarURL = avatarURL
|
|
|
|
self.emailAddress = emailAddress
|
|
|
|
|
2017-08-20 17:46:15 -07:00
|
|
|
if let authorID = authorID {
|
|
|
|
self.authorID = authorID
|
2017-07-16 19:36:38 -07:00
|
|
|
}
|
|
|
|
else {
|
2018-08-25 12:05:47 -07:00
|
|
|
var s = name ?? ""
|
|
|
|
s += url ?? ""
|
|
|
|
s += avatarURL ?? ""
|
|
|
|
s += emailAddress ?? ""
|
2017-08-20 17:46:15 -07:00
|
|
|
self.authorID = databaseIDWithString(s)
|
2017-07-13 13:38:47 -07:00
|
|
|
}
|
2017-07-01 17:22:19 -07:00
|
|
|
}
|
2017-12-02 14:20:58 -08:00
|
|
|
|
2018-09-15 11:16:05 -07:00
|
|
|
public static func authorsWithJSON(_ jsonString: String) -> Set<Author>? {
|
|
|
|
// This is JSON stored in the database, not the JSON Feed version of an author.
|
|
|
|
guard let data = jsonString.data(using: .utf8) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
do {
|
|
|
|
let authors = try decoder.decode([Author].self, from: data)
|
|
|
|
return Set(authors)
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
assertionFailure("JSON representation of Author array could not be decoded: \(jsonString) error: \(error)")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-02 14:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Set where Element == Author {
|
|
|
|
|
2018-09-15 11:16:05 -07:00
|
|
|
public func json() -> String? {
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
do {
|
|
|
|
let jsonData = try encoder.encode(Array(self))
|
|
|
|
return String(data: jsonData, encoding: .utf8)
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
assertionFailure("JSON representation of Author array could not be encoded: \(self) error: \(error)")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-01 17:22:19 -07:00
|
|
|
}
|