2011-08-05 03:36:36 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
2011-08-05 20:55:40 +00:00
|
|
|
require 'time'
|
2011-08-05 03:36:36 +00:00
|
|
|
require 'json'
|
2011-08-05 21:00:36 +00:00
|
|
|
require 'base64'
|
2011-08-05 03:36:36 +00:00
|
|
|
require 'addressable/uri'
|
|
|
|
require 'addressable/template'
|
|
|
|
|
|
|
|
require 'google/inflection'
|
|
|
|
require 'google/api_client/errors'
|
|
|
|
|
|
|
|
module Google
|
|
|
|
class APIClient
|
2011-08-10 20:48:22 +00:00
|
|
|
module Schema
|
|
|
|
def self.parse(api, schema_data)
|
|
|
|
# This method is super-long, but hard to break up due to the
|
2011-08-05 03:36:36 +00:00
|
|
|
# unavoidable dependence on closures and execution context.
|
2011-08-10 20:48:22 +00:00
|
|
|
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
|
2011-08-05 03:36:36 +00:00
|
|
|
end
|
2011-08-10 20:48:22 +00:00
|
|
|
|
|
|
|
# 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
|
2011-08-05 20:55:40 +00:00
|
|
|
schema = self
|
2011-08-10 20:48:22 +00:00
|
|
|
schema_class = Class.new(APIObject) do |klass|
|
|
|
|
properties = []
|
|
|
|
define_method('schema') do
|
|
|
|
schema_data
|
|
|
|
end
|
|
|
|
(schema_data['properties'] || []).each do |(k, v)|
|
|
|
|
property_name = Google::INFLECTOR.underscore(k)
|
|
|
|
properties << property_name.to_sym
|
|
|
|
define_method(:schema) { schema }
|
|
|
|
define_method(property_name + '_schema') do
|
|
|
|
v
|
|
|
|
end
|
|
|
|
define_method(property_name + '_description') do
|
|
|
|
v['description']
|
|
|
|
end
|
|
|
|
case v['type']
|
|
|
|
when 'string'
|
|
|
|
define_string_property(api, property_name, k, v)
|
|
|
|
when 'boolean'
|
|
|
|
define_boolean_property(api, property_name, k, v)
|
|
|
|
when 'number'
|
|
|
|
define_number_property(api, property_name, k, v)
|
|
|
|
when 'array'
|
|
|
|
define_array_property(api, property_name, k, v)
|
|
|
|
when 'object'
|
|
|
|
define_object_property(api, property_name, k, v)
|
|
|
|
else
|
|
|
|
# Either type 'any' or we don't know what this is,
|
|
|
|
# default to anything goes.
|
|
|
|
define_any_property(api, property_name, k, v)
|
2011-08-05 03:36:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-10 20:48:22 +00:00
|
|
|
define_method('properties') do
|
|
|
|
properties
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if schema_name
|
|
|
|
api_version.const_set(schema_name, schema_class)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return schema_class
|
2011-08-05 03:36:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class APIObject
|
2011-08-10 20:48:22 +00:00
|
|
|
def self.define_string_property(api, property_name, key, schema_data)
|
2011-08-05 20:55:40 +00:00
|
|
|
define_method(property_name) do
|
2011-08-10 20:48:22 +00:00
|
|
|
self[key] ||= schema_data['default']
|
|
|
|
if schema_data['format'] == 'byte' && self[key] != nil
|
2011-08-05 20:55:40 +00:00
|
|
|
Base64.decode64(self[key])
|
2011-08-10 20:48:22 +00:00
|
|
|
elsif schema_data['format'] == 'date-time' && self[key] != nil
|
2011-08-05 20:55:40 +00:00
|
|
|
Time.parse(self[key])
|
2011-08-10 20:48:22 +00:00
|
|
|
elsif schema_data['format'] =~ /^u?int(32|64)$/ && self[key] != nil
|
2011-08-05 20:55:40 +00:00
|
|
|
self[key].to_i
|
|
|
|
else
|
|
|
|
self[key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
define_method(property_name + '=') do |value|
|
2011-08-10 20:48:22 +00:00
|
|
|
if schema_data['format'] == 'byte'
|
2011-08-05 20:55:40 +00:00
|
|
|
self[key] = Base64.encode64(value)
|
2011-08-10 20:48:22 +00:00
|
|
|
elsif schema_data['format'] == 'date-time'
|
2011-08-05 20:55:40 +00:00
|
|
|
if value.respond_to?(:to_str)
|
|
|
|
value = Time.parse(value.to_str)
|
|
|
|
elsif !value.respond_to?(:xmlschema)
|
|
|
|
raise TypeError,
|
|
|
|
"Could not obtain RFC 3339 timestamp from #{value.class}."
|
|
|
|
end
|
|
|
|
self[key] = value.xmlschema
|
2011-08-10 20:48:22 +00:00
|
|
|
elsif schema_data['format'] =~ /^u?int(32|64)$/
|
2011-08-05 20:55:40 +00:00
|
|
|
self[key] = value.to_s
|
|
|
|
elsif value.respond_to?(:to_str)
|
|
|
|
self[key] = value.to_str
|
|
|
|
elsif value.kind_of?(Symbol)
|
|
|
|
self[key] = value.to_s
|
|
|
|
else
|
|
|
|
raise TypeError,
|
|
|
|
"Expected String or Symbol, got #{value.class}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-10 20:48:22 +00:00
|
|
|
def self.define_boolean_property(api, property_name, key, schema_data)
|
2011-08-05 20:55:40 +00:00
|
|
|
define_method(property_name) do
|
2011-08-10 20:48:22 +00:00
|
|
|
self[key] ||= schema_data['default']
|
2011-08-05 20:55:40 +00:00
|
|
|
case self[key].to_s.downcase
|
|
|
|
when 'true', 'yes', 'y', 'on', '1'
|
|
|
|
true
|
|
|
|
when 'false', 'no', 'n', 'off', '0'
|
|
|
|
false
|
|
|
|
when 'nil', 'null'
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
raise TypeError,
|
|
|
|
"Expected boolean, got #{self[key].class}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
define_method(property_name + '=') do |value|
|
|
|
|
case value.to_s.downcase
|
|
|
|
when 'true', 'yes', 'y', 'on', '1'
|
|
|
|
self[key] = true
|
|
|
|
when 'false', 'no', 'n', 'off', '0'
|
|
|
|
self[key] = false
|
|
|
|
when 'nil', 'null'
|
|
|
|
self[key] = nil
|
|
|
|
else
|
|
|
|
raise TypeError, "Expected boolean, got #{value.class}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-10 20:48:22 +00:00
|
|
|
def self.define_number_property(api, property_name, key, schema_data)
|
2011-08-05 20:55:40 +00:00
|
|
|
define_method(property_name) do
|
2011-08-10 20:48:22 +00:00
|
|
|
self[key] ||= schema_data['default']
|
2011-08-05 20:55:40 +00:00
|
|
|
if self[key] != nil && !self[key].respond_to?(:to_f)
|
|
|
|
raise TypeError,
|
|
|
|
"Expected Float, got #{self[key].class}."
|
|
|
|
elsif self[key] != nil && self[key].respond_to?(:to_f)
|
|
|
|
self[key].to_f
|
|
|
|
else
|
|
|
|
self[key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
define_method(property_name + '=') do |value|
|
|
|
|
if value == nil
|
|
|
|
self[key] = value
|
|
|
|
else
|
2011-08-10 20:48:22 +00:00
|
|
|
case schema_data['format']
|
2011-08-05 20:55:40 +00:00
|
|
|
when 'double', 'float'
|
|
|
|
if value.respond_to?(:to_f)
|
|
|
|
self[key] = value.to_f
|
|
|
|
else
|
|
|
|
raise TypeError,
|
|
|
|
"Expected String or Symbol, got #{value.class}."
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise TypeError,
|
2011-08-10 20:48:22 +00:00
|
|
|
"Unexpected type format for number: #{schema_data['format']}."
|
2011-08-05 20:55:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-10 20:48:22 +00:00
|
|
|
def self.define_array_property(api, property_name, key, schema_data)
|
|
|
|
define_method(property_name) do
|
|
|
|
# The default value of an empty Array obviates a mutator method.
|
|
|
|
self[key] ||= []
|
|
|
|
array = if self[key] != nil && !self[key].respond_to?(:to_ary)
|
|
|
|
raise TypeError,
|
|
|
|
"Expected Array, got #{self[key].class}."
|
|
|
|
else
|
|
|
|
self[key].to_ary
|
|
|
|
end
|
|
|
|
if schema_data['items'] && schema_data['items']['$ref']
|
|
|
|
schema_name = schema_data['items']['$ref']
|
|
|
|
if api.schemas[schema_name]
|
|
|
|
schema_class = api.schemas[schema_name]
|
|
|
|
array.map! do |item|
|
|
|
|
schema_class.new(item)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise ArgumentError,
|
|
|
|
"Could not find schema '#{schema_name}' in API '#{api.id}'."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
array
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.define_object_property(api, property_name, key, schema_data)
|
|
|
|
# TODO finish this up...
|
|
|
|
schema = Schema.parse(api, schema_data)
|
|
|
|
define_method(property_name) do
|
2011-08-10 20:59:55 +00:00
|
|
|
self[key] ||= schema_data['default']
|
2011-08-10 20:48:22 +00:00
|
|
|
schema.new(self[key])
|
|
|
|
end
|
2011-08-05 20:55:40 +00:00
|
|
|
end
|
|
|
|
|
2011-08-10 20:48:22 +00:00
|
|
|
def self.define_any_property(api, property_name, key, schema_data)
|
2011-08-05 20:55:40 +00:00
|
|
|
define_method(property_name) do
|
2011-08-10 20:59:55 +00:00
|
|
|
self[key] ||= schema_data['default']
|
2011-08-05 20:55:40 +00:00
|
|
|
end
|
|
|
|
define_method(property_name + '=') do |value|
|
2011-08-10 20:48:22 +00:00
|
|
|
self[key] = value
|
2011-08-05 20:55:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-05 03:36:36 +00:00
|
|
|
def initialize(data)
|
|
|
|
@data = data
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
|
|
|
return @data[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, value)
|
|
|
|
return @data[key] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|