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.
18 require 'compat/multi_json'
21 require 'addressable/uri'
22 require 'addressable/template'
24 require 'active_support/inflector'
25 require 'google/api_client/errors'
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']
38 # Due to an oversight, schema IDs may not be URI references.
39 # TODO(bobaman): Remove this code once this has been resolved.
42 (schema_name[0..0] != '#' ? '#' + schema_name : schema_name)
45 # Due to an oversight, schema IDs may not be URI references.
46 # TODO(bobaman): Remove this whole lambda once this has been resolved.
47 reformat_references = lambda do |data|
48 # This code is not particularly efficient due to recursive traversal
49 # and excess object creation, but this hopefully shouldn't be an
50 # issue since it should only be called only once per schema per
52 if data.kind_of?(Hash) &&
53 data['$ref'] && !data['$ref'].kind_of?(Hash)
54 if data['$ref'].respond_to?(:to_str)
55 reference = data['$ref'].to_str
57 raise TypeError, "Expected String, got #{data['$ref'].class}"
59 reference = '#' + reference if reference[0..0] != '#'
63 elsif data.kind_of?(Hash)
64 data.inject({}) do |accu, (key, value)|
65 if value.kind_of?(Hash)
66 accu[key] = reformat_references.call(value)
76 schema_data = reformat_references.call(schema_data)
79 api_name_string = ActiveSupport::Inflector.camelize(api.name)
80 api_version_string = ActiveSupport::Inflector.camelize(api.version).gsub('.', '_')
81 # This is for compatibility with Ruby 1.8.7.
82 # TODO(bobaman) Remove this when we eventually stop supporting 1.8.7.
84 args << false if Class.method(:const_defined?).arity != 1
85 if Google::APIClient::Schema.const_defined?(api_name_string, *args)
86 api_name = Google::APIClient::Schema.const_get(
87 api_name_string, *args
90 api_name = Google::APIClient::Schema.const_set(
91 api_name_string, Module.new
94 if api_name.const_defined?(api_version_string, *args)
95 api_version = api_name.const_get(api_version_string, *args)
97 api_version = api_name.const_set(api_version_string, Module.new)
99 if api_version.const_defined?(schema_name, *args)
100 schema_class = api_version.const_get(schema_name, *args)
104 # It's possible the schema has already been defined. If so, don't
105 # redefine it. This means that reloading a schema which has already
106 # been loaded into memory is not possible.
108 schema_class = AutoParse.generate(schema_data, :uri => schema_uri)
110 api_version.const_set(schema_name, schema_class)