-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.rb
124 lines (101 loc) · 2.9 KB
/
api.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
require 'sinatra/base'
require 'web_utils'
require 'populate_me/version'
require 'json'
class PopulateMe::API < Sinatra::Base
use Rack::MethodOverride
set :show_exceptions, false
before do
content_type :json
end
after do
redirect(params['_destination']) unless params['_destination'].nil?
end
get '/' do
status 200
{'success'=>true}.to_json
end
get '/version' do
status 200
{'success'=>true, 'version'=>PopulateMe::VERSION}.to_json
end
get '/:model' do
end
post '/:model' do
model_class = resolve_model_class params[:model]
model_instance = model_class.new.set_from_hash((params[:data]||{}), typecast: true)
if model_instance.valid?
model_instance.save
status 201
{
'success'=>true,'message'=>'Created Successfully',
'data'=>model_instance.to_h
}.to_json
else
status 400
{
'success'=>false,'message'=>'Invalid Document',
'data'=>model_instance.error_report
}.to_json
end
end
put '/:model' do
pass unless params[:action]=='sort'
model_class = resolve_model_class params[:model]
model_class.set_indexes(params[:field].to_sym,params[:ids])
{'success'=>true,'message'=>'Sorted Successfully'}.to_json
end
get '/:model/:id' do
model_class = resolve_model_class params[:model]
model_instance = resolve_model_instance model_class, params[:id]
{'success'=>true,'data'=>model_instance.to_h}.to_json
end
put '/:model/:id' do
model_class = resolve_model_class params[:model]
model_instance = resolve_model_instance model_class, params[:id]
model_instance.set_from_hash params[:data], typecast: true
if model_instance.valid?
model_instance.save
{
'success'=>true,'message'=>'Updated Successfully',
'data'=>model_instance.to_h
}.to_json
else
status 400
{
'success'=>false,'message'=>'Invalid Document',
'data'=>model_instance.error_report
}.to_json
end
end
delete '/:model/:id' do
model_class = resolve_model_class params[:model]
model_instance = resolve_model_instance model_class, params[:id]
model_instance.delete
{'success'=>true,'message'=>'Deleted Successfully','data'=>model_instance.to_h}.to_json
end
not_found do
response.headers['X-Cascade'] = 'pass'
{'success'=>false,'message'=>'Not Found'}.to_json
end
error do
puts
puts env['sinatra.error'].inspect
puts
{'success'=>false,'message'=>env['sinatra.error'].message}.to_json
end
module Helpers
include WebUtils
def resolve_model_class name
model_class = resolve_dasherized_class_name(name) rescue nil
halt(404) unless model_class.respond_to?(:admin_get)
model_class
end
def resolve_model_instance model_class, id
instance = model_class.admin_get id
halt(404) if instance.nil?
instance
end
end
helpers Helpers
end