Stub out support for Twitter entities

This commit is contained in:
Maurice Parker 2020-04-19 19:19:39 -05:00
parent d0d29fd7da
commit bb61b15265
5 changed files with 58 additions and 4 deletions

View File

@ -8,6 +8,28 @@
import Foundation
protocol TwitterEntity {
var indices: [Int]? { get }
func renderAsHTML() -> String
}
extension TwitterEntity {
var startIndex: Int? {
if indices?.count ?? 0 > 0 {
return indices?[0]
}
return nil
}
var endIndex: Int? {
if indices?.count ?? 0 > 1 {
return indices?[1]
}
return nil
}
}
struct TwitterEntities: Codable {
let hashtags: [TwitterHashtag]?
@ -22,4 +44,21 @@ struct TwitterEntities: Codable {
case symbols = "symbols"
}
func combineAndSort() -> [TwitterEntity] {
var entities = [TwitterEntity]()
if let hashtags = hashtags {
entities.append(contentsOf: hashtags)
}
if let urls = urls {
entities.append(contentsOf: urls)
}
if let userMentions = userMentions {
entities.append(contentsOf: userMentions)
}
if let symbols = symbols {
entities.append(contentsOf: symbols)
}
return entities
}
}

View File

@ -8,7 +8,7 @@
import Foundation
struct TwitterHashtag: Codable {
struct TwitterHashtag: Codable, TwitterEntity {
let text: String?
let indices: [Int]?
@ -18,4 +18,7 @@ struct TwitterHashtag: Codable {
case indices = "indices"
}
func renderAsHTML() -> String {
return ""
}
}

View File

@ -8,7 +8,7 @@
import Foundation
struct TwitterMention: Codable {
struct TwitterMention: Codable, TwitterEntity {
let name: String?
let indices: [Int]?
@ -24,4 +24,8 @@ struct TwitterMention: Codable {
case idStr = "idStr"
}
func renderAsHTML() -> String {
return ""
}
}

View File

@ -8,7 +8,7 @@
import Foundation
struct TwitterSymbol: Codable {
struct TwitterSymbol: Codable, TwitterEntity {
let name: String?
let indices: [Int]?
@ -18,4 +18,8 @@ struct TwitterSymbol: Codable {
case indices = "indices"
}
func renderAsHTML() -> String {
return ""
}
}

View File

@ -8,7 +8,7 @@
import Foundation
struct TwitterURL: Codable {
struct TwitterURL: Codable, TwitterEntity {
let url: String?
let indices: [Int]?
@ -22,4 +22,8 @@ struct TwitterURL: Codable {
case expandedURL = "expandedURL"
}
func renderAsHTML() -> String {
return ""
}
}