-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
62 lines (50 loc) · 1.46 KB
/
index.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
var request = require('request'),
util = require('util'),
helpers = require('./lib/helpers'),
core = require('./lib/core'),
push = require('./lib/push');
function DataSift(options){
if(!options) { throw new Error('You must include your USERNAME and API_KEY in options'); }
this.username = options.USERNAME;
this.api_key = options.API_KEY;
this.vars = {
username: this.username,
api_key: this.api_key
};
this.core.vars = this.vars;
this.core.sendRequest = this.sendRequest;
this.helpers.core = this.core;
this.push.vars = this.vars;
this.push.sendRequest = this.sendRequest;
};
DataSift.prototype.core = core;
DataSift.prototype.helpers = helpers;
DataSift.prototype.push = push;
DataSift.prototype.sendRequest = function sendRequest(call, params, callback){
var headers = {
Auth: this.vars.username + ":" + this.vars.api_key
};
var req = request.post({
headers: headers,
url: "https://api.datasift.com/" + call,
form: params
}, function(err, res, data){
if (err) {
callback(new DataSiftError(err));
} else {
if (!data) { data = "{}" };
data = JSON.parse(data);
if (data.error) {
callback(new DataSiftError(data.error));
} else {
callback(null, data, res);
}
}
});
};
function DataSiftError (message) {
Error.captureStackTrace(this, DataSiftError);
this.message = message;
}
util.inherits(DataSiftError, Error);
module.exports = DataSift;