Merge remote branch 'remotes/upstream/master'
[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         # Due to an oversight, schema IDs may not be URI references.
35         # TODO(bobaman): Remove this code once this has been resolved.
36         schema_uri = (
37           api.document_base +
38           (schema_name[0..0] != '#' ? '#' + schema_name : schema_name)
39         )
40         # puts schema_uri
41
42         # Due to an oversight, schema IDs may not be URI references.
43         # TODO(bobaman): Remove this whole lambda once this has been resolved.
44         reformat_references = lambda do |data|
45           # This code is not particularly efficient due to recursive traversal
46           # and excess object creation, but this hopefully shouldn't be an
47           # issue since it should only be called only once per schema per
48           # process.
49           if data.kind_of?(Hash) && data['$ref']
50             reference = data['$ref']
51             reference = '#' + reference if reference[0..0] != '#'
52             data.merge({
53               '$ref' => reference
54             })
55           elsif data.kind_of?(Hash)
56             data.inject({}) do |accu, (key, value)|
57               if value.kind_of?(Hash)
58                 accu[key] = reformat_references.call(value)
59               else
60                 accu[key] = value
61               end
62               accu
63             end
64           else
65             data
66           end
67         end
68         schema_data = reformat_references.call(schema_data)
69         # puts schema_data.inspect
70
71         if schema_name
72           api_name_string =
73             Google::INFLECTOR.camelize(api.name)
74           api_version_string =
75             Google::INFLECTOR.camelize(api.version).gsub('.', '_')
76           if Google::APIClient::Schema.const_defined?(api_name_string)
77             api_name = Google::APIClient::Schema.const_get(api_name_string)
78           else
79             api_name = Google::APIClient::Schema.const_set(
80               api_name_string, Module.new
81             )
82           end
83           if api_name.const_defined?(api_version_string)
84             api_version = api_name.const_get(api_version_string)
85           else
86             api_version = api_name.const_set(api_version_string, Module.new)
87           end
88           if api_version.const_defined?(schema_name)
89             schema_class = api_version.const_get(schema_name)
90           end
91         end
92
93         # It's possible the schema has already been defined. If so, don't
94         # redefine it. This means that reloading a schema which has already
95         # been loaded into memory is not possible.
96         unless schema_class
97           schema_class = AutoParse.generate(schema_data, :uri => schema_uri)
98           if schema_name
99             api_version.const_set(schema_name, schema_class)
100           end
101         end
102         return schema_class
103       end
104     end
105   end
106 end