-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.rb
More file actions
39 lines (33 loc) · 797 Bytes
/
server.rb
File metadata and controls
39 lines (33 loc) · 797 Bytes
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
require 'rubygems'
require 'sinatra'
require 'json'
configure do
set :method_override, true
set :tasks, [
{ 'id' => 1, 'title' => "Get Passport" },
{ 'id' => 2, 'title' => "Book tickets" },
{ 'id' => 3, 'title' => "Fly to London" },
{ 'id' => 4, 'title' => "Talk!" }
]
end
get '/' do
erb :index, :locals => { :tasks => settings.tasks }
end
post '/tasks' do
task = JSON.parse(request.body.read)
task['id'] = rand(10000);
settings.tasks << task
task.to_json
end
get "/tasks/:id" do
erb :show, :locals => { :task => find_task(params[:id]) }, :layout => false
end
delete "/tasks/:id" do
settings.tasks.reject! { |task| task["id"] == params[:id].to_i }
""
end
helpers do
def find_task(id)
settings.tasks.find { |task| task["id"] == id.to_i }
end
end