-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathopenstates.js
141 lines (118 loc) · 5.18 KB
/
openstates.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
var qs = require('querystring'),
request = require('request');
var OpenStates = module.exports = function(apiKey) {
if (!(this instanceof OpenStates)) { return new OpenStates(apiKey) }
if (!apiKey) throw new Error('Must provide API Key');
this.key = apiKey;
}
OpenStates.prototype.makeRequest = function(method, params, callback) {
// creates and executes an HTTP request
if (typeof callback != 'function') {
throw new Error('callback must be a function');
}
var options = this.createOptions(method, params, this.key);
return this.executeRequest(options, callback);
};
OpenStates.prototype.createOptions = function(method, params, key) {
// generates the options for the http request from the method, params, and key
var query = qs.stringify(params);
return {
url: 'http://openstates.org/api/v1/' + method + '/?' + 'apikey=' + key + '&' + query,
agent: false,
headers: {
"User-Agent": "Mozilla/4.0 (compatible; sunlight node.js client)",
"Content-type": "application/x-www-form-urlencoded"
}
};
};
OpenStates.prototype.executeRequest = function(options, callback) {
// executes the HTTP request with the given options
request(options, function(err, res, body) {
if (!err && res.statusCode == 200) {
callback(null, JSON.parse(body));
} else {
callback(new Error('Request failed with ' + res.statusCode));
}
});
};
// Get list of all states with data available and basic metadata about their status
OpenStates.prototype.metadataOverview = function(callback) {
this.makeRequest('metadata', {}, callback);
};
// Get detailed metadata for a particular state
OpenStates.prototype.metadataState = function(state, callback) {
this.makeRequest('metadata/' + state, {}, callback);
};
/* Search bills by (almost) any of their attributes, or full text
* For possible params, go to http://sunlightlabs.github.io/openstates-api/bills.html#methods/bill-search
*/
OpenStates.prototype.billSearch = function(params, callback) {
if (typeof params != 'object') throw new Error('The first argument of billSearch must be an object with the parameters');
this.makeRequest('bills', params, callback);
};
/* Search bill by id
* bills/openstates_bill_id , alternate lookup - allows lookup by bill_id
*/
OpenStates.prototype.getBillById = function(openstates_unique_id, callback) {
if (typeof params == 'object') throw new Error('The first argument should be a openstates bill id , (not the HB 4566, but the unique id ie: MAB00011570)');
this.makeRequest('bills/' + openstates_unique_id, {} ,callback);
};
// Get full detail for bill, including any actions, votes, etc.
OpenStates.prototype.billDetail = function(state, session, id, callback) {
id = encodeURIComponent(id);
this.makeRequest('bills/' + state + '/' + session + '/' + id, {}, callback);
};
/* Search legislators by their attributes.
* For possible params, go to http://sunlightlabs.github.io/openstates-api/legislators.html#methods/legislator-search
*/
OpenStates.prototype.legSearch = function(params, callback) {
if (typeof params != 'object') throw new Error('The first argument of legSearch must be an object with the parameters');
this.makeRequest('legislators', params, callback);
};
// Get full detail for a legislator, including all roles
OpenStates.prototype.legDetail = function(leg_id, callback) {
this.makeRequest('legislators/' + leg_id, {}, callback);
};
// Lookup all legislators that serve districts containing a given point
OpenStates.prototype.geoLookup = function(lat, long, callback) {
var params = {
lat: lat,
long: long
};
this.makeRequest('legislators/geo', params, callback);
};
/* Search committees by any of their attributes.
* For params go to http://sunlightlabs.github.io/openstates-api/committees.html#methods/committee-search
*/
OpenStates.prototype.comSearch = function(params, callback) {
if (typeof params != 'object') throw new Error('The first argument of comSearch must be an object with the parameters');
this.makeRequest('committees', params, callback);
};
// Get full detail for committee, including all members.
OpenStates.prototype.comDetail = function(com_id, callback) {
this.makeRequest('committees/' + com_id, {}, callback);
};
// Search events by state and type
OpenStates.prototype.eventSearch = function(params, callback) {
if (typeof params != 'object') throw new Error('The first argument of eventSearch must be an object with the parameters');
this.makeRequest('events', params, callback);
};
// Get full detail for event
OpenStates.prototype.eventDetail = function(event_id, callback) {
this.makeRequest('events/' + event_id, {}, callback);
};
// List districts for state (and optionally filtered by chamber)
OpenStates.prototype.districtSearch = function(state, chamber, callback) {
var params = {};
var urlPattern = 'districts/' + state;
if (arguments.length == 2 && typeof arguments[1] == 'function') {
callback = chamber;
} else {
urlPattern = urlPattern + '/' + chamber;
}
this.makeRequest(urlPattern, params, callback);
}
// Get geographic boundary for a district
OpenStates.prototype.districtBoundary = function(boundary_id, callback) {
this.makeRequest('districts/boundary/' + boundary_id, {}, callback);
};