1 # Copyright 2010 Google Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 require 'addressable/uri'
21 require 'addressable/template'
23 require 'google/inflection'
24 require 'google/api_client/errors'
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']
35 # Due to an oversight, schema IDs may not be URI references.
36 # TODO(bobaman): Remove this code once this has been resolved.
39 (schema_name[0..0] != '#' ? '#' + schema_name : schema_name)
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
50 if data.kind_of?(Hash) && data['$ref']
51 reference = data['$ref']
52 reference = '#' + reference if reference[0..0] != '#'
56 elsif data.kind_of?(Hash)
57 data.inject({}) do |accu, (key, value)|
58 if value.kind_of?(Hash)
59 accu[key] = reformat_references.call(value)
69 schema_data = reformat_references.call(schema_data)
70 # puts schema_data.inspect
74 Google::INFLECTOR.camelize(api.name)
76 Google::INFLECTOR.camelize(api.version).gsub('.', '_')
77 if Google::APIClient::Schema.const_defined?(api_name_string, false)
78 api_name = Google::APIClient::Schema.const_get(api_name_string, false)
80 api_name = Google::APIClient::Schema.const_set(
81 api_name_string, Module.new
84 if api_name.const_defined?(api_version_string, false)
85 api_version = api_name.const_get(api_version_string, false)
87 api_version = api_name.const_set(api_version_string, Module.new)
89 if api_version.const_defined?(schema_name, false)
90 schema_class = api_version.const_get(schema_name, false)
94 # It's possible the schema has already been defined. If so, don't
95 # redefine it. This means that reloading a schema which has already
96 # been loaded into memory is not possible.
98 schema_class = AutoParse.generate(schema_data, :uri => schema_uri)
100 api_version.const_set(schema_name, schema_class)