generate discovery document at /orvos/v1/rest. refs #1406
[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 'discovery_rest_description' 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 'discovery_rest_description' 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         object_properties = {}
91         k.columns.
92           select { |col| col.name != 'id' }.
93           collect do |col|
94           if k.serialized_attributes.has_key? col.name
95             object_properties[col.name] = {
96               type: k.serialized_attributes[col.name].object_class.to_s
97             }
98           else
99             object_properties[col.name] = {
100               type: col.type
101             }
102           end
103         end
104         discovery[:schemas][k.to_s + 'List'] = {
105           id: k.to_s,
106           description: k.to_s,
107           type: "object",
108           properties: {
109             kind: {
110               type: "string",
111               description: "Object type. Always orvos##{k.to_s.camelcase(:lower)}List.",
112               default: "orvos##{k.to_s.camelcase(:lower)}List"
113             },
114             etag: {
115               type: "string",
116               description: "List version."
117             },
118             items: {
119               type: "array",
120               description: "The list of #{k.to_s.pluralize}."
121             },
122             next_link: {
123               type: "string",
124               description: "A link to the next page of #{k.to_s.pluralize}."
125             },
126             next_page_token: {
127               type: "string",
128               description: "The page token for the next page of #{k.to_s.pluralize}."
129             },
130             selfLink: {
131               type: "string",
132               description: "A link back to this list."
133             }
134           }
135         }
136         discovery[:schemas][k.to_s] = {
137           id: k.to_s,
138           description: k.to_s,
139           type: "object",
140           properties: {
141             uuid: {
142               type: "string",
143               description: "Object ID."
144             },
145             etag: {
146               type: "string",
147               description: "Object version."
148             }
149           }.merge(object_properties)
150         }
151         discovery[:resources][k.to_s.underscore.pluralize] = {
152           methods: {
153             get: {
154               id: "orvos.#{k.to_s.underscore.pluralize}.get",
155               path: "#{k.to_s.underscore.pluralize}/{uuid}",
156               httpMethod: "GET",
157               description: "Gets a #{k.to_s}'s metadata by ID.",
158               parameters: {
159                 uuid: {
160                   type: "string",
161                   description: "The ID for the #{k.to_s} in question.",
162                   required: true,
163                   location: "path"
164                 }
165               },
166               parameterOrder: [
167                                "uuid"
168                               ],
169               response: {
170                 "$ref" => k.to_s
171               },
172               scopes: [
173                        "https://api.clinicalfuture.com/auth/orvos",
174                        "https://api.clinicalfuture.com/auth/orvos.readonly"
175                       ]
176             },
177             list: {
178               id: "orvos.#{k.to_s.underscore.pluralize}.list",
179               path: k.to_s.underscore.pluralize,
180               httpMethod: "GET",
181               description: "List #{k.to_s.underscore.pluralize}.",
182               parameters: {
183                 limit: {
184                   type: "integer",
185                   description: "Maximum number of #{k.to_s.underscore.pluralize} to return.",
186                   default: "100",
187                   format: "int32",
188                   minimum: "0",
189                   location: "query"
190                 },
191                 pageToken: {
192                   type: "string",
193                   description: "Page token.",
194                   location: "query"
195                 },
196                 q: {
197                   type: "string",
198                   description: "Query string for searching #{k.to_s.underscore.pluralize}.",
199                   location: "query"
200                 }
201               },
202               response: {
203                 "$ref" => "#{k.to_s.pluralize}List"
204               },
205               scopes: [
206                        "https://api.clinicalfuture.com/auth/orvos",
207                        "https://api.clinicalfuture.com/auth/orvos.readonly"
208                       ]
209             }
210           }
211         }
212       end
213       discovery
214     end
215     render json: discovery
216   end
217 end