-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
230 lines (196 loc) · 5.13 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
229
230
## Device
# RESTful API example
# - manages single resource called Device /device
# - 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
### Device
class Device
include DataMapper::Resource
include StandardProperties
extend Validations
property :name, String, :required => true
property :status, String
property :operatingSystem, String
property :version, 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
## DeviceResource application
class DeviceResource < 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 /devices - return all devices
get "/devices/?", :provides => :json do
content_type :json
if devices = Device.all
devices.to_json
else
json_status 404, "Not found"
end
end
## GET /device/:id - return device with specified id
get "/devices/:id", :provides => :json do
content_type :json
# check that :id param is an integer
if Device.valid_id?(params[:id])
if device = Device.first(:id => params[:id].to_i)
device.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 /device/ - create new device
post "/devices/?", :provides => :json do
content_type :json
new_params = accept_params(params, :name, :status, :operatingSystem, :version)
device = Device.new(new_params)
if device.save
headers["Location"] = "/devices/#{device.id}"
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5
status 201 # Created
device.to_json
else
json_status 400, device.errors.to_hash
end
end
## PUT /device/:id/:status - change a device's status
put_or_post "/devices/:id/status/:status", :provides => :json do
content_type :json
if Device.valid_id?(params[:id])
if device = Device.first(:id => params[:id].to_i)
device.status = params[:status]
if device.save
device.to_json
else
json_status 400, device.errors.to_hash
end
else
json_status 404, "Not found"
end
else
json_status 404, "Not found"
end
end
## PUT /device/:id - change or create a device
put "/devices/:id", :provides => :json do
content_type :json
new_params = accept_params(params, :name, :status, :operatingSystem, :version)
if Device.valid_id?(params[:id])
if device = Device.first_or_create(:id => params[:id].to_i)
device.attributes = device.attributes.merge(new_params)
if device.save
device.to_json
else
json_status 400, device.errors.to_hash
end
else
json_status 404, "Not found"
end
else
json_status 404, "Not found"
end
end
## DELETE /device/:id - delete a specific device
delete "/devices/:id/?", :provides => :json do
content_type :json
if device = Device.first(:id => params[:id].to_i)
device.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