This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.rb
92 lines (79 loc) · 1.81 KB
/
service.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
require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/reloader' if development?
require'./environments'
require './models/post'
# the HTTP entry points to our service
# set content type for all requests
before do
content_type 'application/json'
end
# get all posts
get '/api/v1/posts' do
Post.all.to_json
end
# get a post by id
get '/api/v1/posts/:id' do
post = Post.find_by_id(params[:id])
if post
post.to_json
else
error 404, {:error => "post not found"}.to_json
end
end
# create a post
post '/api/v1/posts' do
begin
post = Post.create(JSON.parse(request.body.read))
if post.valid?
post.to_json
else
error 400, post.errors.to_json # :bad_request
end
rescue => e
error 400, {:error => e.message}.to_json
end
end
# update a post
put '/api/v1/posts/:id' do
post = Post.find_by_id(params['id'])
if post
begin
attributes = JSON.parse(request.body.read)
updated_post = post.update_attributes(attributes)
if updated_post
post.to_json
else
error 400, post.errors.to_json
end
rescue => e
error 400, {:error => e.message}.to_json
end
else
error 404, {:error => 'post not found'}.to_json
end
end
# delete a post
delete '/api/v1/posts/:id' do
post = Post.find_by_id(params[:id])
if post
post.destroy
post.to_json
else
error 404, {:error => 'post not found'}.to_json
end
end
#
# HELPERS
#
def show_request(request,log)
log.debug "request.request_method: #{request.request_method}"
log.debug "request.body: #{request.body}"
log.debug "request.cookies: #{request.cookies}"
log.debug "request.env: #{request.env}"
log.debug "request.content_length: #{request.content_length}"
log.debug "request.media_type: #{request.media_type}"
end
get '/foo' do
show_request(request, log)
end