Created
February 20, 2020 12:51
-
-
Save anupamchugh/40919db8f197faeaee37c7fb39595407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct FetchComment: Publisher { | |
typealias Output = CommentItem | |
typealias Failure = Error | |
//1 | |
let id: Int | |
let nlTagger: NLTagger | |
func receive<S>(subscriber: S) where S: Subscriber, Failure == S.Failure, Output == S.Input { | |
let request = URLRequest(url: URL(string: "https://hacker-news.firebaseio.com/v0/item/\(id).json")!) | |
URLSession.DataTaskPublisher(request: request, session: URLSession.shared) | |
.map { $0.data } | |
.decode(type: CommentItem.self, decoder: JSONDecoder()) | |
.map{ | |
commentItem in | |
//2 | |
let data = Data(commentItem.text?.utf8 ?? "".utf8) | |
var commentString = commentItem.text | |
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) { | |
commentString = attributedString.string | |
} | |
//3 | |
self.nlTagger.string = commentString | |
var sentimentScore = "" | |
if let string = self.nlTagger.string{ | |
//4 | |
let (sentiment,_) = self.nlTagger.tag(at: string.startIndex, unit: .paragraph, scheme: .sentimentScore) | |
sentimentScore = sentiment?.rawValue ?? "" | |
} | |
//5 | |
let result = CommentItem(id: commentItem.id, text: commentString, sentimentScore: sentimentScore) | |
return result | |
} | |
.print() | |
.receive(subscriber: subscriber) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment