-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
app.rb
228 lines (194 loc) · 4.95 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
## Thing
# RESTful API example
# - manages single resource called Thing /thing
# - all results (including error messages) returned as JSON (Accept header)
## requires
require 'sinatra'
require 'json'
require 'time'
require 'pp'
### datamapper requires
require 'data_mapper'
require 'dm-types'
require 'dm-timestamps'
require 'dm-validations'
## model
### helper modules
#### StandardProperties
module StandardProperties
def self.included(other)
other.class_eval do
property :id, other::Serial
# property :created_at, DateTime
# property :updated_at, DateTime
end
end
end
#### Validations
module Validations
def valid_id?(id)
id && id.to_s =~ /^\d+$/
end
end
### Thing
class Thing
include DataMapper::Resource
include StandardProperties
extend Validations
property :name, String, :required => true
property :status, String
end
## set up db
env = ENV["RACK_ENV"]
puts "RACK_ENV: #{env}"
if env.to_s.strip == ""
abort "Must define RACK_ENV (used for db name)"
end
case env
when "test"
DataMapper.setup(:default, "sqlite3::memory:")
else
DataMapper.setup(:default, "sqlite3:#{ENV["RACK_ENV"]}.db")
end
## create schema if necessary
DataMapper.auto_upgrade!
## logger
def logger
@logger ||= Logger.new(STDOUT)
end
## ThingResource application
class ThingResource < Sinatra::Base
set :methodoverride, true
## helpers
def self.put_or_post(*a, &b)
put *a, &b
post *a, &b
end
helpers do
def json_status(code, reason)
status code
{
:status => code,
:reason => reason
}.to_json
end
def accept_params(params, *fields)
h = { }
fields.each do |name|
h[name] = params[name] if params[name]
end
h
end
end
## GET /thing - return all things
get "/thing/?", :provides => :json do
content_type :json
if things = Thing.all
things.to_json
else
json_status 404, "Not found"
end
end
## GET /thing/:id - return thing with specified id
get "/thing/:id", :provides => :json do
content_type :json
# check that :id param is an integer
if Thing.valid_id?(params[:id])
if thing = Thing.first(:id => params[:id].to_i)
thing.to_json
else
json_status 404, "Not found"
end
else
# TODO: find better error for this (id not an integer)
json_status 404, "Not found"
end
end
## POST /thing/ - create new thing
post "/thing/?", :provides => :json do
content_type :json
new_params = accept_params(params, :name, :status)
thing = Thing.new(new_params)
if thing.save
headers["Location"] = "/thing/#{thing.id}"
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5
status 201 # Created
thing.to_json
else
json_status 400, thing.errors.to_hash
end
end
## PUT /thing/:id/:status - change a thing's status
put_or_post "/thing/:id/status/:status", :provides => :json do
content_type :json
if Thing.valid_id?(params[:id])
if thing = Thing.first(:id => params[:id].to_i)
thing.status = params[:status]
if thing.save
thing.to_json
else
json_status 400, thing.errors.to_hash
end
else
json_status 404, "Not found"
end
else
json_status 404, "Not found"
end
end
## PUT /thing/:id - change or create a thing
put "/thing/:id", :provides => :json do
content_type :json
new_params = accept_params(params, :name, :status)
if Thing.valid_id?(params[:id])
if thing = Thing.first_or_create(:id => params[:id].to_i)
thing.attributes = thing.attributes.merge(new_params)
if thing.save
thing.to_json
else
json_status 400, thing.errors.to_hash
end
else
json_status 404, "Not found"
end
else
json_status 404, "Not found"
end
end
## DELETE /thing/:id - delete a specific thing
delete "/thing/:id/?", :provides => :json do
content_type :json
if thing = Thing.first(:id => params[:id].to_i)
thing.destroy!
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7
status 204 # No content
else
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2
# Note: section 9.1.2 states:
# Methods can also have the property of "idempotence" in that
# (aside from error or expiration issues) the side-effects of
# N > 0 identical requests is the same as for a single
# request.
# i.e that the /side-effects/ are idempotent, not that the
# result of the /request/ is idempotent, so I think it's correct
# to return a 404 here.
json_status 404, "Not found"
end
end
## misc handlers: error, not_found, etc.
get "*" do
status 404
end
put_or_post "*" do
status 404
end
delete "*" do
status 404
end
not_found do
json_status 404, "Not found"
end
error do
json_status 500, env['sinatra.error'].message
end
end