166ab5fad0b3d873a718f2436712f3df341d451f
[arvados.git] / app / controllers / orvos / v1 / schema_controller.rb
1 class Orvos::V1::SchemaController < ApplicationController
2   skip_before_filter :find_object_by_uuid
3   skip_before_filter :login_required
4
5   def show
6     classes = Rails.cache.fetch 'orvos_v1_schema' do
7       Rails.application.eager_load!
8       classes = {}
9       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
10         classes[k] = k.columns.collect do |col|
11           if k.serialized_attributes.has_key? col.name
12             { name: col.name,
13               type: k.serialized_attributes[col.name].object_class.to_s }
14           else
15             { name: col.name,
16               type: col.type }
17           end
18         end
19       end
20       classes
21     end
22     render json: classes
23   end
24
25   def discovery_rest_description
26     discovery = Rails.cache.fetch 'orvos_v1_rest_discovery' do
27       Rails.application.eager_load!
28       discovery = {
29         kind: "discovery#restDescription",
30         discoveryVersion: "v1",
31         id: "orvos:v1",
32         name: "orvos",
33         version: "v1",
34         revision: "20130226",
35         title: "Orvos API",
36         description: "The API to interact with Orvos.",
37         documentationLink: "https://redmine.clinicalfuture.com/projects/orvos/",
38         protocol: "rest",
39         baseUrl: root_url + "/orvos/v1/",
40         basePath: "/orvos/v1/",
41         rootUrl: root_url,
42         servicePath: "orvos/v1/",
43         batchPath: "batch",
44         parameters: {
45           alt: {
46             type: "string",
47             description: "Data format for the response.",
48             default: "json",
49             enum: [
50                    "json"
51                   ],
52             enumDescriptions: [
53                                "Responses with Content-Type of application/json"
54                               ],
55             location: "query"
56           },
57           fields: {
58             type: "string",
59             description: "Selector specifying which fields to include in a partial response.",
60             location: "query"
61           },
62           key: {
63             type: "string",
64             description: "API key. Your API key identifies your project and provides you with API access, quota, and reports. Required unless you provide an OAuth 2.0 token.",
65             location: "query"
66           },
67           oauth_token: {
68             type: "string",
69             description: "OAuth 2.0 token for the current user.",
70             location: "query"
71           }
72         },
73         auth: {
74           oauth2: {
75             scopes: {
76               "https://api.clinicalfuture.com/auth/orvos" => {
77                 description: "View and manage objects"
78               },
79               "https://api.clinicalfuture.com/auth/orvos.readonly" => {
80                 description: "View objects"
81               }
82             }
83           }
84         },
85         schemas: {},
86         resources: {}
87       }
88       
89       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
90         next if k == ApiClientAuthorization
91         next if k == ApiClient
92         object_properties = {}
93         k.columns.
94           select { |col| col.name != 'id' }.
95           collect do |col|
96           if k.serialized_attributes.has_key? col.name
97             object_properties[col.name] = {
98               type: k.serialized_attributes[col.name].object_class.to_s
99             }
100           else
101             object_properties[col.name] = {
102               type: col.type
103             }
104           end
105         end
106         discovery[:schemas][k.to_s + 'List'] = {
107           id: k.to_s,
108           description: k.to_s,
109           type: "object",
110           properties: {
111             kind: {
112               type: "string",
113               description: "Object type. Always orvos##{k.to_s.camelcase(:lower)}List.",
114               default: "orvos##{k.to_s.camelcase(:lower)}List"
115             },
116             etag: {
117               type: "string",
118               description: "List version."
119             },
120             items: {
121               type: "array",
122               description: "The list of #{k.to_s.pluralize}.",
123               items: {
124                 "$ref" => k.to_s
125               }
126             },
127             next_link: {
128               type: "string",
129               description: "A link to the next page of #{k.to_s.pluralize}."
130             },
131             next_page_token: {
132               type: "string",
133               description: "The page token for the next page of #{k.to_s.pluralize}."
134             },
135             selfLink: {
136               type: "string",
137               description: "A link back to this list."
138             }
139           }
140         }
141         discovery[:schemas][k.to_s] = {
142           id: k.to_s,
143           description: k.to_s,
144           type: "object",
145           properties: {
146             uuid: {
147               type: "string",
148               description: "Object ID."
149             },
150             etag: {
151               type: "string",
152               description: "Object version."
153             }
154           }.merge(object_properties)
155         }
156         discovery[:resources][k.to_s.underscore.pluralize] = {
157           methods: {
158             get: {
159               id: "orvos.#{k.to_s.underscore.pluralize}.get",
160               path: "#{k.to_s.underscore.pluralize}/{uuid}",
161               httpMethod: "GET",
162               description: "Gets a #{k.to_s}'s metadata by UUID.",
163               parameters: {
164                 uuid: {
165                   type: "string",
166                   description: "The UUID of the #{k.to_s} in question.",
167                   required: true,
168                   location: "path"
169                 }
170               },
171               parameterOrder: [
172                                "uuid"
173                               ],
174               response: {
175                 "$ref" => k.to_s
176               },
177               scopes: [
178                        "https://api.clinicalfuture.com/auth/orvos",
179                        "https://api.clinicalfuture.com/auth/orvos.readonly"
180                       ]
181             },
182             list: {
183               id: "orvos.#{k.to_s.underscore.pluralize}.list",
184               path: k.to_s.underscore.pluralize,
185               httpMethod: "GET",
186               description: "List #{k.to_s.underscore.pluralize}.",
187               parameters: {
188                 limit: {
189                   type: "integer",
190                   description: "Maximum number of #{k.to_s.underscore.pluralize} to return.",
191                   default: "100",
192                   format: "int32",
193                   minimum: "0",
194                   location: "query"
195                 },
196                 pageToken: {
197                   type: "string",
198                   description: "Page token.",
199                   location: "query"
200                 },
201                 q: {
202                   type: "string",
203                   description: "Query string for searching #{k.to_s.underscore.pluralize}.",
204                   location: "query"
205                 }
206               },
207               response: {
208                 "$ref" => "#{k.to_s}List"
209               },
210               scopes: [
211                        "https://api.clinicalfuture.com/auth/orvos",
212                        "https://api.clinicalfuture.com/auth/orvos.readonly"
213                       ]
214             },
215             create: {
216               id: "orvos.#{k.to_s.underscore.pluralize}.create",
217               path: "#{k.to_s.underscore.pluralize}",
218               httpMethod: "POST",
219               description: "Create a new #{k.to_s}.",
220               parameters: {
221                 k.to_s.underscore => {
222                   type: "object",
223                   required: true,
224                   location: "query",
225                   properties: object_properties
226                 }
227               },
228               request: {
229                 "$ref" => k.to_s
230               },
231               response: {
232                 "$ref" => k.to_s
233               },
234               scopes: [
235                        "https://api.clinicalfuture.com/auth/orvos"
236                       ]
237             },
238             update: {
239               id: "orvos.#{k.to_s.underscore.pluralize}.update",
240               path: "#{k.to_s.underscore.pluralize}/{uuid}",
241               httpMethod: "PUT",
242               description: "Update attributes of an existing #{k.to_s}.",
243               parameters: {
244                 uuid: {
245                   type: "string",
246                   description: "The UUID of the #{k.to_s} in question.",
247                   required: true,
248                   location: "path"
249                 },
250                 k.to_s.underscore => {
251                   type: "object",
252                   required: true,
253                   location: "query",
254                   properties: object_properties
255                 }
256               },
257               request: {
258                 "$ref" => k.to_s
259               },
260               response: {
261                 "$ref" => k.to_s
262               },
263               scopes: [
264                        "https://api.clinicalfuture.com/auth/orvos"
265                       ]
266             },
267             delete: {
268               id: "orvos.#{k.to_s.underscore.pluralize}.delete",
269               path: "#{k.to_s.underscore.pluralize}/{uuid}",
270               httpMethod: "DELETE",
271               description: "Delete an existing #{k.to_s}.",
272               parameters: {
273                 uuid: {
274                   type: "string",
275                   description: "The UUID of the #{k.to_s} in question.",
276                   required: true,
277                   location: "path"
278                 }
279               },
280               response: {
281                 "$ref" => k.to_s
282               },
283               scopes: [
284                        "https://api.clinicalfuture.com/auth/orvos"
285                       ]
286             }
287           }
288         }
289       end
290       discovery
291     end
292     render json: discovery
293   end
294 end