From d35bc6639fa5df4bdd227a673b553cf134dc139c Mon Sep 17 00:00:00 2001 From: Khoa Pham Date: Mon, 31 Jul 2017 12:54:30 +0200 Subject: [PATCH 1/4] Add parse(urn). Auto perform percent encoding when dealing with urn --- Sources/Navigator.swift | 36 +++++++++++--------------------- Tests/Compass/CompassTests.swift | 7 ++----- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/Sources/Navigator.swift b/Sources/Navigator.swift index ef3cd97..0ebf6d8 100644 --- a/Sources/Navigator.swift +++ b/Sources/Navigator.swift @@ -24,6 +24,18 @@ public struct Navigator { /// A list of route strings public static var routes = [String]() + /// Parse Location from urn + /// + /// - Parameter urn: Safely construct url from urn, perform percent encoding + /// - Returns: The Location that can be used + public static func parse(urn: String) -> Location? { + guard let url = URL(string: "\(scheme)\(urn.compass_encoded())") else { + return nil + } + + return parse(url: url) + } + /// Parse Location from url /// /// - Parameters: @@ -117,27 +129,3 @@ public struct Navigator { wildcardMatchCount: wildcardMatchCount) } } - -extension Navigator { - - /// Parse an urn to be Compass friendly URL - /// - /// - Parameters: - /// - urn: The requested urn - /// - scheme: The application scheme - /// - Returns: The URL to be ready used by Compass - public static func compassURL(urn: String, scheme: String = Navigator.scheme) -> URL? { - return URL(string: "\(scheme)\(urn.compass_encoded())") - } - - - /// Navigate by telling the application to open a url - /// - /// - Parameters: - /// - urn: The requested urn - /// - scheme: The application scheme - public static func navigate(to urn: String, scheme: String = Navigator.scheme) { - guard let url = compassURL(urn: urn, scheme: scheme) else { return } - open(url: url) - } -} diff --git a/Tests/Compass/CompassTests.swift b/Tests/Compass/CompassTests.swift index d829e4e..de26d86 100644 --- a/Tests/Compass/CompassTests.swift +++ b/Tests/Compass/CompassTests.swift @@ -1,6 +1,6 @@ import Foundation import XCTest -import Compass +@testable import Compass class CompassTests: XCTestCase { @@ -267,11 +267,8 @@ class CompassTests: XCTestCase { func testEncodedURN() { let urn = "organization:hyper oslo:simply awesome" - let url = Navigator.compassURL(urn: urn) - XCTAssertNotNil(url) - - guard let location = Navigator.parse(url: url!) else { + guard let location = Navigator.parse(urn: urn) else { XCTFail("Compass parsing failed") return } From e0d67b35cad8c98071a2a14c7952ca4d22311c2e Mon Sep 17 00:00:00 2001 From: Khoa Pham Date: Mon, 31 Jul 2017 12:55:44 +0200 Subject: [PATCH 2/4] Declare as fileprivate to hide helper functions --- Sources/Navigator.swift | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Sources/Navigator.swift b/Sources/Navigator.swift index 0ebf6d8..da1391c 100644 --- a/Sources/Navigator.swift +++ b/Sources/Navigator.swift @@ -2,14 +2,6 @@ import Foundation /// The Navigator is used to parse Location from url, and navigate public struct Navigator { - - /// The Result used in the findMatch function - typealias Result = ( - route: String, - arguments: [String: String], - concreteMatchCount: Int, - wildcardMatchCount: Int) - fileprivate static var internalScheme = "" /// The delimiter used to split parts within url, default to : @@ -73,7 +65,7 @@ public struct Navigator { /// - url: The url to be parsed /// - payload: The optional payload if you want to send in app objects /// - Returns: The Location that can be used - static func parseComponents(url: URL, payload: Any? = nil) -> Location? { + fileprivate static func parseComponents(url: URL, payload: Any? = nil) -> Location? { guard let route = url.host else { return nil } let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) @@ -90,13 +82,20 @@ public struct Navigator { return Location(path: route, arguments: arguments, payload: payload) } + /// The Result used in the findMatch function + fileprivate typealias Result = ( + route: String, + arguments: [String: String], + concreteMatchCount: Int, + wildcardMatchCount: Int) + /// Find the best match registed route for a certain route string /// /// - Parameters: /// - routeString: The registered route string /// - pathString: The path extracted from the requested url /// - Returns: The Result on how this pathString matches - static func findMatch(routeString: String, pathString: String) -> Result? { + fileprivate static func findMatch(routeString: String, pathString: String) -> Result? { let routes = routeString.split(delimiter) let paths = pathString.split(delimiter) From f29d64326499910b61044a962968af4e6c6174ea Mon Sep 17 00:00:00 2001 From: Khoa Pham Date: Mon, 31 Jul 2017 12:58:58 +0200 Subject: [PATCH 3/4] Remove unused property in Location --- Sources/Location.swift | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Sources/Location.swift b/Sources/Location.swift index 918219b..a85648f 100644 --- a/Sources/Location.swift +++ b/Sources/Location.swift @@ -11,11 +11,6 @@ public struct Location { /// An optional payload if you want to send in app objects public let payload: Any? - /// The application scheme - public var scheme: String { - return Navigator.scheme - } - /// Construct a Location public init(path: String, arguments: [String: String] = [:], payload: Any? = nil) { self.path = path From afe6f36cb10e481a3bd15ae01ceab665ce580b12 Mon Sep 17 00:00:00 2001 From: Khoa Pham Date: Mon, 31 Jul 2017 13:16:59 +0200 Subject: [PATCH 4/4] Remove @testable --- Tests/Compass/CompassTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Compass/CompassTests.swift b/Tests/Compass/CompassTests.swift index de26d86..7d3ac58 100644 --- a/Tests/Compass/CompassTests.swift +++ b/Tests/Compass/CompassTests.swift @@ -1,6 +1,6 @@ import Foundation import XCTest -@testable import Compass +import Compass class CompassTests: XCTestCase {