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
|
# File 'lib/easy_api_operations.rb', line 4
def self.set_up_api(api, ressource_name, operations)
operations.downcase
ressource_name.capitalize
api_version = api.parent_name
http_codes_hash = {
200 => 'Ok',
400 => 'Invalid parameter entry',
403 => 'Not allowed',
404 => 'No appointment found'
}
= {500 => 'unable to create/update'}
if operations.include?("plural") or operations.include?("all")
api.resource ressource_name.downcase.pluralize.to_sym do
if operations.include?("plural_get") or operations.include?("all")
desc "Returns a list of #{ressource_name}. Limited by 1000", params: (api_version + "::Entities::" + ressource_name.pluralize).constantize.documentation get do
if params.empty?
present "Choose something dude"
else
results = ressource_name.constantize.where(EasyModelSelects.get_where_statement_from_param(params)).order(ressource_name.constantize.primary_key).limit(1000)
present results, with: (api_version + "::Entities::" + ressource_name.pluralize).constantize
end
end
else
end
end
else
end
if operations.include?("singular") or operations.include?("all")
api.resource ressource_name.downcase.to_sym do
if operations.include?("singular_primary_key") or operations.include?("all")
desc "Returns the primary key from #{ressource_name.pluralize}"
get "primary_key" do
ressource_name.constantize.primary_key.to_sym
end
else
end
if operations.include?("singular_get") or operations.include?("all")
desc "Returns #{ressource_name.indefinitize} by #{ressource_name.constantize.primary_key}"
params do
requires ressource_name.constantize.primary_key, type: Integer, desc: "#{ressource_name} #{ressource_name.constantize.primary_key}"
end
get "#{ressource_name.constantize.primary_key.to_sym}", http_codes: http_codes_hash do
instance_variable = ressource_name.constantize.where("#{ressource_name.constantize.primary_key} IN (?)", params[ressource_name.constantize.primary_key]).first
if instance_variable.nil?
error! http_codes_hash[404], 404
end
present instance_variable, with: (api_version + "::Entities::" + ressource_name).constantize
end
else
end
if operations.include?("singular_put") or operations.include?("all")
desc "Updates #{ressource_name.indefinitize} by #{ressource_name.constantize.primary_key}"
params do
optional :body, type: (api_version + "::Entities::" + ressource_name + "UpdateBody").constantize, desc: 'Body to update params'
end
put ressource_name.constantize.primary_key, http_codes: http_codes_hash.merge() do
instance_variable = ressource_name.constantize.where("#{ressource_name.constantize.primary_key} IN (?)", params[ressource_name.constantize.primary_key]).first
if instance_variable.nil?
error! http_codes_hash[404], 404
end
transformed_params = (api_version + "::Entities::" + ressource_name + "UpdateBody").constantize.represent(params, serializable: true)
transformed_params.keys.each do |key|
if transformed_params[key].nil?
transformed_params.delete(key)
end
end
if instance_variable.update!(transformed_params)
status 204
else
error! [500], 500
end
end
else
end
if operations.include?("singular_delete") or operations.include?("all")
desc "Deletes #{ressource_name.indefinitize} by #{ressource_name.constantize.primary_key}"
params do
requires ressource_name.constantize.primary_key, type: String, desc: "#{ressource_name} #{ressource_name.constantize.primary_key}"
end
delete ressource_name.constantize.primary_key, http_codes: http_codes_hash do
instance_variable = ressource_name.constantize.where("#{ressource_name.constantize.primary_key} IN (?)", params[ressource_name.constantize.primary_key]).first
if instance_variable.nil?
error! http_codes_hash[404], 404
else
instance_variable.destroy
status 204
end
end
else
end
if operations.include?("singular_post") or operations.include?("all")
desc "Creates #{ressource_name.indefinitize}" params do
optional :body, type: (api_version + "::Entities::" + ressource_name + "CreateBody").constantize, desc: "Body to create #{"Appointment".indefinitize}"
end
post http_codes: http_codes_hash.merge() do
transformed_params = (api_version + "::Entities::" + ressource_name + "CreateBody").constantize.represent(params, serializable: true)
transformed_params.keys.each do |key|
if transformed_params[key].nil?
transformed_params.delete(key)
end
end
instance_variable = ressource_name.constantize.create(transformed_params)
if instance_variable.save
body instance_variable.send("#{ressource_name.constantize.primary_key}")
else
error! [500], 500
end
end
else
end
end
else
end
end
|