More carefully resolving the discovery-of-discovery issue.
[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 'multi_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
27 module Google
28   class APIClient
29     module Schema
30       def self.parse(api, schema_data)
31         # This method is super-long, but hard to break up due to the
32         # unavoidable dependence on closures and execution context.
33         schema_name = schema_data['id']
34
35         # Due to an oversight, schema IDs may not be URI references.
36         # TODO(bobaman): Remove this code once this has been resolved.
37         schema_uri = (
38           api.document_base +
39           (schema_name[0..0] != '#' ? '#' + schema_name : schema_name)
40         )
41         # puts schema_uri
42
43         # Due to an oversight, schema IDs may not be URI references.
44         # TODO(bobaman): Remove this whole lambda once this has been resolved.
45         reformat_references = lambda do |data|
46           # This code is not particularly efficient due to recursive traversal
47           # and excess object creation, but this hopefully shouldn't be an
48           # issue since it should only be called only once per schema per
49           # process.
50           if data.kind_of?(Hash) &&
51               data['$ref'] && !data['$ref'].kind_of?(Hash)
52             if data['$ref'].respond_to?(:to_str)
53               reference = data['$ref'].to_str
54             else
55               raise TypeError, "Expected String, got #{data['$ref'].class}"
56             end
57             reference = '#' + reference if reference[0..0] != '#'
58             data.merge({
59               '$ref' => reference
60             })
61           elsif data.kind_of?(Hash)
62             data.inject({}) do |accu, (key, value)|
63               if value.kind_of?(Hash)
64                 accu[key] = reformat_references.call(value)
65               else
66                 accu[key] = value
67               end
68               accu
69             end
70           else
71             data
72           end
73         end
74         schema_data = reformat_references.call(schema_data)
75         # puts schema_data.inspect
76
77         if schema_name
78           api_name_string =
79             Google::INFLECTOR.camelize(api.name)
80           api_version_string =
81             Google::INFLECTOR.camelize(api.version).gsub('.', '_')
82           # This is for compatibility with Ruby 1.8.7.
83           # TODO(bobaman) Remove this when we eventually stop supporting 1.8.7.
84           args = []
85           args << false if Class.method(:const_defined?).arity != 1
86           if Google::APIClient::Schema.const_defined?(api_name_string, *args)
87             api_name = Google::APIClient::Schema.const_get(
88               api_name_string, *args
89             )
90           else
91             api_name = Google::APIClient::Schema.const_set(
92               api_name_string, Module.new
93             )
94           end
95           if api_name.const_defined?(api_version_string, *args)
96             api_version = api_name.const_get(api_version_string, *args)
97           else
98             api_version = api_name.const_set(api_version_string, Module.new)
99           end
100           if api_version.const_defined?(schema_name, *args)
101             schema_class = api_version.const_get(schema_name, *args)
102           end
103         end
104
105         # It's possible the schema has already been defined. If so, don't
106         # redefine it. This means that reloading a schema which has already
107         # been loaded into memory is not possible.
108         unless schema_class
109           schema_class = AutoParse.generate(schema_data, :uri => schema_uri)
110           if schema_name
111             api_version.const_set(schema_name, schema_class)
112           end
113         end
114         return schema_class
115       end
116     end
117   end
118 end