-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsunlight.js
More file actions
137 lines (124 loc) · 4.27 KB
/
Copy pathsunlight.js
File metadata and controls
137 lines (124 loc) · 4.27 KB
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
var qs = require('querystring'),
request = require('request'),
async = require('async');
var SunlightClient = exports.SunlightClient = function(apiKey) {
this.key = apiKey;
this.legislators = new LegislatorClient(this);
this.committees = new CommitteeClient(this);
this.districts = new DistrictClient(this);
this._call = function(type, params, callback) {
if (this.key === null) {
throw new Error("Missing Sunlight Labs API key");
}
var query = qs.stringify(params);
var uri = encodeURI("http://services.sunlightlabs.com/api/" + type + ".json?apikey=" + this.key + "&" + query);
request(uri, function (err, res, body) {
if (!err && res.statusCode == 200) {
var result = JSON.parse(body).response;
_adjust(result, callback);
} else {
callback(new Error(body));
}
});
};
};
// Necessary function to maintain async while adjusting object
var _adjust = function(obj, callback) {
if (obj.legislator != null) {
callback(obj.legislator);
} else if (obj.legislators != null) {
async.map(obj.legislators, function(i, cb) {
process.nextTick(function() {
cb(null, i.legislator);
});
},
function(err, results) {
if (err) throw err;
callback(results);
});
} else if (obj.results != null) {
async.map(obj.results, function(i, cb) {
process.nextTick(function() {
cb(null, i.result);
});
},
function(err, results) {
if (err) throw err;
callback(results);
});
} else if (obj.committee != null) {
callback(obj.committee);
} else if (obj.committees != null) {
async.map(obj.committees, function(i, cb) {
process.nextTick(function() {
cb(null, i.committee);
});
},
function(err, results) {
if (err) throw err;
callback(results);
});
} else if(obj.districts != null) {
async.map(obj.districts, function(i, cb) {
process.nextTick(function() {
cb(null, i.district);
});
},
function(err, results) {
if (err) throw err;
callback(results);
});
}
};
var LegislatorClient = function(parent) {
this.parent = parent;
};
LegislatorClient.prototype = {
get: function(query, callback) {
this.parent._call('legislators.get', query, callback);
},
getList: function(query, callback) {
this.parent._call('legislators.getList', query, callback);
},
search: function(name, options, callback) {
var query = { name: name };
for (key in options) {
query[key] = options[key];
}
this.parent._call('legislators.search', query, callback);
},
allForZip: function(zip, callback) {
this.parent._call('legislators.allForZip', {zip: zip}, callback);
},
allForLatLong: function(latitude, longitude, callback) {
this.parent._call('legislators.allForLatLong',
{latitude: latitude, longitude: longitude}, callback);
}
}
var CommitteeClient = function(parent) {
this.parent = parent;
}
CommitteeClient.prototype = {
get: function(id, callback) {
this.parent._call('committees.get', {id: id}, callback);
},
getList: function(chamber, callback) {
this.parent._call('committees.getList', {chamber: chamber}, callback);
},
allForLegislator: function(bioguide_id, callback) {
this.parent._call('committees.allForLegislator',
{bioguide_id: bioguide_id}, callback);
}
}
var DistrictClient = function(parent) {
this.parent = parent;
}
DistrictClient.prototype = {
getDistrictsFromZip: function(zip, callback) {
this.parent._call('districts.getDistrictsFromZip', {zip: zip}, callback);
},
getDistrictFromLatLong: function(latitude, longitude, callback) {
this.parent._call('districts.getDistrictFromLatLong',
{latitude: latitude, longitude: longitude}, callback);
}
}