-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_application.rb
50 lines (39 loc) · 1.03 KB
/
rest_application.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
class RestApplication < Sinatra::Base
configure do
# Don't log them. We'll do that ourself
set :dump_errors, false
# Don't capture any errors. Throw them up the stack
set :raise_errors, true
# Disable internal middleware for presenting errors
# as useful HTML pages
set :show_exceptions, false
end
helpers do
def parsed_body
::MultiJson.decode(request.body)
end
end
get %r{^/$|(^/posts.*)} do
send_file File.expand_path('index.html', settings.public_folder)
end
get "/api/posts" do
content_type :json
Post.all.to_json
end
post "/api/posts" do
content_type :json
Post.create(parsed_body.select_keys('name', 'body')).to_json
end
put '/api/posts/:post_id' do
content_type :json
Post.update(params[:post_id], parsed_body.select_keys('name', 'body')).to_json
end
get '/api/posts/:post_id' do
content_type :json
Post.find(params[:post_id]).to_json
end
delete '/api/posts/:post_id' do
Post.find(params[:post_id]).destroy()
end
private
end