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.
17 require 'addressable/uri'
18 require 'addressable/template'
20 require 'google/inflection'
21 require 'google/api_client/errors'
26 def initialize(api, api_name, api_version, discovery_document)
27 # This constructor is super-long, but hard to break up due to the
28 # unavoidable dependence on closures and execution context.
30 @discovery_document = discovery_document
32 Google::INFLECTOR.camelize(api_name)
34 Google::INFLECTOR.camelize(api_version).gsub('.', '_')
35 @schema_name = @discovery_document['id']
36 if Google::APIClient::Schema.const_defined?(api_name_string)
37 @api_name = Google::APIClient::Schema.const_get(api_name_string)
39 @api_name = Google::APIClient::Schema.const_set(
40 api_name_string, Class.new
43 if @api_name.const_defined?(api_version_string)
44 @api_version = @api_name.const_get(api_version_string)
46 @api_version = @api_name.const_set(api_version_string, Class.new)
48 if @api_version.const_defined?(@schema_name)
49 @schema_class = @api_version.const_get(@schema_name)
51 @schema_class = @api_version.const_set(
53 Class.new(APIObject) do |klass|
54 discovery_document['properties'].each do |(k, v)|
55 property_name = Google::INFLECTOR.underscore(k)
56 define_method(property_name + '_property') do
61 define_method(property_name) do
62 self[k] || v['default']
64 define_method(property_name + '=') do |value|
65 if value.respond_to?(:to_str)
66 self[k] = value.to_str
67 elsif value.kind_of?(Symbol)
71 "Expected String or Symbol, got #{value.class}."
76 # Implement the remainder of the types.
78 # Don't know what this is, default to anything goes.
79 define_method(property_name) do
80 self[k] || v['default']
82 define_method(property_name + '=') do |value|
101 # Returns a <code>String</code> representation of the resource's state.
103 # @return [String] The resource's state, as a <code>String</code>.
106 "#<%s:%#0x CLASS:%s>",
107 self.class.to_s, self.object_id, self.schema_class.name
122 return @data[key] = value