This commit is contained in:
Steven Bazyl 2012-03-01 17:24:14 -08:00
commit 1121bb7fe6
7 changed files with 102 additions and 46 deletions

View File

@ -1,3 +1,7 @@
# 0.4.2
* Fixed incompatibility with Ruby 1.8.7
# 0.4.1
* Fixed ancestor checking issue when assigning Autoparse identifiers

View File

@ -2,11 +2,11 @@
Gem::Specification.new do |s|
s.name = "google-api-client"
s.version = "0.4.1"
s.version = "0.4.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Bob Aman"]
s.date = "2012-02-10"
s.date = "2012-02-22"
s.description = "The Google API Ruby Client makes it trivial to discover and access supported\nAPIs.\n"
s.email = "bobaman@google.com"
s.executables = ["google-api"]

View File

@ -47,8 +47,6 @@ module Google
# <li><code>:oauth_1</code></li>
# <li><code>:oauth_2</code></li>
# </ul>
# @option options [String] :host ("www.googleapis.com")
# The API hostname used by the client. This rarely needs to be changed.
# @option options [String] :application_name
# The name of the application using the client.
# @option options [String] :application_version
@ -57,6 +55,12 @@ module Google
# ("{app_name} google-api-ruby-client/{version} {os_name}/{os_version}")
# The user agent used by the client. Most developers will want to
# leave this value alone and use the `:application_name` option instead.
# @option options [String] :host ("www.googleapis.com")
# The API hostname used by the client. This rarely needs to be changed.
# @option options [String] :port (443)
# The port number used by the client. This rarely needs to be changed.
# @option options [String] :discovery_path ("/discovery/v1")
# The discovery base path. This rarely needs to be changed.
def initialize(options={})
# Normalize key to String to allow indifferent access.
options = options.inject({}) do |accu, (key, value)|
@ -65,6 +69,9 @@ module Google
end
# Almost all API usage will have a host of 'www.googleapis.com'.
self.host = options["host"] || 'www.googleapis.com'
self.port = options["port"] || 443
self.discovery_path = options["discovery_path"] || '/discovery/v1'
# Most developers will want to leave this value alone and use the
# application_name option.
application_string = (
@ -80,7 +87,7 @@ module Google
).strip
# The writer method understands a few Symbols and will generate useful
# default authentication mechanisms.
self.authorization = options["authorization"] || :oauth_2
self.authorization = options.key?("authorization") ? options["authorization"] : :oauth_2
self.key = options["key"]
self.user_ip = options["user_ip"]
@discovery_uris = {}
@ -160,13 +167,6 @@ module Google
# @return [String] The user's IP address.
attr_accessor :user_ip
##
# The API hostname used by the client.
#
# @return [String]
# The API hostname. Should almost always be 'www.googleapis.com'.
attr_accessor :host
##
# The user agent used by the client.
#
@ -174,15 +174,58 @@ module Google
# The user agent string used in the User-Agent header.
attr_accessor :user_agent
##
# The API hostname used by the client.
#
# @return [String]
# The API hostname. Should almost always be 'www.googleapis.com'.
attr_accessor :host
##
# The port number used by the client.
#
# @return [String]
# The port number. Should almost always be 443.
attr_accessor :port
##
# The base path used by the client for discovery.
#
# @return [String]
# The base path. Should almost always be '/discovery/v1'.
attr_accessor :discovery_path
##
# Resolves a URI template against the client's configured base.
#
# @param [String, Addressable::URI, Addressable::Template] template
# The template to resolve.
# @param [Hash] mapping The mapping that corresponds to the template.
# @return [Addressable::URI] The expanded URI.
def resolve_uri(template, mapping={})
@base_uri ||= Addressable::URI.new(
:scheme => 'https',
:host => self.host,
:port => self.port
).normalize
template = if template.kind_of?(Addressable::Template)
template.pattern
elsif template.respond_to?(:to_str)
template.to_str
else
raise TypeError,
"Expected String, Addressable::URI, or Addressable::Template, " +
"got #{template.class}."
end
return Addressable::Template.new(@base_uri + template).expand(mapping)
end
##
# Returns the URI for the directory document.
#
# @return [Addressable::URI] The URI of the directory document.
def directory_uri
template = Addressable::Template.new(
"https://{host}/discovery/v1/apis"
)
return template.expand({"host" => self.host})
return resolve_uri(self.discovery_path + '/apis')
end
##
@ -207,17 +250,13 @@ module Google
def discovery_uri(api, version=nil)
api = api.to_s
version = version || 'v1'
return @discovery_uris["#{api}:#{version}"] ||= (begin
template = Addressable::Template.new(
"https://{host}/discovery/v1/apis/" +
"{api}/{version}/rest"
return @discovery_uris["#{api}:#{version}"] ||= (
resolve_uri(
self.discovery_path + '/apis/{api}/{version}/rest',
'api' => api,
'version' => version
)
)
template.expand({
"host" => self.host,
"api" => api,
"version" => version
})
end)
end
##
@ -596,7 +635,7 @@ module Google
unless headers.kind_of?(Enumerable)
# We need to use some Enumerable methods, relying on the presence of
# the #each method.
class <<headers
class << headers
include Enumerable
end
end

View File

@ -43,7 +43,7 @@ module Google
def initialize(document_base, discovery_document)
@document_base = Addressable::URI.parse(document_base)
@discovery_document = discovery_document
metaclass = (class <<self; self; end)
metaclass = (class << self; self; end)
self.discovered_resources.each do |resource|
method_name = Google::INFLECTOR.underscore(resource.name).to_sym
if !self.respond_to?(method_name)

View File

@ -47,8 +47,13 @@ module Google
# 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']
if data.kind_of?(Hash) &&
data['$ref'] && !data['$ref'].kind_of?(Hash)
if data['$ref'].respond_to?(:to_str)
reference = data['$ref'].to_str
else
raise TypeError, "Expected String, got #{data['$ref'].class}"
end
reference = '#' + reference if reference[0..0] != '#'
data.merge({
'$ref' => reference
@ -74,22 +79,26 @@ module Google
Google::INFLECTOR.camelize(api.name)
api_version_string =
Google::INFLECTOR.camelize(api.version).gsub('.', '_')
if Google::APIClient::Schema.const_defined?(api_name_string, false)
# This is for compatibility with Ruby 1.8.7.
# TODO(bobaman) Remove this when we eventually stop supporting 1.8.7.
args = []
args << false if Class.method(:const_defined?).arity != 1
if Google::APIClient::Schema.const_defined?(api_name_string, *args)
api_name = Google::APIClient::Schema.const_get(
api_name_string, false
api_name_string, *args
)
else
api_name = Google::APIClient::Schema.const_set(
api_name_string, Module.new
)
end
if api_name.const_defined?(api_version_string, false)
api_version = api_name.const_get(api_version_string, false)
if api_name.const_defined?(api_version_string, *args)
api_version = api_name.const_get(api_version_string, *args)
else
api_version = api_name.const_set(api_version_string, Module.new)
end
if api_version.const_defined?(schema_name, false)
schema_class = api_version.const_get(schema_name, false)
if api_version.const_defined?(schema_name, *args)
schema_class = api_version.const_get(schema_name, *args)
end
end

View File

@ -16,7 +16,8 @@
module Google
class APIClient
module ENV
OS_VERSION = if RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/
OS_VERSION = begin
if RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/
# TODO(bobaman)
# Confirm that all of these Windows environments actually have access
# to the `ver` command.
@ -26,6 +27,9 @@ module Google
else
`uname -sr`.sub(' ', '/')
end
rescue Exception
RUBY_PLATFORM
end
end
end
end

View File

@ -22,7 +22,7 @@ if !defined?(::Google::APIClient::VERSION)
module VERSION
MAJOR = 0
MINOR = 4
TINY = 1
TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
end