Skip to content

Commit

Permalink
Merge pull request #50 from hyperoslo/fix/navigator
Browse files Browse the repository at this point in the history
Fix navigator
  • Loading branch information
onmyway133 committed Jul 31, 2017
2 parents 9598ba8 + afe6f36 commit 1c57fb4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 43 deletions.
5 changes: 0 additions & 5 deletions Sources/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 21 additions & 34 deletions Sources/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand All @@ -24,6 +16,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:
Expand Down Expand Up @@ -61,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)
Expand All @@ -78,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)

Expand Down Expand Up @@ -117,27 +128,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)
}
}
5 changes: 1 addition & 4 deletions Tests/Compass/CompassTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 1c57fb4

Please sign in to comment.