OSDN Git Service

APIResponseのENABLE_JSON_LOG部分を移動
[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 class JSONViewCommand: JSONCommand {
14     let jsonTree: [JSONNode]?
15     let parameterList: [Any]
16     let recieveDate: Date?
17     let command: JSONCommand
18     
19     init(apiResponse: APIResponse, command: JSONCommand) {
20         self.recieveDate = Date()
21         self.parameterList = apiResponse
22             .parameter
23             .map { ["key": $0.0, "value": $0.1] }
24         self.jsonTree = JSONNode
25             .nodeWithJSON(apiResponse.json as AnyObject?)
26             .map { [$0] }
27         self.command = command
28         super.init(apiResponse: apiResponse)
29     }
30     
31     required init(apiResponse: APIResponse) {
32         fatalError("use init(apiResponse:command:)")
33     }
34     
35     override func execute() {
36         do {
37             try command.execute()
38         }
39         catch {
40             print("JSONTracker Cought Exception -> \(error)")
41         }
42         
43         guard let _ = jsonTree else { return print("jsonTree is nil.") }
44         guard let _ = recieveDate else { return print("recieveDate is nil.") }
45         
46         DispatchQueue.main.async {
47             let commands: [String: Any] = [
48                 "api": self.api,
49                 "argument": self.parameterList,
50                 "json": self.jsonTree ?? [],
51                 "recieveDate": self.recieveDate ?? Date(),
52                 "date": Date()
53              ]
54             AppDelegate.shared.jsonViewWindowController?.setCommand(commands)
55         }
56     }
57 }
58
59 #endif