Skip to content

Commit

Permalink
Migrate to Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Piñera Buendía committed Dec 10, 2016
1 parent 441d973 commit 6714423
Show file tree
Hide file tree
Showing 21 changed files with 152 additions and 137 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
5 changes: 5 additions & 0 deletions Example/Paparajote.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
TargetAttributes = {
23A5DC781D9461E90036CC9F = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0810;
};
607FACE41AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
Expand Down Expand Up @@ -454,6 +455,7 @@
PRODUCT_BUNDLE_IDENTIFIER = io.caramba.macOS;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -471,6 +473,7 @@
PRODUCT_BUNDLE_IDENTIFIER = io.caramba.macOS;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down Expand Up @@ -574,6 +577,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -590,6 +594,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use_frameworks!
target 'Paparajote_Tests' do
pod 'Paparajote', :path => '../'
pod 'Quick', '~> 0.8'
pod 'Nimble', '~> 3.0'
pod 'Quick'
pod 'Nimble'
end


Expand Down
20 changes: 10 additions & 10 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
PODS:
- Nimble (3.2.0)
- Nimble (5.1.1)
- NSURL+QueryDictionary (1.2.0)
- Paparajote (0.1.9):
- Paparajote (0.1.12):
- NSURL+QueryDictionary (~> 1.2)
- Quick (0.9.3)
- Quick (1.0.0)

DEPENDENCIES:
- Nimble (~> 3.0)
- Nimble
- Paparajote (from `../`)
- Quick (~> 0.8)
- Quick

EXTERNAL SOURCES:
Paparajote:
:path: "../"

SPEC CHECKSUMS:
Nimble: 703854335d181df169bbca9c97117b5cf8c47c1d
Nimble: 415e3aa3267e7bc2c96b05fa814ddea7bb686a29
NSURL+QueryDictionary: bae616404e2adf6409d3d5c02a093cbf44c8a236
Paparajote: 73a998abf9d589e1d621c1fbea0ce3f3dc667fed
Quick: 13a2a2b19a5d8e3ed4fd0c36ee46597fd77ebf71
Paparajote: e7f0435daf10bfba55853c9304ef159f23028784
Quick: 8024e4a47e6cc03a9d5245ef0948264fc6d27cff

PODFILE CHECKSUM: 0fdb5dfe549b46de27104f3bec2b2fd56ac93485
PODFILE CHECKSUM: 3d26cb39d1450a4534259f2162e0106af1a81fd1

COCOAPODS: 1.1.0.rc.2
COCOAPODS: 1.1.1
4 changes: 2 additions & 2 deletions Example/macOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!


func applicationDidFinishLaunching(aNotification: NSNotification) {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}

func applicationWillTerminate(aNotification: NSNotification) {
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "cocoapods", "1.1.0rc2"
gem "cocoapods"
gem "fastlane"

# Danger
Expand Down
20 changes: 10 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ GEM
cork
nap
open4 (~> 1.3)
cocoapods (1.1.0.rc.2)
cocoapods (1.1.1)
activesupport (>= 4.0.2, < 5)
claide (>= 1.0.0, < 2.0)
cocoapods-core (= 1.1.0.rc.2)
claide (>= 1.0.1, < 2.0)
cocoapods-core (= 1.1.1)
cocoapods-deintegrate (>= 1.0.1, < 2.0)
cocoapods-downloader (>= 1.1.1, < 2.0)
cocoapods-downloader (>= 1.1.2, < 2.0)
cocoapods-plugins (>= 1.0.0, < 2.0)
cocoapods-search (>= 1.0.0, < 2.0)
cocoapods-stats (>= 1.0.0, < 2.0)
cocoapods-trunk (>= 1.0.0, < 2.0)
cocoapods-trunk (>= 1.1.1, < 2.0)
cocoapods-try (>= 1.1.0, < 2.0)
colored (~> 1.2)
escape (~> 0.0.4)
fourflusher (~> 1.0.1)
fourflusher (~> 2.0.1)
gh_inspector (~> 1.0)
molinillo (~> 0.5.1)
nap (~> 1.0)
xcodeproj (>= 1.3.1, < 2.0)
cocoapods-core (1.1.0.rc.2)
xcodeproj (>= 1.3.3, < 2.0)
cocoapods-core (1.1.1)
activesupport (>= 4.0.2, < 5)
fuzzy_match (~> 2.0.4)
nap (~> 1.0)
Expand Down Expand Up @@ -144,7 +144,7 @@ GEM
plist (>= 3.1.0, < 4.0.0)
rubyzip (~> 1.1.6)
terminal-table (>= 1.4.5, < 2.0.0)
fourflusher (1.0.1)
fourflusher (2.0.1)
frameit (3.0.0)
deliver (> 0.3)
fastimage (~> 1.6.3)
Expand Down Expand Up @@ -319,7 +319,7 @@ PLATFORMS
ruby

DEPENDENCIES
cocoapods (= 1.1.0rc2)
cocoapods
danger
danger-junit
danger-the_coding_love
Expand Down
22 changes: 11 additions & 11 deletions Paparajote/Classes/Controllers/OAuth2Controller.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public class OAuth2Controller {
open class OAuth2Controller {

// MARK: - Attributes

Expand All @@ -23,34 +23,34 @@ public class OAuth2Controller {

// MARK: - Pubic

public func start() throws {
open func start() throws {
if self.inProgress {
throw OAuth2Error.AlreadyStarted
throw OAuth2Error.alreadyStarted
}
self.inProgress = true
self.delegate?.oauth(event: .Open(url: self.provider.authorization()))
self.delegate?.oauth(event: .open(url: self.provider.authorization()))
}

public func shouldRedirect(url url: NSURL) -> Bool {
open func shouldRedirect(url: URL) -> Bool {
guard let request = self.provider.authentication(url) else { return true }
self.authenticate(request: request)
return false
}

// MARK: - Private

private func authenticate(request request: NSURLRequest) {
fileprivate func authenticate(request: URLRequest) {
self.service.execute(request) { [weak self] (data, response, error) in
if let data = data, response = response {
if let data = data, let response = response {
if let session = self?.provider.sessionAdapter(data, response) {
self?.delegate?.oauth(event: .Session(session))
self?.delegate?.oauth(event: .session(session))
} else {
self?.delegate?.oauth(event: .Error(OAuth2Error.SessionNotFound))
self?.delegate?.oauth(event: .error(OAuth2Error.sessionNotFound))
}
} else if let error = error {
self?.delegate?.oauth(event: .Error(error))
self?.delegate?.oauth(event: .error(error))
} else {
self?.delegate?.oauth(event: .Error(OAuth2Error.NoResponse))
self?.delegate?.oauth(event: .error(OAuth2Error.noResponse))
}
self?.inProgress = false
}
Expand Down
2 changes: 1 addition & 1 deletion Paparajote/Classes/Controllers/OAuth2Delegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public protocol OAuth2Delegate: class {
- parameter event: Eventn to be processed.
*/
func oauth(event event: OAuth2Event)
func oauth(event: OAuth2Event)

}
14 changes: 7 additions & 7 deletions Paparajote/Classes/Errors/OAuth2Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import Foundation
- NoResponse: We didn't get any response when trying to authenticate the user.
- SessionNotFound: The session couldn't be found from the authentication endpoint response.
*/
public enum OAuth2Error: ErrorType {
case AlreadyStarted
case NoResponse
case SessionNotFound
public enum OAuth2Error: Error {
case alreadyStarted
case noResponse
case sessionNotFound
}

// MARK: - CustomStringConvertible
Expand All @@ -19,11 +19,11 @@ extension OAuth2Error: CustomStringConvertible {

public var description: String {
switch self {
case .AlreadyStarted:
case .alreadyStarted:
return "Oauth2 flow already started."
case .NoResponse:
case .noResponse:
return "No response from the provider."
case .SessionNotFound:
case .sessionNotFound:
return "Session not found"
}
}
Expand Down
4 changes: 2 additions & 2 deletions Paparajote/Classes/Helpers/String+Randomizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import Foundation

internal extension String {

static func random(length: Int = 10) -> String {
static func random(_ length: Int = 10) -> String {
let allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let allowedCharsCount = UInt32(allowedChars.characters.count)
var randomString = ""
for _ in (0..<length) {
let randomNum = Int(arc4random_uniform(allowedCharsCount))
let newCharacter = allowedChars[allowedChars.startIndex.advancedBy(randomNum)]
let newCharacter = allowedChars[allowedChars.characters.index(allowedChars.startIndex, offsetBy: randomNum)]
randomString += String(newCharacter)
}
return randomString
Expand Down
24 changes: 10 additions & 14 deletions Paparajote/Classes/Models/OAuth2Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,32 @@ import Foundation
- Session: User was authenticated and the session is provided.
*/
public enum OAuth2Event: Equatable {
case Open(url: NSURL)
case Error(ErrorType)
case Session(OAuth2Session)
case open(url: URL)
case error(Error)
case session(OAuth2Session)
}

// MARK: - <Equatable>

public func == (lhs: OAuth2Event, rhs: OAuth2Event) -> Bool {
switch lhs {
case .Open(let lhsUrl):
case .open(let lhsUrl):
switch rhs {
case .Open(let rhsUrl):
case .open(let rhsUrl):
return lhsUrl == rhsUrl
default:
return false
}
case .Error(let lhsError):
case .error(let lhsError):
switch rhs {
case .Error(let rhsError):
if let lhsEquatableError = lhsError as? NSError,
rhsEquatableError = rhsError as? NSError {
return lhsEquatableError == rhsEquatableError
}
return false
case .error(let rhsError):
return lhsError as NSError == rhsError as NSError
default:
return false
}
case .Session(let lhsSession):
case .session(let lhsSession):
switch rhs {
case .Session(let rhsSession):
case .session(let rhsSession):
return lhsSession == rhsSession
default:
return false
Expand Down
32 changes: 16 additions & 16 deletions Paparajote/Classes/Platforms/OAuth2WKNavigationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import WebKit

/// WKNavigationDelegate that handles the OAuth2 flow.
@available(OSX 10.10, *)
@objc public class OAuth2WKNavigationDelegate: NSObject, WKNavigationDelegate, OAuth2Delegate {
@objc open class OAuth2WKNavigationDelegate: NSObject, WKNavigationDelegate, OAuth2Delegate {

// MARK: - Attributes

private let provider: OAuth2Provider
private var controller: OAuth2Controller!
private weak var webView: WKWebView?
private let completion: OAuth2SessionCompletion
fileprivate let provider: OAuth2Provider
fileprivate var controller: OAuth2Controller!
fileprivate weak var webView: WKWebView?
fileprivate let completion: OAuth2SessionCompletion

// MARK: - Init

Expand All @@ -24,7 +24,7 @@ import WebKit
- returns: Initialized instance of OAuth2WKNavigationDelegate.
*/
internal init(provider: OAuth2Provider, webView: WKWebView, completion: OAuth2SessionCompletion) {
internal init(provider: OAuth2Provider, webView: WKWebView, completion: @escaping OAuth2SessionCompletion) {
self.provider = provider
self.webView = webView
self.completion = completion
Expand All @@ -37,7 +37,7 @@ import WebKit
- throws: It throws an exception if this method is called again once the flow has started.
*/
public func start() throws {
open func start() throws {
if self.controller == nil {
self.controller = OAuth2Controller(provider: self.provider, delegate: self)
}
Expand All @@ -46,24 +46,24 @@ import WebKit

// MARK: - <WKNavigationDelegate>

public func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.URL else {
decisionHandler(WKNavigationActionPolicy.Allow)
open func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url else {
decisionHandler(WKNavigationActionPolicy.allow)
return
}
let shouldRedirect = self.controller.shouldRedirect(url: url)
decisionHandler(shouldRedirect ? .Allow : .Cancel)
decisionHandler(shouldRedirect ? .allow : .cancel)
}

// MARK: - <OAuth2Delegate>

public func oauth(event event: OAuth2Event) {
open func oauth(event: OAuth2Event) {
switch event {
case .Error(let error):
case .error(let error):
self.completion(nil, error)
case .Open(let url):
self.webView?.loadRequest(NSURLRequest(URL: url))
case .Session(let session):
case .open(let url):
_ = self.webView?.load(URLRequest(url: url))
case .session(let session):
self.completion(session, nil)
}
}
Expand Down
Loading

0 comments on commit 6714423

Please sign in to comment.