Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix navigator #50

Merged
merged 4 commits into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
7 changes: 2 additions & 5 deletions Tests/Compass/CompassTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
import XCTest
import Compass
@testable import Compass

class CompassTests: XCTestCase {

Expand Down 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