Basic structure of schema parsing complete.

* Note that additionalProperties fields are not currently supported and may
  only be accessed in raw form. Shouldn't be a big deal because only the Buzz
  API uses them extensively.
This commit is contained in:
Bob Aman 2011-08-10 16:48:22 -04:00
parent d9a108415b
commit 27ae32d2e7
3 changed files with 145 additions and 100 deletions

View File

@ -62,7 +62,10 @@ module Google
# #
# @return [String] The service id. # @return [String] The service id.
def id def id
return @discovery_document['id'] return (
@discovery_document['id'] ||
"#{self.name}:#{self.version}"
)
end end
## ##
@ -160,9 +163,7 @@ module Google
def schemas def schemas
return @schemas ||= ( return @schemas ||= (
(@discovery_document['schemas'] || []).inject({}) do |accu, (k, v)| (@discovery_document['schemas'] || []).inject({}) do |accu, (k, v)|
accu[k] = Google::APIClient::Schema.new( accu[k] = Google::APIClient::Schema.parse(self, v)
self, self.name, self.version, v
)
accu accu
end end
) )

View File

@ -24,40 +24,49 @@ require 'google/api_client/errors'
module Google module Google
class APIClient class APIClient
class Schema module Schema
def initialize(api, api_name, api_version, discovery_document) def self.parse(api, schema_data)
# This constructor is super-long, but hard to break up due to the # This method is super-long, but hard to break up due to the
# unavoidable dependence on closures and execution context. # unavoidable dependence on closures and execution context.
@api = api schema_name = schema_data['id']
@discovery_document = discovery_document
if schema_name
api_name_string = api_name_string =
Google::INFLECTOR.camelize(api_name) Google::INFLECTOR.camelize(api.name)
api_version_string = api_version_string =
Google::INFLECTOR.camelize(api_version).gsub('.', '_') Google::INFLECTOR.camelize(api.version).gsub('.', '_')
@schema_name = @discovery_document['id']
if Google::APIClient::Schema.const_defined?(api_name_string) if Google::APIClient::Schema.const_defined?(api_name_string)
@api_name = Google::APIClient::Schema.const_get(api_name_string) api_name = Google::APIClient::Schema.const_get(api_name_string)
else else
@api_name = Google::APIClient::Schema.const_set( api_name = Google::APIClient::Schema.const_set(
api_name_string, Class.new api_name_string, Module.new
) )
end end
if @api_name.const_defined?(api_version_string) if api_name.const_defined?(api_version_string)
@api_version = @api_name.const_get(api_version_string) api_version = api_name.const_get(api_version_string)
else else
@api_version = @api_name.const_set(api_version_string, Class.new) api_version = api_name.const_set(api_version_string, Module.new)
end end
if @api_version.const_defined?(@schema_name) if api_version.const_defined?(schema_name)
@schema_class = @api_version.const_get(@schema_name) schema_class = api_version.const_get(schema_name)
else 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 = self schema = self
@schema_class = @api_version.const_set( schema_class = Class.new(APIObject) do |klass|
@schema_name, properties = []
Class.new(APIObject) do |klass| define_method('schema') do
discovery_document['properties'].each do |(k, v)| schema_data
end
(schema_data['properties'] || []).each do |(k, v)|
property_name = Google::INFLECTOR.underscore(k) property_name = Google::INFLECTOR.underscore(k)
properties << property_name.to_sym
define_method(:schema) { schema } define_method(:schema) { schema }
define_method(property_name + '_property') do define_method(property_name + '_schema') do
v v
end end
define_method(property_name + '_description') do define_method(property_name + '_description') do
@ -65,62 +74,52 @@ module Google
end end
case v['type'] case v['type']
when 'string' when 'string'
define_string_property(property_name, k, v) define_string_property(api, property_name, k, v)
when 'boolean' when 'boolean'
define_boolean_property(property_name, k, v) define_boolean_property(api, property_name, k, v)
when 'number' when 'number'
define_number_property(property_name, k, v) define_number_property(api, property_name, k, v)
when 'array'
define_array_property(api, property_name, k, v)
when 'object' when 'object'
define_object_property(property_name, k, v) define_object_property(api, property_name, k, v)
else else
# Either type 'any' or we don't know what this is, # Either type 'any' or we don't know what this is,
# default to anything goes. # default to anything goes.
define_any_property(property_name, k, v) define_any_property(api, property_name, k, v)
end
end
end
)
end end
end end
def schema_name define_method('properties') do
return @schema_name properties
end end
def schema_class
return @schema_class
end end
if schema_name
## api_version.const_set(schema_name, schema_class)
# Returns a <code>String</code> representation of the resource's state. end
# end
# @return [String] The resource's state, as a <code>String</code>. return schema_class
def inspect
sprintf(
"#<%s:%#0x CLASS:%s>",
self.class.to_s, self.object_id, self.schema_class.name
)
end end
end end
class APIObject class APIObject
def self.define_string_property(property_name, key, schema) def self.define_string_property(api, property_name, key, schema_data)
define_method(property_name) do define_method(property_name) do
self[key] ||= schema['default'] self[key] ||= schema_data['default']
if schema['format'] == 'byte' && self[key] != nil if schema_data['format'] == 'byte' && self[key] != nil
Base64.decode64(self[key]) Base64.decode64(self[key])
elsif schema['format'] == 'date-time' && self[key] != nil elsif schema_data['format'] == 'date-time' && self[key] != nil
Time.parse(self[key]) Time.parse(self[key])
elsif schema['format'] =~ /^u?int(32|64)$/ && self[key] != nil elsif schema_data['format'] =~ /^u?int(32|64)$/ && self[key] != nil
self[key].to_i self[key].to_i
else else
self[key] self[key]
end end
end end
define_method(property_name + '=') do |value| define_method(property_name + '=') do |value|
if schema['format'] == 'byte' if schema_data['format'] == 'byte'
self[key] = Base64.encode64(value) self[key] = Base64.encode64(value)
elsif schema['format'] == 'date-time' elsif schema_data['format'] == 'date-time'
if value.respond_to?(:to_str) if value.respond_to?(:to_str)
value = Time.parse(value.to_str) value = Time.parse(value.to_str)
elsif !value.respond_to?(:xmlschema) elsif !value.respond_to?(:xmlschema)
@ -128,7 +127,7 @@ module Google
"Could not obtain RFC 3339 timestamp from #{value.class}." "Could not obtain RFC 3339 timestamp from #{value.class}."
end end
self[key] = value.xmlschema self[key] = value.xmlschema
elsif schema['format'] =~ /^u?int(32|64)$/ elsif schema_data['format'] =~ /^u?int(32|64)$/
self[key] = value.to_s self[key] = value.to_s
elsif value.respond_to?(:to_str) elsif value.respond_to?(:to_str)
self[key] = value.to_str self[key] = value.to_str
@ -141,9 +140,9 @@ module Google
end end
end end
def self.define_boolean_property(property_name, key, schema) def self.define_boolean_property(api, property_name, key, schema_data)
define_method(property_name) do define_method(property_name) do
self[key] ||= schema['default'] self[key] ||= schema_data['default']
case self[key].to_s.downcase case self[key].to_s.downcase
when 'true', 'yes', 'y', 'on', '1' when 'true', 'yes', 'y', 'on', '1'
true true
@ -170,9 +169,9 @@ module Google
end end
end end
def self.define_number_property(property_name, key, schema) def self.define_number_property(api, property_name, key, schema_data)
define_method(property_name) do define_method(property_name) do
self[key] ||= schema['default'] self[key] ||= schema_data['default']
if self[key] != nil && !self[key].respond_to?(:to_f) if self[key] != nil && !self[key].respond_to?(:to_f)
raise TypeError, raise TypeError,
"Expected Float, got #{self[key].class}." "Expected Float, got #{self[key].class}."
@ -186,7 +185,7 @@ module Google
if value == nil if value == nil
self[key] = value self[key] = value
else else
case schema['format'] case schema_data['format']
when 'double', 'float' when 'double', 'float'
if value.respond_to?(:to_f) if value.respond_to?(:to_f)
self[key] = value.to_f self[key] = value.to_f
@ -196,24 +195,53 @@ module Google
end end
else else
raise TypeError, raise TypeError,
"Unexpected type format for number: #{schema['format']}." "Unexpected type format for number: #{schema_data['format']}."
end end
end end
end end
end end
def self.define_object_property(property_name, key, schema) def self.define_array_property(api, property_name, key, schema_data)
# TODO(bobaman):
# Do we treat this differently from any?
self.define_any_property(property_name, key, schema)
end
def self.define_any_property(property_name, key, schema)
define_method(property_name) do define_method(property_name) do
self[k] || v['default'] # 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
self[key] ||= v['default']
schema.new(self[key])
end
end
def self.define_any_property(api, property_name, key, schema_data)
define_method(property_name) do
self[key] ||= v['default']
end end
define_method(property_name + '=') do |value| define_method(property_name + '=') do |value|
self[k] = value self[key] = value
end end
end end

View File

@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
require 'google/api_client/parsers/json_parser'
module Google module Google
class APIClient class APIClient
## ##
@ -54,9 +52,27 @@ module Google
_, content_type = self.headers.detect do |h, v| _, content_type = self.headers.detect do |h, v|
h.downcase == 'Content-Type'.downcase h.downcase == 'Content-Type'.downcase
end end
parser_type = media_type = content_type[/^([^;]*);?.*$/, 1].strip.downcase
Google::APIClient::Parser.match_content_type(content_type) data = self.body
parser_type.parse(self.body) case media_type
when 'application/json'
data = JSON.parse(data)
# Strip data wrapper, if present
data = data['data'] if data.has_key?('data')
else
raise ArgumentError,
"Content-Type not supported for parsing: #{media_type}"
end
if @reference.api_method && @reference.api_method.response_schema
# Automatically parse using the schema designated for the
# response of this API method.
data = @reference.api_method.response_schema.new(data)
data
else
# Otherwise, return the raw unparsed value.
# This value must be indexable like a Hash.
data
end
end) end)
end end