Merge remote branch 'remotes/upstream/master'

This commit is contained in:
Martin Sarsale committed 2011-11-30 11:39:28 -03:00
commit f736ac4d97
21 files changed
+339 -4599

No files matched your search

+1
View File
@@ -16,3 +16,4 @@
require 'google/api_client/discovery/api'
require 'google/api_client/discovery/resource'
require 'google/api_client/discovery/method'
require 'google/api_client/discovery/schema'
+40 -5
View File
@@ -62,7 +62,10 @@ module Google
#
# @return [String] The service id.
def id
return @discovery_document['id']
return (
@discovery_document['id'] ||
"#{self.name}:#{self.version}"
)
end
##
@@ -168,15 +171,47 @@ module Google
end
end
##
# A list of schemas available for this version of the API.
#
# @return [Hash] A list of {Google::APIClient::Schema} objects.
def schemas
return @schemas ||= (
(@discovery_document['schemas'] || []).inject({}) do |accu, (k, v)|
accu[k] = Google::APIClient::Schema.parse(self, v)
accu
end
)
end
##
# Returns a schema for a kind value.
#
# @return [Google::APIClient::Schema] The associated Schema object.
def schema_for_kind(kind)
api_name, schema_name = kind.split('#', 2)
if api_name != self.name
raise ArgumentError,
"The kind does not match this API. " +
"Expected '#{self.name}', got '#{api_name}'."
end
for k, v in self.schemas
return v if k.downcase == schema_name.downcase
end
return nil
end
##
# A list of resources available at the root level of this version of the
# service.
# API.
#
# @return [Array] A list of {Google::APIClient::Resource} objects.
def resources
return @resources ||= (
(@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
accu << Google::APIClient::Resource.new(self.method_base, k, v)
accu << Google::APIClient::Resource.new(
self, self.method_base, k, v
)
accu
end
)
@@ -184,13 +219,13 @@ module Google
##
# A list of methods available at the root level of this version of the
# service.
# API.
#
# @return [Array] A list of {Google::APIClient::Method} objects.
def methods
return @methods ||= (
(@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
accu << Google::APIClient::Method.new(self.method_base, k, v)
accu << Google::APIClient::Method.new(self, self.method_base, k, v)
accu
end
)
+28 -1
View File
@@ -35,7 +35,8 @@ module Google
# The section of the discovery document that applies to this method.
#
# @return [Google::APIClient::Method] The constructed method object.
def initialize(method_base, method_name, discovery_document)
def initialize(api, method_base, method_name, discovery_document)
@api = api
@method_base = method_base
@name = method_name
@discovery_document = discovery_document
@@ -102,6 +103,32 @@ module Google
)
end
##
# Returns the Schema object for the method's request, if any.
#
# @return [Google::APIClient::Schema] The request schema.
def request_schema
if @discovery_document['request']
schema_name = @discovery_document['request']['$ref']
return @api.schemas[schema_name]
else
return nil
end
end
##
# Returns the Schema object for the method's response, if any.
#
# @return [Google::APIClient::Schema] The response schema.
def response_schema
if @discovery_document['response']
schema_name = @discovery_document['response']['$ref']
return @api.schemas[schema_name]
else
return nil
end
end
##
# Normalizes parameters, converting to the appropriate types.
#
+6 -3
View File
@@ -35,7 +35,8 @@ module Google
# The section of the discovery document that applies to this resource.
#
# @return [Google::APIClient::Resource] The constructed resource object.
def initialize(method_base, resource_name, discovery_document)
def initialize(api, method_base, resource_name, discovery_document)
@api = api
@method_base = method_base
@name = resource_name
@discovery_document = discovery_document
@@ -95,7 +96,9 @@ module Google
def resources
return @resources ||= (
(@discovery_document['resources'] || []).inject([]) do |accu, (k, v)|
accu << Google::APIClient::Resource.new(self.method_base, k, v)
accu << Google::APIClient::Resource.new(
@api, self.method_base, k, v
)
accu
end
)
@@ -108,7 +111,7 @@ module Google
def methods
return @methods ||= (
(@discovery_document['methods'] || []).inject([]) do |accu, (k, v)|
accu << Google::APIClient::Method.new(self.method_base, k, v)
accu << Google::APIClient::Method.new(@api, self.method_base, k, v)
accu
end
)
+106
View File
@@ -0,0 +1,106 @@
# 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'
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']
# Due to an oversight, schema IDs may not be URI references.
# TODO(bobaman): Remove this code once this has been resolved.
schema_uri = (
api.document_base +
(schema_name[0..0] != '#' ? '#' + schema_name : schema_name)
)
# puts schema_uri
# Due to an oversight, schema IDs may not be URI references.
# TODO(bobaman): Remove this whole lambda once this has been resolved.
reformat_references = lambda do |data|
# This code is not particularly efficient due to recursive traversal
# and excess object creation, but this hopefully shouldn't be an
# issue since it should only be called only once per schema per
# process.
if data.kind_of?(Hash) && data['$ref']
reference = data['$ref']
reference = '#' + reference if reference[0..0] != '#'
data.merge({
'$ref' => reference
})
elsif data.kind_of?(Hash)
data.inject({}) do |accu, (key, value)|
if value.kind_of?(Hash)
accu[key] = reformat_references.call(value)
else
accu[key] = value
end
accu
end
else
data
end
end
schema_data = reformat_references.call(schema_data)
# puts schema_data.inspect
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, :uri => schema_uri)
if schema_name
api_version.const_set(schema_name, schema_class)
end
end
return schema_class
end
end
end
end
+5 -2
View File
@@ -1,8 +1,11 @@
module Google
class APIClient
module ENV
OS_VERSION = if RUBY_PLATFORM =~ /win32/
`ver`.sub(/\s*\[Version\s*/, '/').sub(']', '')
OS_VERSION = if RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/
# TODO(bobaman)
# Confirm that all of these Windows environments actually have access
# to the `ver` command.
`ver`.sub(/\s*\[Version\s*/, '/').sub(']', '').strip
elsif RUBY_PLATFORM =~ /darwin/i
"Mac OS X/#{`sw_vers -productVersion`}"
else
+21 -5
View File
@@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
require 'google/api_client/parsers/json_parser'
module Google
class APIClient
##
@@ -54,9 +52,27 @@ module Google
_, content_type = self.headers.detect do |h, v|
h.downcase == 'Content-Type'.downcase
end
parser_type =
Google::APIClient::Parser.match_content_type(content_type)
parser_type.parse(self.body)
media_type = content_type[/^([^;]*);?.*$/, 1].strip.downcase
data = 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