OSDN Git Service

9a9cf02bfc17ddd82ed208b418d7a388e4c7da18
[kcd/KCD.git] / KCD / JSONViewCommand.swift
1 //
2 //  JSONViewCommand.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/19.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 #if ENABLE_JSON_LOG
12
13 final class JSONViewCommand: JSONCommand {
14     
15     let jsonTree: [JSONNode]?
16     let parameterList: [Any]
17     let recieveDate: Date?
18     let command: JSONCommand
19     
20     init(apiResponse: APIResponse, command: JSONCommand) {
21         
22         self.recieveDate = Date()
23         self.parameterList = apiResponse
24             .parameter
25            .map { ["key": $0, "value": $1] }
26         self.jsonTree = JSONNode
27             .nodeWithJSON(apiResponse.json)
28             .map { [$0] }
29         self.command = command
30         
31         super.init(apiResponse: apiResponse)
32     }
33     
34     required init(apiResponse: APIResponse) {
35         
36         fatalError("use init(apiResponse:command:)")
37     }
38     
39     override func execute() {
40         
41         do {
42             
43             try command.execute()
44             
45         } catch {
46             
47             print("JSONTracker Cought Exception -> \(error)")
48         }
49         
50         guard let _ = jsonTree else { return Logger.shared.log("jsonTree is nil.") }
51         guard let _ = recieveDate else { return Logger.shared.log("recieveDate is nil.") }
52         
53         DispatchQueue.main.async {
54             
55             let commands: [String: Any] = [
56                 "api": self.api,
57                 "argument": self.parameterList,
58                 "json": self.jsonTree ?? [],
59                 "recieveDate": self.recieveDate ?? Date(),
60                 "date": Date()
61              ]
62             AppDelegate.shared.jsonViewWindowController?.setCommand(commands)
63         }
64     }
65 }
66
67 #endif