-
Notifications
You must be signed in to change notification settings - Fork 3
/
authors.rb
53 lines (46 loc) · 1.09 KB
/
authors.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
require 'sinatra'
require 'sinatra/json'
require_relative 'ext/failure_rate'
require_relative 'models/author'
require_relative 'resources/book'
get '/authors.json' do
authors = Author.where(params.slice(*Author.attribute_names)).order(params[:order])
json authors
end
get '/authors/:id.json' do
if author = Author.find_by_id(params[:id])
json author
else
404
end
end
post '/authors.json' do
author = Author.new(JSON.parse(request.body.read))
if author.save
[201, {'Location' => "/authors/#{author.id}.json"}, author.to_json]
else
[422, author.errors.to_json]
end
end
put '/authors/:id.json' do
if author = Author.find_by_id(params[:id])
if author.update(JSON.parse(request.body.read).slice(*%w{first_name last_name}))
[200, {'Location' => "/authors/#{author.id}.json"}, author.to_json]
else
[422, author.errors.to_json]
end
else
404
end
end
delete '/authors/:id.json' do
if author = Author.find_by_id(params[:id])
author.destroy
204
else
404
end
end
get '/ping' do
[200, {"Content-Type" => "text/plain"}, "pong"]
end