2017-07-02 02:22:19 +02:00
|
|
|
//
|
|
|
|
// Author.swift
|
|
|
|
// DataModel
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/1/17.
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2018-09-15 20:16:05 +02:00
|
|
|
public struct Author: Codable, Hashable {
|
2017-07-02 02:22:19 +02:00
|
|
|
|
2017-08-21 00:56:58 +02:00
|
|
|
public let authorID: String // calculated
|
2017-07-02 02:22:19 +02:00
|
|
|
public let name: String?
|
|
|
|
public let url: String?
|
|
|
|
public let avatarURL: String?
|
|
|
|
public let emailAddress: String?
|
2017-07-03 19:29:44 +02:00
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
public init?(authorID: String?, name: String?, url: String?, avatarURL: String?, emailAddress: String?) {
|
2017-07-02 02:22:19 +02:00
|
|
|
if name == nil && url == nil && emailAddress == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.name = name
|
|
|
|
self.url = url
|
|
|
|
self.avatarURL = avatarURL
|
|
|
|
self.emailAddress = emailAddress
|
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
if let authorID = authorID {
|
|
|
|
self.authorID = authorID
|
2017-07-17 04:36:38 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-08-25 21:05:47 +02:00
|
|
|
var s = name ?? ""
|
|
|
|
s += url ?? ""
|
|
|
|
s += avatarURL ?? ""
|
|
|
|
s += emailAddress ?? ""
|
2017-08-21 02:46:15 +02:00
|
|
|
self.authorID = databaseIDWithString(s)
|
2017-07-13 22:38:47 +02:00
|
|
|
}
|
2017-07-02 02:22:19 +02:00
|
|
|
}
|
2017-12-02 23:20:58 +01:00
|
|
|
|
2018-09-15 20:16:05 +02: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 23:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Set where Element == Author {
|
|
|
|
|
2018-09-15 20:16:05 +02: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-02 02:22:19 +02:00
|
|
|
}
|