From 1e56fe942073ebb6e9cdd0a38927882771a2ff31 Mon Sep 17 00:00:00 2001 From: rolivieri Date: Sat, 10 Dec 2016 19:21:53 -0600 Subject: [PATCH] refactoring --- Sources/CloudFoundryEnv/App.swift | 85 +++++++++++++++++++++++++++- Sources/CloudFoundryEnv/AppEnv.swift | 32 +++++------ 2 files changed, 97 insertions(+), 20 deletions(-) diff --git a/Sources/CloudFoundryEnv/App.swift b/Sources/CloudFoundryEnv/App.swift index 5b97a30..c20b420 100644 --- a/Sources/CloudFoundryEnv/App.swift +++ b/Sources/CloudFoundryEnv/App.swift @@ -21,6 +21,89 @@ */ public struct App { + public class Builder { + var id: String? + var name: String? + var uris: [String]? + var version: String? + var instanceId: String? + var instanceIndex: Int? + var limits: Limits? + var port: Int? + var spaceId: String? + var startedAt: Date? + + init() {} + + func setId(id: String?) -> Builder { + self.id = id + return self + } + + func setName(name: String?) -> Builder { + self.name = name + return self + } + + func setUris(uris: [String]) -> Builder { + self.uris = uris + return self + } + + func setVersion(version: String?) -> Builder { + self.version = version + return self + } + + func setInstanceId(instanceId: String?) -> Builder { + self.instanceId = instanceId + return self + } + + func setInstanceIndex(instanceIndex: Int?) -> Builder { + self.instanceIndex = instanceIndex + return self + } + + func setLimits(limits: Limits) -> Builder { + self.limits = limits + return self + } + + func setPort(port: Int?) -> Builder { + self.port = port + return self + } + + func setSpaceId(spaceId: String?) -> Builder { + self.spaceId = spaceId + return self + } + + func setStartedAt(startedAt: Date?) -> Builder { + self.startedAt = startedAt + return self + } + + func build() -> App? { + guard let id = id, let name = name, + let uris = uris, let version = version, + let instanceId = instanceId, + let instanceIndex = instanceIndex, + let limits = limits, + let port = port, + let spaceId = spaceId, + let startedAt = startedAt else { + return nil + } + + let startedAtTs = startedAt.timeIntervalSince1970 + return App(id: id, name: name, uris: uris, version: version, instanceId: instanceId, + instanceIndex: instanceIndex, limits: limits, port: port, spaceId: spaceId, + startedAtTs: startedAtTs, startedAt: startedAt) + } + } + public struct Limits { let memory: Int let disk: Int @@ -49,7 +132,7 @@ public struct App { * Constructor. */ // swiftlint:disable function_parameter_count - public init(id: String, name: String, uris: [String], version: String, + private init(id: String, name: String, uris: [String], version: String, instanceId: String, instanceIndex: Int, limits: Limits, port: Int, spaceId: String, startedAtTs: TimeInterval, startedAt: Date) { // swiftlint:enable function_parameter_count diff --git a/Sources/CloudFoundryEnv/AppEnv.swift b/Sources/CloudFoundryEnv/AppEnv.swift index 7f117e3..31c9512 100644 --- a/Sources/CloudFoundryEnv/AppEnv.swift +++ b/Sources/CloudFoundryEnv/AppEnv.swift @@ -73,29 +73,23 @@ public struct AppEnv { } // Get uris - let uris = app["uris"] as? [String] + let uris = JSONUtils.convertJSONArrayToStringArray(json: app, fieldName: "uris") // Create DateUtils instance let dateUtils = DateUtils() - guard - let name = app["application_name"] as? String, - let id = app["application_id"] as? String, - let version = app["version"] as? String, - let instanceId = app["instance_id"] as? String, - let instanceIndex = app["instance_index"] as? Int, - let port = app["port"] as? Int, - let startedAt: Date = dateUtils.convertStringToNSDate(dateString: app["started_at"] as? String), - let spaceId = app["space_id"] as? String else { - return nil - } - - let startedAtTs = startedAt.timeIntervalSince1970 - // App instance should only be created if all required variables exist - let appObj = App(id: id, name: name, uris: uris!, version: version, - instanceId: instanceId, instanceIndex: instanceIndex, - limits: limits, port: port, spaceId: spaceId, - startedAtTs: startedAtTs, startedAt: startedAt) + let appObj = App.Builder() + .setId(id: app["application_id"] as? String) + .setName(name: app["application_name"] as? String) + .setUris(uris: uris) + .setVersion(version: app["version"] as? String) + .setInstanceId(instanceId: app["instance_id"] as? String) + .setInstanceIndex(instanceIndex: app["instance_index"] as? Int) + .setLimits(limits: limits) + .setPort(port: app["port"] as? Int) + .setSpaceId(spaceId: app["space_id"] as? String) + .setStartedAt(startedAt: dateUtils.convertStringToNSDate(dateString: app["started_at"] as? String)) + .build() return appObj }