/
airship.js
171 lines (164 loc) · 4.97 KB
/
airship.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var u = require('./utils')
, Request = require('./request').Request
, Airship;
Airship = function (appKey, masterSecret) {
if (!appKey) { throw new Error('You must provide an app key'); }
if (!masterSecret) { throw new Error('You must provide your master secret'); }
this.appKey = appKey;
this.masterSecret = masterSecret;
};
Airship.prototype = {
/**
* @private
*/
request: function (options) {
var req = new Request(options);
req.setAuth(this.appKey, this.masterSecret);
return req;
}
/**
* @param {Object} options Use to set the page number of results you want
*
* @see http://urbanairship.com/docs/push.html#device-token-list-api
*/
, deviceTokens: function (options) {
var path = u.deviceTokenPath();
if (options && options.page) {
path += '?page=' + options.page;
}
return this.request({ path: path }).get();
}
/**
* @see http://urbanairship.com/docs/tags.html#seeing-your-tags
*/
, tags: function () {
return this.request({ path: u.tagPath() }).get();
}
/**
* @param {String} tagName the name of the tag to add to the application
*
* @see http://urbanairship.com/docs/tags.html#adding-tags
*/
, addTag: function (tagName) {
return this.request({ path: u.tagPath(tagName) }).put();
}
/**
* @param {String} tagName the name of the tag to remove from the application
*
* @see http://urbanairship.com/docs/tags.html#removing-tags
*/
, removeTag: function (tagName) {
return this.request({ path: u.tagPath(tagName) }).delete();
}
/**
* @param {String} tagName the name of the tag to modify
*
* @param {Object} payload a JavaScript object describing how to modify the tag
*
* @see http://urbanairship.com/docs/tags.html#modifying-device-tokens-on-a-tag
*/
, modifyTokensOnTag: function (tagName, payload) {
return this.request({ path: u.tagPath(tagName) }).post(payload);
}
/**
* @param {String} deviceToken the token for the Device of interest
*/
, deviceTags: function (deviceToken) {
return this.request({ path: u.deviceTokenTagPath(deviceToken) }).get();
}
/**
* @param {String} deviceToken the token for the Device of interest
*
* @param {String} tag the tag to add to the Device
*/
, addTagToDevice: function (deviceToken, tag) {
return this.request({ path: u.deviceTokenTagPath(deviceToken, tag) }).put();
}
/**
* @param {String} deviceToken the token for the Device of interest
*
* @param {String} tag the tag to remove from the Device
*/
, removeTagFromDevice: function (deviceToken, tag) {
return this.request({ path: u.deviceTokenTagPath(deviceToken, tag) }).delete();
}
/**
* @param {String} deviceToken the token of the Device to register
*
* @param {Object} payload The optional payload of options to initialize the
* Device with
*
* @see http://urbanairship.com/docs/push.html#registration
*/
, register: function (deviceToken, payload) {
return this.request({ path: u.deviceTokenPath(deviceToken) }).put(payload);
}
/**
* Inactive a device
*
* @param {String} deviceToken the token of the Device to inactivate
*
* @see http://urbanairship.com/docs/push.html#registration
*/
, inactivate: function (deviceToken) {
return this.request({ path: u.deviceTokenPath(deviceToken) }).delete();
}
/**
* Send a push message
*
* @param {Object} payload describes the push message to send
*
* @see http://urbanairship.com/docs/push.html#push
*/
, push: function (payload) {
return this.request({ path: '/api/push/' }).post(payload);
}
/**
* Cancel a single push message, or many
*
* @param {Object, String} idOrArgs Either the payload describing the bulk
* or the ID of the specific message to send
*
* @see http://urbanairship.com/docs/push.html#scheduled-notifications
*/
, cancelPush: function (idOrArgs) {
var req = this.request({ path: u.scheduledPushPath(idOrArgs) });
if (typeof idOrArgs == 'object') {
return req.post(idOrArgs);
} else {
return req.delete();
}
}
/**
* Send a batch push message request
*
* @param {Object} payload
*
* @see http://urbanairship.com/docs/push.html#batch-push
*/
, batchPush: function (payload) {
return this.request({ path: '/api/push/batch/' }).post(payload);
}
/**
* Broadcast a message to all registered devices
*
* @param {Object} payload
*
* @see http://urbanairship.com/docs/push.html#broadcast
*/
, broadcast: function (payload) {
return this.request({ path: '/api/push/broadcast/' }).post(payload);
}
/**
* Get feedback regarding usage
*
* @param {Date} since The point in time from which you want feedback
*
* @see http://urbanairship.com/docs/push.html#feedback-service
*/
, feedback: function (since) {
var path = u.deviceTokenPath() + 'feedback/?since=' + since.toISOString();
return this.request({ path: path }).get();
}
};
exports.Airship = Airship;