Switched to using AutoParse for schemas.
[arvados.git] / lib / google / api_client / discovery / schema.rb
1 # Copyright 2010 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15
16 require 'time'
17 require 'json'
18 require 'base64'
19 require 'autoparse'
20 require 'addressable/uri'
21 require 'addressable/template'
22
23 require 'google/inflection'
24 require 'google/api_client/errors'
25
26 module Google
27   class APIClient
28     module Schema
29       def self.parse(api, schema_data)
30         # This method is super-long, but hard to break up due to the
31         # unavoidable dependence on closures and execution context.
32         schema_name = schema_data['id']
33
34         if schema_name
35           api_name_string =
36             Google::INFLECTOR.camelize(api.name)
37           api_version_string =
38             Google::INFLECTOR.camelize(api.version).gsub('.', '_')
39           if Google::APIClient::Schema.const_defined?(api_name_string)
40             api_name = Google::APIClient::Schema.const_get(api_name_string)
41           else
42             api_name = Google::APIClient::Schema.const_set(
43               api_name_string, Module.new
44             )
45           end
46           if api_name.const_defined?(api_version_string)
47             api_version = api_name.const_get(api_version_string)
48           else
49             api_version = api_name.const_set(api_version_string, Module.new)
50           end
51           if api_version.const_defined?(schema_name)
52             schema_class = api_version.const_get(schema_name)
53           end
54         end
55
56         # It's possible the schema has already been defined. If so, don't
57         # redefine it. This means that reloading a schema which has already
58         # been loaded into memory is not possible.
59         unless schema_class
60           schema_class = AutoParse.generate(schema_data)
61           if schema_name
62             api_version.const_set(schema_name, schema_class)
63           end
64         end
65         return schema_class
66       end
67     end
68   end
69 end