-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchannels.js
199 lines (187 loc) · 7.21 KB
/
channels.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* robin-js-sdk
* http://getrobin.com/
*
* Copyright (c) 2014 Robin Powered Inc.
* Licensed under the Apache v2 license.
* https://github.com/robinpowered/robin-js-sdk/blob/master/LICENSE
*
*/
var constants = require('../../util').constants;
module.exports = {
/**
* Get all the channels or a particular channel identified by the `identifier` parameter
* @param {String|Integer|undefined} identifier A Robin channel identifier
* @param {Object|undefined} params A querystring object
* @return {Function} A Promise
*/
get: function (identifier, params) {
var path = this.constructPath(constants.CHANNELS, identifier);
return this.Core.GET(path, params);
},
/**
* Create a channel
* @param {Object} data A data object
* @return {Function} A Promise
*/
create: function (data) {
var path;
if (data) {
path = this.constructPath(constants.CHANNELS);
return this.Core.POST(path, data);
} else {
return this.rejectRequest('Bad Request: A data object is required.');
}
},
/**
* Update a channel
* @param {String|Integer|undefined} identifier A Robin channel identifier
* @param {Object} data A data object
* @return {Function} A Promise
*/
update: function (identifier, data) {
var path;
if (identifier && data) {
path = this.constructPath(constants.CHANNELS, identifier);
return this.Core.PATCH(path, data);
} else {
return this.rejectRequest('Bad Request: A channel identifier and a data object are required.');
}
},
/**
* Delete a channel
* @param {String|Integer} identifier A Robin channel identifier
* @return {Function} A Promise
*/
delete: function (identifier) {
var path;
if (identifier) {
path = this.constructPath(constants.CHANNELS, identifier);
return this.Core.DELETE(path);
} else {
return this.rejectRequest('Bad Request: A channel identifier is required.');
}
},
/**
* Channel Data
* @type {Object}
*/
data: {
/**
* Get all the data from a channel or a particular channel data point identified by `dataIdentifier`
* @param {String|Integer} channelIdentifier A Robin channel identifier
* @param {String|Integer|undefined} dataIdentifier A Robin channel data point identifier
* @param {Object|undefined} params A querystring object
* @return {Function} A Promise
*/
get: function (channelIdentifier, dataIdentifier, params) {
var path;
if (channelIdentifier) {
path = this.constructPath(constants.CHANNELS, channelIdentifier, constants.DATA, dataIdentifier);
return this.Core.GET(path, params);
} else {
return this.rejectRequest('Bad Request: A channel identifier is required.');
}
},
/**
* Add data to a channel
* @param {String|Integer} channelIdentifier A Robin channel identifier
* @param {Object} data A querystring object
* @return {Function} A Promise
*/
add: function (channelIdentifier, data) {
var path;
if (channelIdentifier) {
path = this.constructPath(constants.CHANNELS, channelIdentifier, constants.DATA);
return this.Core.POST(path, data);
} else {
return this.rejectRequest('Bad Request: A channel identifier is required.');
}
},
/**
* Delete a data point from a channel
* @param {String|Integer} channelIdentifier A Robin channel identifier
* @param {String|Integer} dataIdentifier A Robin channel data point identifier
* @return {Function} A Promise
*/
delete: function (channelIdentifier, dataIdentifier) {
var path;
if (channelIdentifier && dataIdentifier) {
path = this.constructPath(constants.CHANNELS, channelIdentifier, constants.DATA, dataIdentifier);
return this.Core.DELETE(path);
} else {
return this.rejectRequest('Bad Request: A channel identifier and a data point identifier are required.');
}
}
},
/**
* Channel Triggers
* @type {Object}
*/
triggers: {
/**
* Get all the triggers on a channel or a particular channel trigger identified by `triggerIdentifier`
* @param {String|Integer} channelIdentifier A Robin channel identifier
* @param {String|Integer|undefined} triggerIdentifier A Robin channel trigger identifier
* @param {Object|undefined} params A querystring object
* @return {Function} A Promise
*/
get: function (channelIdentifier, triggerIdentifier, params) {
var path;
if (channelIdentifier) {
path = this.constructPath(constants.CHANNELS, channelIdentifier, constants.TRIGGERS, triggerIdentifier);
return this.Core.GET(path, params);
} else {
return this.rejectRequest('Bad Request: A channel identifier is required.');
}
},
/**
* Add a trigger to a channel
* @param {String|Integer} channelIdentifier A Robin channel identifier
* @param {Object} data A querystring object
* @return {Function} A Promise
*/
add: function (channelIdentifier, data) {
var path;
if (channelIdentifier) {
path = this.constructPath(constants.CHANNELS, channelIdentifier, constants.TRIGGERS);
return this.Core.POST(path, data);
} else {
return this.rejectRequest('Bad Request: A channel identifier and a data object are required.');
}
},
/**
* Update a trigger on a channel
* @param {String|Integer} channelIdentifier A Robin channel identifier
* @param {String|Integer} triggerIdentifier A Robin channel trigger identifier
* @param {Object} data A querystring object
* @return {Function} A Promise
*/
update: function (channelIdentifier, triggerIdentifier, data) {
var path,
rejectMsg;
if (channelIdentifier && triggerIdentifier && data) {
path = this.constructPath(constants.CHANNELS, channelIdentifier, constants.TRIGGERS, triggerIdentifier);
return this.Core.PATCH(path, data);
} else {
rejectMsg = 'Bad Request: A channel identifier, a feed identifier and a trigger object are required.';
return this.rejectRequest(rejectMsg);
}
},
/**
* Delete a trigger from a channel
* @param {String|Integer} channelIdentifier A Robin channel identifier
* @param {String|Integer} triggerIdentifier A Robin channel data point identifier
* @return {Function} A Promise
*/
delete: function (channelIdentifier, triggerIdentifier) {
var path;
if (channelIdentifier && triggerIdentifier) {
path = this.constructPath(constants.CHANNELS, channelIdentifier, constants.TRIGGERS, triggerIdentifier);
return this.Core.DELETE(path);
} else {
return this.rejectRequest('Bad Request: A channel identifier and a trigger identifier are required.');
}
}
}
};