forked from insidegui/WWDC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscriptsJSONAdapter.swift
More file actions
67 lines (48 loc) · 1.97 KB
/
TranscriptsJSONAdapter.swift
File metadata and controls
67 lines (48 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// TranscriptsJSONAdapter.swift
// WWDC
//
// Created by Guilherme Rambo on 20/02/17.
// Copyright © 2017 Guilherme Rambo. All rights reserved.
//
import Foundation
import SwiftyJSON
private enum TranscriptKeys: String, JSONSubscriptType {
case year, number, transcript, timecodes, annotations
var jsonKey: JSONKey {
return JSONKey.key(rawValue)
}
}
final class TranscriptsJSONAdapter: Adapter {
typealias InputType = JSON
typealias OutputType = Transcript
func adapt(_ input: JSON) -> Result<Transcript, AdapterError> {
guard let year = input[TranscriptKeys.year].int else {
return .error(.missingKey(TranscriptKeys.year))
}
guard let number = input[TranscriptKeys.number].int else {
return .error(.missingKey(TranscriptKeys.number))
}
// guard let fullText = input[TranscriptKeys.transcript].string else {
// return .error(.missingKey(TranscriptKeys.transcript))
// }
guard let timecodes = input[TranscriptKeys.timecodes].array else {
return .error(.missingKey(TranscriptKeys.timecodes))
}
guard let annotationsJson = input[TranscriptKeys.annotations].array else {
return .error(.missingKey(TranscriptKeys.annotations))
}
let annotations: [TranscriptAnnotation] = zip(timecodes, annotationsJson).flatMap { time, body in
guard let time = time.double, let body = body.string else { return nil }
let annotation = TranscriptAnnotation()
annotation.timecode = time
annotation.body = body
return annotation
}
let transcript = Transcript()
transcript.identifier = "\(year)-\(number)"
// transcript.fullText = fullText
transcript.annotations.append(objectsIn: annotations)
return .success(transcript)
}
}