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

Refactor: make it perfect #5

Merged
merged 5 commits into from
Nov 13, 2015
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
14 changes: 6 additions & 8 deletions Pod/Pod.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
objects = {

/* Begin PBXBuildFile section */
BD6929341BC528C60063E97E /* TestCompass.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD6929331BC528C60063E97E /* TestCompass.swift */; settings = {ASSET_TAGS = (); }; };
BD93420C1BC4FCF200F25395 /* Compass.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD93420B1BC4FCF200F25395 /* Compass.swift */; settings = {ASSET_TAGS = (); }; };
BD6929341BC528C60063E97E /* TestCompass.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD6929331BC528C60063E97E /* TestCompass.swift */; };
BD93420C1BC4FCF200F25395 /* Compass.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD93420B1BC4FCF200F25395 /* Compass.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
146D72AC1AB782920058798C /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
146D72B11AB782920058798C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
14A139B21AEFC72B00AD732F /* Tests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Tests-Bridging-Header.h"; sourceTree = "<group>"; };
14C136511AB784B200B7B07A /* .travis.yml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = .travis.yml; path = ../.travis.yml; sourceTree = "<group>"; };
14C136521AB784B200B7B07A /* CONTRIBUTING.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = CONTRIBUTING.md; path = ../CONTRIBUTING.md; sourceTree = "<group>"; };
14C136541AB784B200B7B07A /* LICENSE.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = LICENSE.md; path = ../LICENSE.md; sourceTree = "<group>"; };
14C136551AB784B200B7B07A /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
14C1365C1AB784BC00B7B07A /* .gitkeep */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = .gitkeep; sourceTree = "<group>"; };
BD6929331BC528C60063E97E /* TestCompass.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestCompass.swift; sourceTree = "<group>"; };
BD93420B1BC4FCF200F25395 /* Compass.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Compass.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -59,7 +57,6 @@
isa = PBXGroup;
children = (
146D72B01AB782920058798C /* Supporting Files */,
14A139B21AEFC72B00AD732F /* Tests-Bridging-Header.h */,
BD6929331BC528C60063E97E /* TestCompass.swift */,
);
path = Tests;
Expand Down Expand Up @@ -87,7 +84,6 @@
14C1365B1AB784BC00B7B07A /* Source */ = {
isa = PBXGroup;
children = (
14C1365C1AB784BC00B7B07A /* .gitkeep */,
BD93420B1BC4FCF200F25395 /* Compass.swift */,
);
name = Source;
Expand Down Expand Up @@ -261,7 +257,8 @@
INFOPLIST_FILE = Tests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Tests/Tests-Bridging-Header.h";
SWIFT_INSTALL_OBJC_HEADER = NO;
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
name = Debug;
Expand All @@ -277,7 +274,8 @@
INFOPLIST_FILE = Tests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Tests/Tests-Bridging-Header.h";
SWIFT_INSTALL_OBJC_HEADER = NO;
SWIFT_OBJC_BRIDGING_HEADER = "";
};
name = Release;
};
Expand Down
1 change: 0 additions & 1 deletion Pod/Tests/TestCompass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ class TestCompass: XCTestCase {
XCTAssert(arguments.isEmpty)
}
}

}
4 changes: 0 additions & 4 deletions Pod/Tests/Tests-Bridging-Header.h

This file was deleted.

Empty file removed Source/.gitkeep
Empty file.
18 changes: 14 additions & 4 deletions Source/Compass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import UIKit
public struct Compass {

private static var internalScheme = ""

public static var scheme: String {
set { Compass.internalScheme = newValue }
get { return "\(Compass.internalScheme)://" }
}

public static var routes = [String]()

public static func parse(url: NSURL, completion: (route: String, arguments: [String : String]) -> Void) -> Bool {
public typealias ParseCompletion = (route: String, arguments: [String : String]) -> Void

public static func parse(url: NSURL, completion: ParseCompletion) -> Bool {
var result = false
let query = url.absoluteString.substringFromIndex(scheme.endIndex)

for route in routes.sort({ $0 < $1 }) {
Expand All @@ -19,7 +23,7 @@ public struct Compass {
.map(String.init))
.first else { continue }

if (query.hasPrefix(prefix) || prefix.hasPrefix(query)) {
if query.hasPrefix(prefix) || prefix.hasPrefix(query) {
let queryString = query.stringByReplacingOccurrencesOfString(prefix, withString: "")
let queryArguments = paths(queryString)
let routeArguments = paths(route).filter { $0.containsString("{") }
Expand All @@ -32,19 +36,25 @@ public struct Compass {
? queryArguments[index] : nil
}
completion(route: route, arguments: arguments)
return true

result = true
break
}
}
}
return false

return result
}

public static func navigate(urn: String, scheme: String = Compass.scheme) {
let stringURL = "\(scheme)\(urn)"
guard let url = NSURL(string: stringURL) else { return }

UIApplication.sharedApplication().openURL(url)
}

// MARK: - Private Helpers

private static func paths(urn: String) -> [String] {
return urn.characters
.split { $0 == ":" }
Expand Down