Skip to content
This repository was archived by the owner on Jan 22, 2021. It is now read-only.

Commit e835ba3

Browse files
committed
Cleaned code with swiftlint, changed name of app to Watson ML
1 parent 248373c commit e835ba3

20 files changed

+115
-151
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,3 @@ fastlane/test_output
7272
# The frameworks folder is where they should go, but keep the frameworks themselves out of Xcode
7373
Frameworks/*
7474

75-
**/WatsonMLClientCredentials.json
76-

Server/rainbow-server/.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
disabled_rules:
22
- line_length
3+
- trailing_whitespace
34
file_length:
45
warning: 600
56
error: 1000

Server/rainbow-server/Sources/Application/Constant.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import Foundation
99

1010
public struct Constant {
11-
12-
public static let NOTIFICATION_MESSAGE_KNOCKED_FROM_TOP: String = "You have been knocked from the top spot on the leaderboard. Play again to reclaim your spot!"
13-
public static let NOTIFICATION_MESSAGE_KNOCKED_FROM_TOP_TEN: String = "ou have been knocked from the top ten spots of the leaderboard. Play again to reclaim your spot!"
14-
11+
public static let knockedFromTop = "You have been knocked from the top spot on the leaderboard. Play again to reclaim your spot!"
12+
public static let knockedFromTopTen = "You have been knocked from the top ten spots of the leaderboard. Play again to reclaim your spot!"
1513
}

Server/rainbow-server/Sources/Application/EncodedPassword.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public class EncodedPassword {
3535
/// hashed password
3636
private let encodedPassword: [UInt8]
3737

38-
3938
///
4039
/// Initialize an EncodedPassword object
4140
///
@@ -49,7 +48,7 @@ public class EncodedPassword {
4948
usingSalt saltFromUser: [UInt8]? = nil, encoding: SecureEncoding) throws {
5049

5150
// Define our constants
52-
let mySaltLength:UInt = 32
51+
let mySaltLength: UInt = 32
5352
let myRoundsOfPBKDF: UInt32 = 2
5453

5554
let mySalt: [UInt8] = try saltFromUser ?? Random.generate(byteCount: Int(mySaltLength))
@@ -60,7 +59,7 @@ public class EncodedPassword {
6059
userName = name
6160
encodingType = encoding
6261

63-
switch (encoding) {
62+
switch encoding {
6463
case .PBKDF2:
6564
encodedPassword = try PBKDF.deriveKey(fromPassword: password, salt: mySalt,
6665
prf: .sha512, rounds: roundsOfPBKDF,
@@ -76,16 +75,14 @@ public class EncodedPassword {
7675
/// Verify the password passed with the input password
7776
/// this is done by encoding the input password and comparing the two encoded passwords.
7877
///
79-
public func verifyPassword(withPassword testPassword: String) throws -> Bool{
80-
let testPassword_encoded: [UInt8]
81-
switch (encodingType) {
78+
public func verifyPassword(withPassword testPassword: String) throws -> Bool {
79+
let testPasswordEncoded: [UInt8]
80+
switch encodingType {
8281
case .PBKDF2:
83-
testPassword_encoded = try PBKDF.deriveKey(fromPassword: testPassword, salt: salt,
84-
prf: .sha512, rounds: roundsOfPBKDF,
85-
derivedKeyLength: saltLength)
82+
testPasswordEncoded = try PBKDF.deriveKey(fromPassword: testPassword, salt: salt, prf: .sha512, rounds: roundsOfPBKDF, derivedKeyLength: saltLength)
8683
default:
8784
throw EncodedPasswordError.invalidEncoding
8885
}
89-
return ( encodedPassword == testPassword_encoded)
86+
return encodedPassword == testPasswordEncoded
9087
}
9188
}

Server/rainbow-server/Sources/Application/Model/Authentication.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Authentication: Codable {
1313
var password: String
1414
}
1515

16-
extension Authentication{
16+
extension Authentication {
1717
init(document: JSON) {
1818
username = document["username"].stringValue
1919
password = document["password"].stringValue

Server/rainbow-server/Sources/Application/Model/AuthenticationPersistence.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import CouchDB
1010
import LoggerAPI
1111
import SwiftyJSON
1212

13-
extension Authentication{
13+
extension Authentication {
1414
class Persistence {
1515
private static func getDatabase(from client: CouchDBClient, completion: @escaping (_ database: Database?, _ error: Error?) -> Void) {
1616
client.dbExists("routes-users") { exists, error in
@@ -27,7 +27,7 @@ extension Authentication{
2727
guard let database = database else {
2828
return completion(nil, error)
2929
}
30-
database.retrieveAll(includeDocuments: true, callback: { documents, retrieveError in
30+
database.retrieveAll(includeDocuments: true, callback: { documents, _ in
3131
guard let documents = documents else {
3232
return completion(nil, error)
3333
}

Server/rainbow-server/Sources/Application/Model/EntryPersistence.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ extension ScoreEntry {
3636
guard let updatedCopy = entryCopy.toJSONDocument() else {
3737
return completion(nil, RainbowPersistenceError.noAvatar)
3838
}
39-
database.create(updatedCopy, callback: { id, _, _, error in
40-
completion(id, error)
39+
database.create(updatedCopy, callback: { identifier, _, _, error in
40+
completion(identifier, error)
4141
})
4242
}
4343
}
4444

4545
// Update document
46-
static func update(id: String, entry: ScoreEntry, to client: CouchDBClient, completion: @escaping (_ entryID: String?, _ error: Error?) -> Void) {
46+
static func update(identifier: String, entry: ScoreEntry, to client: CouchDBClient, completion: @escaping (_ entryID: String?, _ error: Error?) -> Void) {
4747
getDatabase(from: client) { database, error in
4848
guard let database = database else {
4949
return completion(nil, error)
5050
}
5151

52-
database.retrieve(id, callback: { document, error in
52+
database.retrieve(identifier, callback: { document, error in
5353
guard let document = document else {
5454
return completion(nil, error)
5555
}
@@ -61,7 +61,7 @@ extension ScoreEntry {
6161
return completion(nil, RainbowPersistenceError.noAvatar)
6262
}
6363

64-
database.update(id, rev: document["_rev"].stringValue, document: updatedCopy, callback: { rev, _, error in
64+
database.update(identifier, rev: document["_rev"].stringValue, document: updatedCopy, callback: { rev, _, error in
6565
completion(rev, error)
6666
})
6767
})
@@ -77,7 +77,7 @@ extension ScoreEntry {
7777
guard let document = document else {
7878
return completion(nil, error)
7979
}
80-
return completion(ScoreEntry(document: document, _id: document["_id"].stringValue), nil)
80+
return completion(ScoreEntry(document: document, entryId: document["_id"].stringValue), nil)
8181
})
8282
}
8383
}
@@ -93,7 +93,7 @@ extension ScoreEntry {
9393
}
9494
var entries = [ScoreEntry]()
9595
for document in documents["rows"].arrayValue {
96-
if let newEntry = ScoreEntry(document: document["doc"], _id: document["doc"]["_id"].stringValue) {
96+
if let newEntry = ScoreEntry(document: document["doc"], entryId: document["doc"]["_id"].stringValue) {
9797
entries.append(newEntry)
9898
}
9999
}

Server/rainbow-server/Sources/Application/Model/ScoreEntry+SwiftyJSON.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import Foundation
99
import SwiftyJSON
1010

1111
extension ScoreEntry {
12-
init?(document: JSON, _id: String) {
13-
id = _id
12+
init?(document: JSON, entryId: String) {
13+
id = entryId
1414
deviceIdentifier = document["deviceIdentifier"].stringValue
1515
username = document["username"].stringValue
1616
startDate = document["startDate"].dateTime
@@ -42,7 +42,6 @@ extension ScoreEntry {
4242
totalTime = nil
4343
}
4444

45-
4645
mutating func toJSONDocument() -> JSON? {
4746
do {
4847
let encoded = try JSONEncoder().encode(self)

Server/rainbow-server/Sources/Application/Model/ScoreEntry.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ struct ObjectEntry: Codable {
1414
}
1515

1616
struct ScoreEntry: Codable {
17+
//swiftlint:disable identifier_name
1718
var id: String?
1819
var username: String
1920
var startDate: Date?
2021
var finishDate: Date?
2122
var deviceIdentifier: String?
22-
var avatarImage: Data?
23+
var avatarImage: Data?
2324
var objects: [ObjectEntry]?
2425
var totalTime: Double?
2526
}

Server/rainbow-server/Sources/Application/Routes/RouteAutheticator.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ func setupBasicAuth(app: App) {
4141
let basicCredentials = CredentialsHTTPBasic( verifyPassword: { userId, password, callback in
4242
if let user = userDB[userId] {
4343
do {
44-
let result: Bool = try user.verifyPassword(withPassword: password)
45-
if ( result ) {
44+
let result = try user.verifyPassword(withPassword: password)
45+
if result {
4646
Log.debug("Successfully authenticated!")
4747
callback(UserProfile(id: userId, displayName: userId, provider: "HTTPBasic-Kitura"))
4848
}
49-
}
50-
catch {
49+
} catch {
5150
Log.error("VerifyPassword internal error")
5251
}
5352
}
@@ -67,19 +66,17 @@ func setupBasicAuth(app: App) {
6766
response.headers["Content-Type"] = "application/json; charset=utf-8"
6867
do {
6968
// if userProfile exists, it means user logged in
70-
if let userProfile = request.userProfile {
69+
if let userProfile = request.userProfile {
7170
Log.debug("User Successfully Authenticated \(userProfile.id)")
7271
next()
7372
return
7473
}
75-
7674
// if 401 returned
7775
Log.debug("User Authentication failed")
78-
try response.status(.unauthorized).send(
79-
"You are not authorized to use this API"
80-
).end()
76+
try response.status(.unauthorized).send("You are not authorized to use this API").end()
77+
} catch {
78+
Log.error("Could not send unauthorized status.")
8179
}
82-
catch {}
8380
next()
8481
})
8582
}

0 commit comments

Comments
 (0)