google-api-ruby-client/lib/google/api_client/discovery/schema.rb

70 lines
2.3 KiB
Ruby
Raw Normal View History

# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'time'
require 'json'
2011-08-05 21:00:36 +00:00
require 'base64'
require 'autoparse'
require 'addressable/uri'
require 'addressable/template'
require 'google/inflection'
require 'google/api_client/errors'
module Google
class APIClient
module Schema
def self.parse(api, schema_data)
# This method is super-long, but hard to break up due to the
# unavoidable dependence on closures and execution context.
schema_name = schema_data['id']
if schema_name
api_name_string =
Google::INFLECTOR.camelize(api.name)
api_version_string =
Google::INFLECTOR.camelize(api.version).gsub('.', '_')
if Google::APIClient::Schema.const_defined?(api_name_string)
api_name = Google::APIClient::Schema.const_get(api_name_string)
else
api_name = Google::APIClient::Schema.const_set(
api_name_string, Module.new
)
end
if api_name.const_defined?(api_version_string)
api_version = api_name.const_get(api_version_string)
else
api_version = api_name.const_set(api_version_string, Module.new)
end
if api_version.const_defined?(schema_name)
schema_class = api_version.const_get(schema_name)
end
end
# It's possible the schema has already been defined. If so, don't
# redefine it. This means that reloading a schema which has already
# been loaded into memory is not possible.
unless schema_class
schema_class = AutoParse.generate(schema_data)
if schema_name
api_version.const_set(schema_name, schema_class)
end
end
return schema_class
end
end
end
end