-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknowtify.js
238 lines (209 loc) · 7.01 KB
/
knowtify.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Generated by CoffeeScript 1.7.1
(function() {
var Contacts, GlobalData, OPTS, http;
http = require('http');
OPTS = {
host: 'www.knowtify.io',
port: 80,
prefix: '/api/v1/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Knowtify-Node/0.1'
}
};
exports.Knowtify = (function() {
function Knowtify(token, debug) {
this.token = token != null ? token : null;
this.debug = debug != null ? debug : false;
this.contacts = new Contacts(this);
this.globalData = new GlobalData(this);
if (this.token === null) {
this.token = process.env['KNOWTIFY_TOKEN'];
}
}
Knowtify.prototype.call = function(uri, params, onresult, onerror) {
var req;
if (params == null) {
params = {};
}
params = new Buffer(JSON.stringify(params), 'utf8');
if (this.debug) {
console.log("Knowtify: Opening request to http://" + OPTS.host + OPTS.prefix + uri);
}
OPTS.path = "" + OPTS.prefix + uri;
OPTS.headers['Content-Length'] = params.length;
OPTS.headers['Authorization'] = 'Token token="' + this.token + '"';
req = http.request(OPTS, (function(_this) {
return function(res) {
var json;
res.setEncoding('utf8');
json = '';
res.on('data', function(d) {
return json += d;
});
return res.on('end', function() {
var e;
try {
json = JSON.parse(json);
} catch (_error) {
e = _error;
json = {
status: 'error',
name: 'GeneralError',
message: e
};
}
if (json == null) {
json = {
status: 'error',
name: 'GeneralError',
message: 'An unexpected error occurred'
};
}
if (res.statusCode !== 200) {
if (onerror) {
return onerror(json);
} else {
return _this.onerror(json);
}
} else {
if (onresult) {
return onresult(json);
}
}
});
};
})(this));
if (this.debug) {
console.log("" + params);
}
req.write(params);
req.end();
req.on('error', (function(_this) {
return function(e) {
if (onerror) {
return onerror(e);
} else {
return _this.onerror({
status: 'error',
name: 'GeneralError',
message: e
});
}
};
})(this));
return null;
};
Knowtify.prototype.onerror = function(err) {
throw {
name: err.name,
message: err.message,
toString: function() {
return "" + err.name + ": " + err.message;
}
};
};
return Knowtify;
})();
Contacts = (function() {
function Contacts(master) {
this.master = master;
/*
Adds a new contact, it also supports batch inserts
@param {Object} params the parameters to pass to the request
@param {Function} onsuccess an optional callback to execute when the API call is successfully made
@param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
*/
}
Contacts.prototype.add = function(params, onsuccess, onerror) {
if (params == null) {
params = {};
}
if (typeof params === 'function') {
onerror = onsuccess;
onsuccess = params;
params = {};
}
return this.master.call('contacts/add', params, onsuccess, onerror);
};
/*
Edit a contact by matching the email address, it also supports batch updates
@param {Object} params the parameters to pass to the request
@param {Function} onsuccess an optional callback to execute when the API call is successfully made
@param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
*/
Contacts.prototype.edit = function(params, onsuccess, onerror) {
if (params == null) {
params = {};
}
if (typeof params === 'function') {
onerror = onsuccess;
onsuccess = params;
params = {};
}
return this.master.call('contacts/edit', params, onsuccess, onerror);
};
/*
Deletes a contact
@param {Object} params the parameters to pass to the request
@param {Function} onsuccess an optional callback to execute when the API call is successfully made
@param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
*/
Contacts.prototype["delete"] = function(params, onsuccess, onerror) {
if (params == null) {
params = {};
}
if (typeof params === 'function') {
onerror = onsuccess;
onsuccess = params;
params = {};
}
return this.master.call('contacts/delete', params, onsuccess, onerror);
};
/*
This is a mix between add and edit. If the email is new to Knowtify it will add a contact, otherwise it will merge the contact information
@param {Object} params the hash of the parameters to pass to the request
@param {Function} onsuccess an optional callback to execute when the API call is successfully made
@param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
*/
Contacts.prototype.upsert = function(params, onsuccess, onerror) {
if (params == null) {
params = {};
}
if (typeof params === 'function') {
onerror = onsuccess;
onsuccess = params;
params = {};
}
return this.master.call('contacts/upsert', params, onsuccess, onerror);
};
return Contacts;
})();
GlobalData = (function() {
function GlobalData(master) {
this.master = master;
}
/*
Edit the global data, global data are shared informations across your account.
You can use them in any email.
If the data is new, it will create a new record.
@param {Object} params the hash of the parameters to pass to the request
@param {Function} onsuccess an optional callback to execute when the API call is successfully made
@param {Function} onerror an optional callback to execute when the API call errors out - defaults to throwing the error as an exception
*/
GlobalData.prototype.edit = function(params, onsuccess, onerror) {
if (params == null) {
params = {};
}
if (typeof params === 'function') {
onerror = onsuccess;
onsuccess = params;
params = {};
}
return this.master.call('data/edit', params, onsuccess, onerror);
};
return GlobalData;
})();
}).call(this);
//# sourceMappingURL=knowtify.map