Skip to content

Commit 0ac843f

Browse files
committed
Tidy up NativePusher and PusherSwift. Add some inline docs
1 parent 7f9e760 commit 0ac843f

File tree

2 files changed

+115
-203
lines changed

2 files changed

+115
-203
lines changed

Source/NativePusher.swift

Lines changed: 108 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,97 +11,122 @@ import Foundation
1111
*/
1212
public class NativePusher {
1313
static let sharedInstance = NativePusher()
14-
14+
1515
private static let PLATFORM_TYPE = "apns"
1616
private let CLIENT_API_V1_ENDPOINT = "https://nativepushclient-cluster1.pusher.com/client_api/v1"
17-
17+
1818
private let URLSession = NSURLSession.sharedSession()
19-
20-
// Identifies a Pusher app.
21-
// This app should have push notifications enabled.
19+
20+
/**
21+
Identifies a Pusher app.
22+
This app should have push notifications enabled.
23+
*/
2224
private var pusherAppKey: String? = nil
23-
25+
26+
2427
public func setPusherAppKey(pusherAppKey: String) {
2528
self.pusherAppKey = pusherAppKey
2629
tryFlushOutbox()
2730
}
28-
29-
// The id issued to this app instance by Pusher.
30-
// We get it upon registration.
31-
// We use it to identify ourselves when subscribing/unsubscribing.
31+
32+
/**
33+
The id issued to this app instance by Pusher.
34+
We get it upon registration.
35+
We use it to identify ourselves when subscribing/unsubscribing.
36+
*/
3237
private var clientId: String? = nil
33-
34-
// Queued actions to perform when the client is registered.
35-
private var outbox: Array<(String, SubscriptionChange)> = []
36-
37-
// Normal clients should access the shared instance via Pusher.nativePusher().
38-
private init() {
38+
39+
/**
40+
Queued actions to perform when the client is registered.
41+
*/
42+
private var outbox: [(String, SubscriptionChange)] = []
43+
44+
/**
45+
Normal clients should access the shared instance via Pusher.nativePusher().
46+
*/
47+
private init() {}
48+
49+
/**
50+
Makes device token presentable to server
51+
52+
- parameter deviceToken: the deviceToken received when registering
53+
to receive push notifications, as NSData
54+
55+
- returns: the deviceToken formatted as a String
56+
*/
57+
private func deviceTokenToString(deviceToken: NSData) -> String {
58+
let characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
59+
60+
let deviceTokenString: String = ( deviceToken.description as NSString )
61+
.stringByTrimmingCharactersInSet( characterSet )
62+
.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
63+
return deviceTokenString
3964
}
40-
65+
4166
/**
4267
Registers this app instance with Pusher for push notifications.
4368
This must be done before we can subscribe to interests.
4469
Registration happens asynchronously; any errors are reported by print statements.
70+
71+
- parameter deviceToken: the deviceToken received when registering
72+
to receive push notifications, as NSData
4573
*/
46-
public func register(deviceToken : NSData) {
74+
public func register(deviceToken: NSData) {
4775
let request = NSMutableURLRequest(URL: NSURL(string: CLIENT_API_V1_ENDPOINT + "/clients")!)
4876
request.HTTPMethod = "POST"
4977
let deviceTokenString = deviceTokenToString(deviceToken)
50-
51-
let params: [String: AnyObject] = [
78+
79+
let params: [String : AnyObject] = [
5280
"platform_type": NativePusher.PLATFORM_TYPE,
5381
"token": deviceTokenString
5482
]
55-
83+
5684
try! request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: [])
5785
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
58-
86+
5987
let task = URLSession.dataTaskWithRequest(request, completionHandler: { data, response, error in
60-
if let httpResponse =
61-
response as? NSHTTPURLResponse
62-
where (
63-
httpResponse.statusCode >= 200 &&
64-
httpResponse.statusCode < 300)
65-
{
66-
// We expect to get a JSON response in the form:
67-
//
68-
// {
69-
// "id": string,
70-
// "pusher_app_key": string,
71-
// "platform_type": either "apns" or "gcm",
72-
// "token": string
73-
// }
74-
//
75-
// Currently, we only care about the "id" value, which is our new client id.
76-
// We store our id so that we can use it to subscribe/unsubscribe.
77-
if let json = try!NSJSONSerialization.JSONObjectWithData(data!, options: [])
78-
as? Dictionary<String, AnyObject>
79-
{
80-
if let clientIdJson = json["id"] {
81-
// There is a value at key "id".
82-
if let clientId = clientIdJson as? String {
83-
// Success. We got a string id. Squirrel it away.
84-
self.clientId = clientId
85-
self.tryFlushOutbox()
88+
if let httpResponse = response as? NSHTTPURLResponse
89+
where (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) {
90+
/**
91+
We expect to get a JSON response in the form:
92+
93+
{
94+
"id": string,
95+
"pusher_app_key": string,
96+
"platform_type": either "apns" or "gcm",
97+
"token": string
98+
}
99+
100+
Currently, we only care about the "id" value, which is our new client id.
101+
We store our id so that we can use it to subscribe/unsubscribe.
102+
*/
103+
if let json = try!NSJSONSerialization.JSONObjectWithData(data!, options: [])
104+
as? [String: AnyObject] {
105+
if let clientIdJson = json["id"] {
106+
if let clientId = clientIdJson as? String {
107+
self.clientId = clientId
108+
self.tryFlushOutbox()
109+
} else {
110+
print("Value at \"id\" key in JSON response was not a string: " + String(json))
111+
}
112+
} else {
113+
print("No \"id\" key in JSON response: " + String(json))
114+
}
86115
} else {
87-
print("Value at \"id\" key in JSON response was not a string: " + String(json))
116+
print("Could not parse body as JSON object:" + String(data))
88117
}
89-
} else {
90-
print("No \"id\" key in JSON response: " + String(json))
91-
}
92-
} else {
93-
print("Could not parse body as JSON object:" + String(data))
94-
}
95118
} else {
96119
print("Bad HTTP response: " + String(response))
97120
}
98121
})
99-
122+
100123
task.resume()
101124
}
102-
125+
103126
/**
104127
Subscribe to an interest with Pusher's Push Notification Service
128+
129+
- parameter interestName: the name of the interest you want to subscribe to
105130
*/
106131
public func subscribe(interestName: String) {
107132
outbox.append(interestName, SubscriptionChange.Subscribe)
@@ -110,26 +135,43 @@ public class NativePusher {
110135

111136
/**
112137
Unsubscribe from an interest with Pusher's Push Notification Service
138+
139+
- parameter interestName: the name of the interest you want to unsubscribe
140+
from
113141
*/
114142
public func unsubscribe(interestName: String) {
115143
outbox.append(interestName, SubscriptionChange.Unsubscribe)
116144
tryFlushOutbox()
117145
}
118-
146+
147+
/**
148+
Attempts to flush the outbox by making the appropriate requests to either
149+
subscribe to or unsubscribe from an interest
150+
*/
119151
private func tryFlushOutbox() {
120152
switch (self.pusherAppKey, self.clientId) {
121153
case (.Some(let pusherAppKey), .Some(let clientId)):
122154
if (0 < outbox.count) {
123-
let (interest,change) = outbox.removeAtIndex(0)
155+
let (interest, change) = outbox.removeAtIndex(0)
124156
modifySubscription(pusherAppKey, clientId: clientId, interest: interest, change: change) {
125157
self.tryFlushOutbox()
126158
}
127159
}
128160
case _: break
129161
}
130162
}
131-
132-
private func modifySubscription(pusherAppKey:String, clientId: String, interest: String, change: SubscriptionChange, callback: (Void)->(Void)) {
163+
164+
/**
165+
Makes either a POST or DELETE request for a given interest
166+
167+
- parameter pusherAppKey: The app key for the Pusher app
168+
- parameter clientId: The clientId returned by Pusher's server
169+
- parameter interest: The name of the interest to be subscribed to /
170+
unsunscribed from
171+
- parameter change: Whether to subscribe or unsubscribe
172+
- parameter callback: Callback to be called upon success
173+
*/
174+
private func modifySubscription(pusherAppKey: String, clientId: String, interest: String, change: SubscriptionChange, callback: (Void) -> (Void)) {
133175
let url = "\(CLIENT_API_V1_ENDPOINT)/clients/\(clientId)/interests/\(interest)"
134176
let request = NSMutableURLRequest(URL: NSURL(string: url)!)
135177
switch (change) {
@@ -138,14 +180,12 @@ public class NativePusher {
138180
case .Unsubscribe:
139181
request.HTTPMethod = "DELETE"
140182
}
141-
142-
let params: [String: AnyObject] = [
143-
"app_key": pusherAppKey,
144-
]
145-
183+
184+
let params: [String : AnyObject] = ["app_key": pusherAppKey]
185+
146186
try! request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: [])
147187
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
148-
188+
149189
let task = URLSession.dataTaskWithRequest(
150190
request,
151191
completionHandler: { data, response, error in
@@ -158,7 +198,7 @@ public class NativePusher {
158198
callback()
159199
}
160200
)
161-
201+
162202
task.resume()
163203
}
164204
}

0 commit comments

Comments
 (0)