-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
62 lines (52 loc) · 1.27 KB
/
app.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
require 'sinatra/base'
require 'sinatra/cross_origin'
require 'json'
class HswApi < Sinatra::Application
register Sinatra::CrossOrigin
configure do
# Enable compression for responses
use Rack::Deflater
# Override the default
set :public_folder, File.join(File.dirname(__FILE__), 'public')
# Declare ALL THE THINGS!
set :things, {
'bill' => Bill,
'contributor' => Contributor,
'committee' => Committee,
'politician' => Politician
}
end
helpers do
def jsonp(body)
callback = params.delete('callback')
if callback
content_type :js
body = "#{callback.gsub(/\W/, '')}(#{body})"
else
content_type :json
end
body
end
end
get '/' do
erb :app
end
before '/api/v1/*' do
cross_origin
end
get '/api/v1/:thing/:id' do
if(settings.things.has_key? params[:thing])
finder = settings.things[params[:thing]].dup
item = finder.find(params[:id])
if item # exists!
itemj = item.export.to_json
etag Digest::SHA1.base64digest itemj
jsonp itemj
else # don't know that item
halt 404
end # end item
else # don't know that type of thing
halt 404
end # thing finder
end # route
end # class