Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rolivieri committed Dec 11, 2016
1 parent e33a59b commit 1e56fe9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 20 deletions.
85 changes: 84 additions & 1 deletion Sources/CloudFoundryEnv/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 13 additions & 19 deletions Sources/CloudFoundryEnv/AppEnv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 1e56fe9

Please sign in to comment.