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