-
Notifications
You must be signed in to change notification settings - Fork 65
/
congress.rb
executable file
·308 lines (243 loc) · 8.31 KB
/
congress.rb
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env ruby
require './config/environment'
require './api/sunlight'
require './api/rss'
include Api::Routes
helpers Api::Helpers
get queryable_route do
check_key!
model = params[:captures][0].singularize.camelize.constantize rescue nil
format = format_for params
fields = fields_for model, params
filters = filters_for model, params
order = order_for params, Queryable.default_sort(model).nil? ? "_id" : Queryable.default_sort(model)
# allow a pagination exception for legislators and committees, by request
if [Legislator, Committee].include?(model) and (params[:per_page] == "all")
pagination = {page: nil, per_page: nil}
else
pagination = pagination_for params
end
query_string = query_string_for params
conditions = Queryable.conditions_for model, query_string, filters, params
if params[:explain] == 'true'
results = Queryable.explain_for model, conditions, fields, order, pagination
else
criteria = Queryable.criteria_for model, conditions, fields, order, pagination
documents = Queryable.documents_for model, criteria, fields
results = Queryable.results_for criteria, documents, pagination
results[:results] = Citable.add_to(model, results[:results], params) if results[:results]
end
hit! "query", format
if format == 'json'
json results
elsif format == 'rss'
rss results, model, params
elsif format == 'rss-json'
jss results, model, params
elsif format == 'xml'
xml results
end
end
get searchable_route do
check_key!
models = params[:captures][0].split(",").map {|m| m.singularize.camelize.constantize rescue nil}.compact
format = format_for params
fields = fields_for models, params
pagination = pagination_for params
filters = filters_for models, params
order = order_for params, "_score"
search_fields = Searchable.search_fields_for models
profiles = Searchable.profiles_for models, params
# query is actually optional, these may both end up null
query_string = query_string_for params
query = Searchable.query_for query_string, params, search_fields
filter = Searchable.filter_for filters
other = Searchable.other_options_for params, search_fields
if params[:explain] == 'true'
results = Searchable.explain_for query_string, models, query, filter, fields, order, pagination, profiles, other
else
results = Searchable.search_for query_string, models, query, filter, fields, order, pagination, profiles, other
results[:results] = Citable.add_to(models, results[:results], params) if results[:results]
end
hit! "search", format
if format == 'json'
json results
elsif format == 'rss'
rss results, models, params
elsif format == 'rss-json'
jss results, model, params
elsif format == 'xml'
xml results
end
end
# special endpoints for doing lat/lng and zip-based lookups on legislators and districts
# uses mix of methods to return information, using helper methods in Location module
get(/(legislators|districts)\/locate\/?/) do
check_key!
error 400, "Provide a 'zip', or a 'latitude' and 'longitude'." unless params[:zip] or (params[:latitude] and params[:longitude])
begin
if params[:zip]
if zip = Zip.where(zip: params[:zip]).first
districts = zip['districts']
location = {zip: params[:zip], districts: zip['districts']}
else
districts = []
end
details = {zip: params[:zip]}
elsif params[:latitude] and params[:longitude]
url = Location.url_for params[:latitude], params[:longitude]
start = Time.now
response = Location.response_for url
elapsed = Time.now - start
districts = Location.response_to_districts response
location = {latitude: params[:latitude], longitude: params[:longitude], response: response, elapsed: elapsed, districts: districts}
end
rescue Location::LocationException => ex
error 500, ex.message
end
if params[:captures][0] == "legislators"
if districts.any?
model = Legislator
fields = fields_for model, params
pagination = pagination_for params
order = order_for params, "_id"
conditions = Location.district_to_legislators districts
if params[:explain] == 'true'
explain = Queryable.explain_for model, conditions, fields, order, pagination
results = {location: location, query: explain}
else
criteria = Queryable.criteria_for model, conditions, fields, order, pagination
documents = Queryable.documents_for model, criteria, fields
results = Queryable.results_for criteria, documents, pagination
end
else # districts
if params[:explain]
results = {location: location}
else
results = {results: [], count: 0}
end
end
else # districts
if params[:explain]
results = {location: location, districts: districts}
else
results = {results: districts, count: districts.size}
end
end
hit! "locate", "json"
json results
end
get '/' do
result = {}
if request.env['HTTP_X_FORWARDED_PROTO'] == 'http'
result[:please_use_the_https_endpoint_at] = "https://congress.api.sunlightfoundation.com"
result[:please] = "https://congress.api.sunlightfoundation.com"
elsif request.env['HTTP_X_FORWARDED_PROTO'] == 'https'
result[:thank_you] = "for using https"
end
json(result.merge(
status: 200,
message: "I live!",
documentation: "https://sunlightlabs.github.io/congress/",
code: "https://github.com/propublica/congress",
report_bugs: "https://github.com/propublica/congress/issues"
))
end
# error handler and test
get '/error' do
raise Exception.new("KABOOM.")
end
error do
exception = env['sinatra.error']
name = exception.class.name
message = exception.message
request = {
method: env['REQUEST_METHOD'],
url: env['REQUEST_URI'],
params: params.inspect,
user_agent: env['HTTP_USER_AGENT']
}
Email.report Report.exception("Exception Notifier", "#{name}: #{message}", exception, request: request)
halt 500
end
helpers do
def json(results)
response['Content-Type'] = 'application/json; charset=utf-8'
json = Oj.dump results, mode: :compat, time_format: :ruby
if jsonp?
"#{params[:callback]}(#{json});"
else
json
end
end
def jsonp?
!!(params[:callback].present?) and (params[:callback] =~ /^[\.a-zA-Z0-9\$_]+$/)
end
def xml(results)
response['Content-Type'] = 'application/xml; charset=utf-8'
results.to_xml root: 'results', dasherize: false
end
def error(status, message)
format = format_for params
results = {
error: message,
status: status
}
if format == "json"
halt( (jsonp? ? 200 : status), json(results))
else
halt status, xml(results)
end
end
def check_key!
#if Environment.check_key? and !ApiKey.allowed?(api_key)
# error 403, 'API key required, you can obtain one from http://services.sunlightlabs.com/accounts/register/'
#end
end
def api_key
params[:apikey] || request.env['HTTP_X_APIKEY']
end
def hit!(method_type, format)
return if params[:explain]
method = params[:captures][0]
key = api_key || "debug"
now = Time.zone.now
extras = {}
if method_type == "locate"
if params[:zip]
method += ".zip"
extras[:zip] = params[:zip]
elsif params[:latitude] and params[:longitude]
method += ".latlng"
# Specifically do NOT store latitude and longitude.
# We promise this in our API documentation.
end
elsif method_type == "search"
method += ".search"
extras[:query] = params[:query]
end
hit = Hit.create!(
key: key,
method: method,
method_type: method_type,
format: format,
extras: extras,
protocol: request.env['HTTP_X_FORWARDED_PROTO'],
user_agent: request.env['HTTP_USER_AGENT'],
app_version: request.env['HTTP_X_APP_VERSION'],
os_version: request.env['HTTP_X_OS_VERSION'],
app_channel: request.env['HTTP_X_APP_CHANNEL'],
created_at: now.utc,
elapsed: ((now - request.env['timer']) * 1000000).to_i
)
HitReport.log! now.strftime("%Y-%m-%d"), key, method
SearchReport.log! extras[:query], method if extras[:query]
end
end
before {request.env['timer'] = Time.now}
before do
NewRelic::Agent.add_custom_parameters(
endpoint: request.path_info,
params: clean_params(params.clone)
)
end