Skip to content

Commit

Permalink
Final Swift 3 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pietbrauer committed Sep 20, 2016
1 parent c01e363 commit e4521fa
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 68 deletions.
5 changes: 2 additions & 3 deletions TanukiKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@
PRODUCT_NAME = TanukiKit;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down Expand Up @@ -792,6 +792,7 @@
PRODUCT_NAME = TanukiKit;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -817,7 +818,6 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -838,7 +838,6 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
7 changes: 3 additions & 4 deletions TanukiKit/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct OAuthConfiguration: Configuration {
return OAuthRouter.authorize(self, redirectURI).URLRequest?.url
}

public func authorize(_ session: RequestKitURLSession = URLSession.shared, code: String, completion: @escaping (_ config: TokenConfiguration) -> Void) {
public func authorize(session: RequestKitURLSession = URLSession.shared, code: String, completion: @escaping (_ config: TokenConfiguration) -> Void) {
let request = OAuthRouter.accessToken(self, code, redirectURI).URLRequest
if let request = request {
let task = session.dataTask(with: request) { data, response, err in
Expand Down Expand Up @@ -80,9 +80,8 @@ public struct OAuthConfiguration: Configuration {
}

public func handleOpenURL(_ session: RequestKitURLSession = URLSession.shared, url: URL, completion: @escaping (_ config: TokenConfiguration) -> Void) {
let absoluteString: String? = url.absoluteString
if let code = absoluteString?.componentsSeparatedByString("=").last {
authorize(session, code: code) { (config) in
if let code = url.absoluteString.components(separatedBy: "=").last {
authorize(session: session, code: code) { (config) in
completion(config)
}
}
Expand Down
14 changes: 7 additions & 7 deletions TanukiKit/Time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ struct Time {
- [https://developer.apple.com/library/mac/qa/qa1480/_index.html](https://developer.apple.com/library/mac/qa/qa1480/_index.html)
- [https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html)
*/
private static var rfc3339DateFormatter: NSDateFormatter = {
let formatter = NSDateFormatter()
private static var rfc3339DateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
return formatter
}()

Expand All @@ -21,8 +21,8 @@ struct Time {
- parameter string: The string representation of the date
- returns: An `NSDate` with a successful parse, otherwise `nil`
*/
static func rfc3339Date(string: String?) -> NSDate? {
static func rfc3339Date(string: String?) -> Date? {
guard let string = string else { return nil }
return Time.rfc3339DateFormatter.dateFromString(string)
return Time.rfc3339DateFormatter.date(from: string)
}
}
}
52 changes: 25 additions & 27 deletions TanukiKit/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,31 @@ import RequestKit

// MARK: model
@objc open class User: NSObject {
public var name: String?
public var login: String?
open let id: Int
open var login: String?
open var state: String?
public var avatarURL: NSURL?
public var webURL: NSURL?
public var createdAt: NSDate?
public var isAdmin: Bool?
open var avatarURL: URL?
open var webURL: URL?
open var createdAt: Date?
open var isAdmin: Bool?
open var bio: String?
open var name: String?
open var location: String?
public var skype: String?
public var linkedin: String?
public var twitter: String?
public var websiteURL: NSURL?
public var lastSignInAt: NSDate?
public var confirmedAt: NSDate?
open var skype: String?
open var linkedin: String?
open var twitter: String?
open var websiteURL: URL?
open var lastSignInAt: Date?
open var confirmedAt: Date?
open var email: String?
public var themeId: Int?
public var colorSchemeId: Int?
public var projectsLimit: Int?
public var currentSignInAt: NSDate?
public var canCreateGroup: Bool?
public var canCreateProject: Bool?
public var twoFactorEnabled: Bool?
public var external: Bool?
open var themeId: Int?
open var colorSchemeId: Int?
open var projectsLimit: Int?
open var currentSignInAt: Date?
open var canCreateGroup: Bool?
open var canCreateProject: Bool?
open var twoFactorEnabled: Bool?
open var external: Bool?
open var privateToken: String?

public init(_ json: [String: Any]) {
Expand All @@ -38,29 +36,29 @@ import RequestKit
login = json["username"] as? String
self.id = id
state = json["state"] as? String
if let urlString = json["avatar_url"] as? String, url = NSURL(string: urlString) {
if let urlString = json["avatar_url"] as? String, let url = URL(string: urlString) {
avatarURL = url
}
if let urlString = json["web_url"] as? String, url = NSURL(string: urlString) {
if let urlString = json["web_url"] as? String, let url = URL(string: urlString) {
webURL = url
}
createdAt = Time.rfc3339Date(json["created_at"] as? String)
createdAt = Time.rfc3339Date(string: json["created_at"] as? String)
isAdmin = json["is_admin"] as? Bool
bio = json["bio"] as? String
location = json["location"] as? String
skype = json["skype"] as? String
linkedin = json["linkedin"] as? String
twitter = json["twitter"] as? String
if let urlString = json["website_url"] as? String, url = NSURL(string: urlString) {
if let urlString = json["website_url"] as? String, let url = URL(string: urlString) {
websiteURL = url
}
lastSignInAt = Time.rfc3339Date(json["last_sign_in_at"] as? String)
confirmedAt = Time.rfc3339Date(json["confirmed_at"] as? String)
lastSignInAt = Time.rfc3339Date(string: json["last_sign_in_at"] as? String)
confirmedAt = Time.rfc3339Date(string: json["confirmed_at"] as? String)
email = json["email"] as? String
themeId = json["theme_id"] as? Int
colorSchemeId = json["color_scheme_id"] as? Int
projectsLimit = json["projects_limit"] as? Int
currentSignInAt = Time.rfc3339Date(json["current_sign_in_at"] as? String)
currentSignInAt = Time.rfc3339Date(string: json["current_sign_in_at"] as? String)
canCreateGroup = json["can_create_group"] as? Bool
canCreateProject = json["can_create_project"] as? Bool
twoFactorEnabled = json["two_factor_enabled"] as? Bool
Expand Down
18 changes: 9 additions & 9 deletions TanukiKitTests/TestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ internal class TestHelper {
return nil
}

internal class func parseDate(date: String?) -> NSDate? {
let dateFormatter = NSDateFormatter()
internal class func parseDate(date: String?) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let dateOutput = dateFormatter.dateFromString(date!)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let dateOutput = dateFormatter.date(from: date!)
return dateOutput
}

internal class func JSONFromFile(name: String) -> AnyObject {
let bundle = NSBundle(forClass: self)
let path = bundle.pathForResource(name, ofType: "json")!
let data = NSData(contentsOfFile: path)!
let dict: AnyObject? = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
internal class func JSONFromFile(name: String) -> Any {
let bundle = Bundle(for: self)
let path = bundle.url(forResource: name, withExtension: "json")!
let data = try! Data(contentsOf: path)
let dict: Any? = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
return dict!
}
}
36 changes: 18 additions & 18 deletions TanukiKitTests/UserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class UserTests: XCTestCase {
func testGetUserCOM() {
let session = TanukiKitURLTestSession(expectedURL: "https://gitlab.com/api/v3/user?access_token=12345", expectedHTTPMethod: "GET", jsonFile: "User", statusCode: 200)
let config = TokenConfiguration("12345")
TanukiKit(config).me(session) { response in
_ = TanukiKit(config).me(session) { response in
switch response {
case .Success(let user):
case .success(let user):
XCTAssertEqual(user.login, "testmctestface")
case .Failure(let error):
case .failure(let error):
XCTAssert(false, "❌ Should not retrieve an error –> (\(error))")
}
}
Expand All @@ -22,11 +22,11 @@ class UserTests: XCTestCase {
func testGetUserEECE() {
let session = TanukiKitURLTestSession(expectedURL: "https://code.tiferrei.com/api/v3/user?private_token=12345", expectedHTTPMethod: "GET", jsonFile: "User", statusCode: 200)
let config = PrivateTokenConfiguration("12345", url: "https://code.tiferrei.com/api/v3/")
TanukiKit(config).me(session) { response in
_ = TanukiKit(config).me(session) { response in
switch response {
case .Success(let user):
case .success(let user):
XCTAssertEqual(user.login, "testmctestface")
case .Failure(let error):
case .failure(let error):
XCTAssert(false, "❌ Should not retrieve an error –> \(error)")
}
}
Expand All @@ -36,14 +36,14 @@ class UserTests: XCTestCase {
func testFailToGetUser() {
let json = "{\"message\":\"401 Unauthorized\"}"
let session = TanukiKitURLTestSession(expectedURL: "https://gitlab.com/api/v3/user", expectedHTTPMethod: "GET", response: json, statusCode: 401)
TanukiKit().me(session) { response in
_ = TanukiKit().me(session) { response in
switch response {
case .Success:
case .success:
XCTAssert(false, "❌ Should not retrieve user.")
case .Failure(let error as NSError):
case .failure(let error as NSError):
XCTAssertEqual(error.code, 401)
XCTAssertEqual(error.domain, TanukiKitErrorDomain)
case .Failure:
case .failure:
XCTAssertTrue(false)
}
}
Expand All @@ -53,27 +53,27 @@ class UserTests: XCTestCase {
// MARK: Model Tests

func testUserParsing() {
let subject = User(TestHelper.JSONFromFile("User") as! [String: AnyObject])
let subject = User(TestHelper.JSONFromFile(name: "User") as! [String: AnyObject])
XCTAssertEqual(subject.name, "Test McTestface")
XCTAssertEqual(subject.login, "testmctestface")
XCTAssertEqual(subject.id, 7)
XCTAssertEqual(subject.avatarURL, NSURL(string: "https://code.tiferrei.com/uploads/user/avatar/7/avatar.png"))
XCTAssertEqual(subject.webURL, NSURL(string: "https://code.tiferrei.com/u/testmctestface"))
XCTAssertEqual(subject.createdAt, TestHelper.parseDate("2016-05-03T15:05:46.391Z"))
XCTAssertEqual(subject.avatarURL, URL(string: "https://code.tiferrei.com/uploads/user/avatar/7/avatar.png"))
XCTAssertEqual(subject.webURL, URL(string: "https://code.tiferrei.com/u/testmctestface"))
XCTAssertEqual(subject.createdAt, TestHelper.parseDate(date: "2016-05-03T15:05:46.391Z"))
XCTAssertEqual(subject.isAdmin, false)
XCTAssertEqual(subject.bio, "I'm a simple test user that Tiago created to test the GitLab API.")
XCTAssertEqual(subject.location, "World Wide Web")
XCTAssertEqual(subject.skype, "testMcTestface")
XCTAssertEqual(subject.linkedin, "testMcTestface")
XCTAssertEqual(subject.twitter, "@testMcTestface")
XCTAssertEqual(subject.websiteURL, NSURL(string: "https://testmctestface.example.com"))
XCTAssertEqual(subject.lastSignInAt, TestHelper.parseDate("2016-05-03T15:06:21.305Z"))
XCTAssertEqual(subject.confirmedAt, TestHelper.parseDate("2016-05-03T15:05:46.183Z"))
XCTAssertEqual(subject.websiteURL, URL(string: "https://testmctestface.example.com"))
XCTAssertEqual(subject.lastSignInAt, TestHelper.parseDate(date: "2016-05-03T15:06:21.305Z"))
XCTAssertEqual(subject.confirmedAt, TestHelper.parseDate(date: "2016-05-03T15:05:46.183Z"))
XCTAssertEqual(subject.email, "EMAIL")
XCTAssertEqual(subject.themeId, 2)
XCTAssertEqual(subject.colorSchemeId, 1)
XCTAssertEqual(subject.projectsLimit, 10)
XCTAssertEqual(subject.currentSignInAt, TestHelper.parseDate("2016-06-26T15:29:16.606Z"))
XCTAssertEqual(subject.currentSignInAt, TestHelper.parseDate(date: "2016-06-26T15:29:16.606Z"))
XCTAssertEqual(subject.canCreateGroup, false)
XCTAssertEqual(subject.canCreateProject, true)
XCTAssertEqual(subject.twoFactorEnabled, false)
Expand Down

0 comments on commit e4521fa

Please sign in to comment.