Permalink
Browse files

Merge branch 'master' of https://code.google.com/p/google-api-ruby-cl…

  • Loading branch information...
2 parents b8301b0 + 2b9ac92 commit 1121bb7fe686b067b51eb5a3b4124493266212e7 @sqrrrl sqrrrl committed Mar 2, 2012
View
@@ -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
@@ -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
@@ -47,8 +47,6 @@ class APIClient
# <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 @@ class APIClient
# ("{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 @@ def initialize(options={})
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 @@ def initialize(options={})
).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 = {}
@@ -161,28 +168,64 @@ def authorization=(new_authorization)
attr_accessor :user_ip
##
+ # The user agent used by the client.
+ #
+ # @return [String]
+ # 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'.
+ # The API hostname. Should almost always be 'www.googleapis.com'.
attr_accessor :host
##
- # The user agent used by the client.
+ # The port number used by the client.
#
# @return [String]
- # The user agent string used in the User-Agent header.
- attr_accessor :user_agent
+ # 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 @@ def register_discovery_uri(api, version, uri)
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 @@ def transmit(options={})
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
@@ -43,7 +43,7 @@ class API
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)
@@ -47,8 +47,13 @@ def self.parse(api, schema_data)
# 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 @@ def self.parse(api, schema_data)
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
@@ -16,15 +16,19 @@
module Google
class APIClient
module ENV
- 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
- `uname -sr`.sub(' ', '/')
+ 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.
+ `ver`.sub(/\s*\[Version\s*/, '/').sub(']', '').strip
+ elsif RUBY_PLATFORM =~ /darwin/i
+ "Mac OS X/#{`sw_vers -productVersion`}"
+ else
+ `uname -sr`.sub(' ', '/')
+ end
+ rescue Exception
+ RUBY_PLATFORM
end
end
end
@@ -22,7 +22,7 @@ class APIClient
module VERSION
MAJOR = 0
MINOR = 4
- TINY = 1
+ TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
end

0 comments on commit 1121bb7

Please sign in to comment.