-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
130 lines (99 loc) · 3.71 KB
/
main.swift
File metadata and controls
130 lines (99 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import Foundation
import URLMatcher
enum URLAction {
case login, signup, invite(key: String)
}
// Quick start example
var matcher = URLMatcher<URLAction>()
try matcher.add(path: "user/login") { .login }
try matcher.add(path: "user/signup") { .signup }
try matcher.add(path: "user/invite/<key: String>") {(_, values) in
.invite(key: values["key"] as! String)
}
// Grab your url from your web view or app delegate method
let url = URL(string:"http://mydomain.com/user/invite/y78gyug76g")!
if let action = matcher.matchURL(url) {
switch action {
case .login, .signup:
print("Show welcome form")
case .invite(let key):
// Show loading overlay
// Look up invite from the server
// Dismiss the loading overlay
// If available, show the accept invite flow
// If not, show an error alert
print("Load invite: \(key)")
}
} else {
// We don't know what to do with this URL, fallback on the system
}
// Matching example
struct UserAction {
let userId: Int64?
let action: String
}
extension UserAction: CustomDebugStringConvertible {
var debugDescription: String {
if let userId = userId {
return "\(action) user \(userId)"
} else {
return "\(action) user"
}
}
}
var userMatcher = URLMatcher<UserAction>()
try userMatcher.add(path: "user/<userId:Int64>/edit") { (_, values: [String : Any]) -> UserAction in
return UserAction(userId: values["userId"] as? Int64, action: "edit")
}
try userMatcher.add(path: "user/<userId:Int64>/<*>") { (query: [String: String], values: [String : Any]) -> UserAction in
if query["force_update"] == "true" {
return UserAction(userId: values["userId"] as? Int64, action: "update app")
} else {
return UserAction(userId: values["userId"] as? Int64, action: "show")
}
}
try userMatcher.add(path: "user/<+>/<*>") { return UserAction(userId: nil, action: "unknown") }
var action = userMatcher.matchURL(URL(string:"user/6578/edit")!)
print(action!)
action = userMatcher.matchURL(URL(string:"user/890822/verify/email")!)
print(action!)
action = userMatcher.matchURL(URL(string:"user/890822/verify/email?force_update=true")!)
print(action!)
action = userMatcher.matchURL(URL(string:"user/an.email@example.com/verify/email")!)
print(action!)
// Wildcard example
var wildcardMatcher = URLMatcher<String>()
try wildcardMatcher.add(path: "a/<+>") { "a" }
try wildcardMatcher.add(path: "<+>/b") { "b" }
var wildcard = wildcardMatcher.matchURL(URL(string:"a/b")!)
wildcard = wildcardMatcher.matchURL(URL(string: "c/b")!)
// Custom components example
struct Commit : ComponentMatchable {
func matches(urlComponent: String) -> Bool {
return "c" == urlComponent || "commit" == urlComponent
}
}
struct HashId: ComponentParseable {
let label: String
func matches(urlComponent: String) -> Bool {
return valueOf(urlComponent: urlComponent) != nil
}
func valueOf(urlComponent: String) -> Any? {
// Should actually validate more completely, not great string handling
if 7 == urlComponent.count || 40 == urlComponent.count {
return String(urlComponent.prefix(7))
} else {
return nil
}
}
}
var commitMatcher = URLMatcher<String>()
commitMatcher.registerType(name: "Commit") { (_) in Commit() }
commitMatcher.registerType(name: "HashId") { HashId(label: $0) }
try commitMatcher.add(path: "<Commit>/<commitId:HashId>") { (_, values: [String : Any]) -> String in
return values["commitId"] as! String
}
var commit = commitMatcher.matchURL(URL(string:"/c/7h1uh89")!)
print(commit!)
commit = commitMatcher.matchURL(URL(string:"/commit/7h1uh8927e39434ae8da5fuy6c1de98c56c09410")!)
print(commit!)