-
Notifications
You must be signed in to change notification settings - Fork 3
/
pollster.js
46 lines (35 loc) · 1023 Bytes
/
pollster.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
var request = require('request');
var API_SERVER = 'elections.huffingtonpost.com';
var API_BASE = '/pollster/api';
exports.version = '0.0.3';
// Gets lists of charts based on state and/or topic parameter
// No parameters gets all charts
exports.charts = function(params, callback) {
invoke('charts', params, function(resp){
callback(resp);
});
};
// Get single chart based on slug
exports.chart = function(slug, callback) {
invoke('charts/' + slug, {}, function(resp){
callback(resp);
});
};
// Gets list of charts based on parameters
// Polls are listed in pages of 10
exports.polls = function(params, callback) {
invoke('polls', params, function(resp){
callback(resp);
});
};
// Helper functions
function buildRequestUrl(path) {
var url = 'http://' + API_SERVER + API_BASE + '/' + path;
return url;
}
function invoke(path, params, callback) {
var url = buildRequestUrl(path);
request(url, {qs: params}, function (error, response, body) {
callback(JSON.parse(body));
});
}