Skip to content

Commit 994884c

Browse files
committed
refactoring - changing the way methods are created
1 parent a9bc7dc commit 994884c

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

Source/HTTPSession.swift

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,9 @@ public class HTTPSession {
184184
location = nil
185185
}
186186

187-
188-
let productInformation: ProductInformation?
189-
if let productInformationJSONObject = lightJSONObject["product"] as? NSDictionary, let name = productInformationJSONObject["name"] as? String, let manufaturer = productInformationJSONObject["company"] as? String {
190-
191-
let capabilities: Capabilities?
192-
if let capabilitiesJSONObject = productInformationJSONObject["capabilities"] as? NSDictionary {
193-
let hasColor = capabilitiesJSONObject["has_color"] as? Bool
194-
let hasIR = capabilitiesJSONObject["has_ir"] as? Bool
195-
let hasMultiZone = capabilitiesJSONObject["has_multizone"] as? Bool
196-
capabilities = Capabilities(hasColor: hasColor, hasIR: hasIR, hasMulitiZone: hasMultiZone)
197-
} else {
198-
capabilities = nil
199-
}
200-
productInformation = ProductInformation(productName: name, manufacturer: manufaturer, capabilities: capabilities)
201-
} else {
202-
productInformation = nil
187+
var productInformation: ProductInformation?
188+
if let productInformationJSONObject = lightJSONObject["product"] as? NSDictionary {
189+
productInformation = ProductInformation(data: productInformationJSONObject)
203190
}
204191

205192
let color = Color(hue: colorHue, saturation: colorSaturation, kelvin: colorKelvin)

Source/ProductInformation.swift

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,42 @@
99
import Foundation
1010

1111
public struct ProductInformation {
12-
public let productName: String?
13-
public let manufacturer: String?
12+
public let productName: String
13+
public let manufacturer: String
1414
public let capabilities: Capabilities?
15+
16+
public init?(data: NSDictionary) {
17+
guard let name = data["name"] as? String, let company = data["company"] as? String, let productCapabilities = data["capabilities"] as? NSDictionary else {
18+
return nil
19+
}
20+
productName = name
21+
manufacturer = company
22+
capabilities = Capabilities(data: productCapabilities)
23+
}
24+
25+
var description: String {
26+
return "Name: \(productName) - manufactured by \(manufacturer) Capabilities supported - \(String(describing: capabilities?.description))"
27+
}
1528
}
1629

1730
public struct Capabilities {
18-
public let hasColor: Bool?
19-
public let hasIR: Bool?
20-
public let hasMulitiZone: Bool?
31+
public let hasColor: Bool
32+
public let hasIR: Bool
33+
public let hasMulitiZone: Bool
34+
35+
public init?(data: NSDictionary) {
36+
guard let hasColor = data["has_color"] as? Bool,
37+
let hasIR = data["has_ir"] as? Bool, let multiZone = data["has_multizone"] as? Bool else {
38+
return nil
39+
}
40+
41+
self.hasColor = hasColor
42+
self.hasIR = hasIR
43+
self.hasMulitiZone = multiZone
44+
}
45+
46+
var description: String {
47+
return "supports color - \(hasColor), supports Infra-red \(hasIR), multiple zones - \(hasMulitiZone)"
48+
}
49+
2150
}

0 commit comments

Comments
 (0)