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

Encoding urn #45

Merged
merged 3 commits into from
Jan 30, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion Sources/Compass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ public struct Compass {

extension Compass {

public static func compassURL(urn: String, scheme: String = Compass.scheme) -> URL? {
return URL(string: "\(scheme)\(urn.compass_encoded())")
}

public static func navigate(to urn: String, scheme: String = Compass.scheme) {
guard let url = URL(string: "\(scheme)\(urn)") else { return }
guard let url = compassURL(urn: urn, scheme: scheme) else { return }
open(url: url)
}
}
8 changes: 7 additions & 1 deletion Sources/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ public struct Location {

public init(path: String, arguments: [String: String] = [:], payload: Any? = nil) {
self.path = path
self.arguments = arguments
self.payload = payload

var decodedArguments = [String: String]()
arguments.forEach { (key, value) in
decodedArguments[key] = value.compass_decoded()
}

self.arguments = decodedArguments
}
}
8 changes: 8 additions & 0 deletions Sources/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ extension String {

return parameters
}

func compass_encoded() -> String {
return addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? self
}

func compass_decoded() -> String {
return removingPercentEncoding ?? self
}
}
18 changes: 17 additions & 1 deletion Tests/Compass/CompassTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CompassTests: XCTestCase {
"profile:admin",
"login",
"callback",
"organization:{name}:{type}",
"user:list:{userId}:{kind}",
"user:list",
"{appId}:user:list:{userId}:{kind}"
Expand All @@ -23,7 +24,7 @@ class CompassTests: XCTestCase {

func testRoutes() {
XCTAssert(!Compass.routes.isEmpty)
XCTAssert(Compass.routes.count == 7)
XCTAssert(Compass.routes.count == 8)
}

func testParseArguments() {
Expand Down Expand Up @@ -263,4 +264,19 @@ class CompassTests: XCTestCase {
XCTAssertEqual(location.arguments["expires_in"], "3600")
XCTAssertEqual(location.arguments["token_type"], "Bearer")
}

func testEncodedURN() {
let urn = "organization:hyper oslo:simply awesome"
let url = Compass.compassURL(urn: urn)

XCTAssertNotNil(url)

guard let location = Compass.parse(url: url!) else {
XCTFail("Compass parsing failed")
return
}

XCTAssertEqual(location.arguments["name"], "hyper oslo")
XCTAssertEqual(location.arguments["type"], "simply awesome")
}
}