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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/rack/autocrud.rb', line 33
def _call(env)
model_klass = nil
endpoint_klass = nil
verb,endpoint,*uri = env['PATH_INFO'].split('/')
verb = env['REQUEST_METHOD'].downcase
return @app.call(env) if endpoint.nil?
ObjectSpace.each_object(Class) { |klass|
kname = String(klass.name).downcase
mname = String(@model_namespace + '::' + endpoint).downcase
ename = String(@endpoint_namespace + '::' + endpoint).downcase
model_klass = klass if kname == mname
endpoint_klass = klass if kname == ename
}
if @model_mod.nil?
ObjectSpace.each_object(Module) { |klass|
if String(klass.name).downcase == @model_namespace.downcase
@model_mod = klass
end
}
end
if endpoint_klass.nil? && @endpoint_mod.nil?
ObjectSpace.each_object(Module) { |klass|
if String(klass.name).downcase == @endpoint_namespace.downcase
@endpoint_mod = klass
end
}
end
if !model_klass.nil? && !model_klass.const_defined?(:EXPOSE) &&
@model_mod.const_defined?(:EXPOSE)
model_klass.const_set(:EXPOSE,@model_mod.const_get(:EXPOSE))
end
if !model_klass.nil? && !model_klass.const_defined?(:COLLECTABLE) &&
@model_mod.const_defined?(:COLLECTABLE)
model_klass.const_set(:COLLECTABLE,
@model_mod.const_get(:COLLECTABLE))
end
if !model_klass.nil? && (
!model_klass.const_defined?(:EXPOSE) ||
model_klass.const_get(:EXPOSE)
)
if endpoint_klass.nil?
endpoint_klass = Class.new(Sinatra::Base)
@endpoint_mod.const_set(endpoint.capitalize,endpoint_klass)
end
@includes.each { |inc|
endpoint_klass.send(:include,inc)
} unless @includes.nil?
@sinatra_opts.each { |sopt,val|
endpoint_klass.send(:set,sopt,val)
}
endpoint_klass.class_exec(model_klass,endpoint) { |model,ep|
def set_request_body(new_body,content_type='text/json')
env['rack.input'] = StringIO.new(new_body)
env['CONTENT_LENGTH'] = new_body.length
env['CONTENT_TYPE'] = content_type
return nil
end
get '/count' do
halt [
403,
'{ "error": "Access Denied" }'
] unless model_klass.const_defined?(:COLLECTABLE) &&
model.const_get(:COLLECTABLE)
{ :count => model.all.count }.to_json
end
get '/' do
halt [
403,
'{ "error": "Access Denied" }'
] unless model_klass.const_defined?(:COLLECTABLE) &&
model.const_get(:COLLECTABLE)
if self.respond_to?(:pre_collect)
ret = pre_collect(model,request,params)
return ret unless ret.nil?
end
collection = model.all
if self.respond_to?(:post_collect)
ret = post_collect(model,request,collection)
return ret unless ret.nil?
end
collection.to_json({},request.env)
end
post '/' do
if self.respond_to?(:pre_create)
ret = pre_create(model,request,params)
return ret unless ret.nil?
end
request.body.rewind if request.body.respond_to?(:rewind)
obj = nil
begin
obj = model.create(JSON.parse(request.body.read))
halt [
402,
'{ "error": "Failed to save ' + ep + '" }'
] unless obj && obj.saved?
rescue JSON::ParserError
halt [
400,
{
'error' => 'Invalid JSON in request body.',
'details' => $!
}.to_json
]
end
if self.respond_to?(:post_create)
ret = post_create(model,request,obj)
return ret unless ret.nil?
end
[ 201, { 'id' => obj.id.to_i }.to_json ]
end
get '/:id' do
if self.respond_to?(:pre_retrieve)
ret = pre_retrieve(model,request,params)
return ret unless ret.nil?
end
obj = model.get(params[:id])
if self.respond_to?(:post_retrieve)
ret = post_retrieve(model,request,obj)
return ret unless ret.nil?
end
obj.to_json({},request.env)
end
put '/:id' do
if self.respond_to?(:pre_update)
ret = pre_update(model,request,params)
return ret unless ret.nil?
end
request.body.rewind if request.body.respond_to?(:rewind)
begin
saved = model.get(params[:id]).update(
JSON.parse(request.body.read)
)
halt [ 402, '{ "error": "Access Denied" }' ] unless saved
rescue JSON::ParserError
halt [
400,
{
'error' => 'Invalid JSON in request body.',
'details' => $!
}.to_json
]
end
if self.respond_to?(:post_update)
ret = post_update(model,request,params)
return ret unless ret.nil?
end
[ 201, '{ "status": "ok" }' ]
end
delete '/:id' do
if self.respond_to?(:pre_destroy)
ret = pre_destroy(model,request,params)
return ret unless ret.nil?
end
obj = model.get(params[:id])
return [
402,
'{ "error": "Failed to delete ' + ep + '" }'
] unless obj && obj.destroy
if self.respond_to?(:post_destroy)
ret = post_destroy(model,request,obj)
return ret unless ret.nil?
end
[ 204 ]
end
}
old_path_info = env['PATH_INFO']
env['PATH_INFO'] = '/' + uri.join('/')
env['CONTENT_TYPE'] = 'text/json'
response = endpoint_klass.call(env)
env['PATH_INFO'] = old_path_info
return response
end
@app.call(env)
end
|