From c50f92c50cb77b879ed247d789bd944f18e3f95e Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 25 Jan 2012 03:23:03 -0500 Subject: [PATCH 01/11] change ApiClient's configurable host to a configurable baseURI, so that protocol, port, and base path may be overridden in addition to host --- lib/google/api_client.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 090dda26a..e75bb784d 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -47,8 +47,8 @@ module Google #
  • :oauth_1
  • #
  • :oauth_2
  • # - # @option options [String] :host ("www.googleapis.com") - # The API hostname used by the client. This rarely needs to be changed. + # @option options [String] :baseURI ("https://www.googleapis.com/discovery/v1") + # The base API URI 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 @@ -63,8 +63,9 @@ module Google accu[key.to_s] = value accu end - # Almost all API usage will have a host of 'www.googleapis.com'. - self.host = options["host"] || 'www.googleapis.com' + # Almost all API usage will have this base URI + self.baseURI = options["baseURI"] || "https://www.googleapis.com/discovery/v1" + # Most developers will want to leave this value alone and use the # application_name option. application_string = ( @@ -165,7 +166,7 @@ module Google # # @return [String] # The API hostname. Should almost always be 'www.googleapis.com'. - attr_accessor :host + attr_accessor :baseURI ## # The user agent used by the client. From d59901bf1edb4e4cb620ed4bcb0ea5134e25bf31 Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 28 Dec 2011 21:04:16 -0700 Subject: [PATCH 02/11] add method ApiClient#relative_uri which constructs a full uri from a given relative path, respecting specified protocol, host, and port; and allowing expanded parameters passed through Addressable::Template --- lib/google/api_client.rb | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index e75bb784d..fb5f53626 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -174,16 +174,17 @@ module Google # @return [String] # The user agent string used in the User-Agent header. attr_accessor :user_agent + + def relative_uri(path, expand={}) + Addressable::Template.new(baseURI+path).expand(expand) + 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}) + relative_uri('/apis') end ## @@ -209,15 +210,7 @@ module Google 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" - ) - template.expand({ - "host" => self.host, - "api" => api, - "version" => version - }) + relative_uri("/apis/{api}/{version}/rest", 'api' => api, 'version' => version) end) end From bd1ce540d48505e5eb216cf47f4807946ffcffc3 Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 25 Jan 2012 03:24:22 -0500 Subject: [PATCH 03/11] if ApiClient receives :authorization => false, respect that, and only default to oauth2 if :authorization is omitted --- lib/google/api_client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index fb5f53626..91aec235d 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -81,7 +81,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 = {} From b6ef9b00ad41bbc7e75f0f7245c0a7226152c6da Mon Sep 17 00:00:00 2001 From: Ethan Date: Sun, 8 Jan 2012 01:38:48 -0500 Subject: [PATCH 04/11] fix schema to not choke when '$ref' is specifying what the '$ref' property actually is, rather than invoking it, and is therefore not a string. this applies to the schema JsonSchema, specified in the document for the discovery service itself --- lib/google/api_client/discovery/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/google/api_client/discovery/schema.rb b/lib/google/api_client/discovery/schema.rb index f3499ebdc..2b4d59833 100644 --- a/lib/google/api_client/discovery/schema.rb +++ b/lib/google/api_client/discovery/schema.rb @@ -47,7 +47,7 @@ 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'] + if data.kind_of?(Hash) && data['$ref'].is_a?(String) reference = data['$ref'] reference = '#' + reference if reference[0..0] != '#' data.merge({ From 80c41c99fc91c9818dbc7c5840b584de4d48ae23 Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 25 Jan 2012 05:01:30 -0500 Subject: [PATCH 05/11] minor whitespace touchup gedit's syntax highlighting breaks on the previous --- lib/google/api_client.rb | 2 +- lib/google/api_client/discovery/api.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 91aec235d..94285acb8 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -590,7 +590,7 @@ module Google unless headers.kind_of?(Enumerable) # We need to use some Enumerable methods, relying on the presence of # the #each method. - class < Date: Mon, 30 Jan 2012 22:33:03 -0500 Subject: [PATCH 06/11] rescue any error invoking external commands attempting to determine OS_VERSION; just return RUBY_PLATFORM on failure. --- lib/google/api_client/environment.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/google/api_client/environment.rb b/lib/google/api_client/environment.rb index e5abe9334..9faa28f7d 100644 --- a/lib/google/api_client/environment.rb +++ b/lib/google/api_client/environment.rb @@ -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 From 1632e98a05f8b50ddb7eee75cba0645ea099497b Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Wed, 22 Feb 2012 15:09:35 +0300 Subject: [PATCH 07/11] Patch to solve Ruby 1.8.7 incompatibility. --- lib/google/api_client/discovery/schema.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/google/api_client/discovery/schema.rb b/lib/google/api_client/discovery/schema.rb index f3499ebdc..cec0db8cc 100644 --- a/lib/google/api_client/discovery/schema.rb +++ b/lib/google/api_client/discovery/schema.rb @@ -74,22 +74,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 From 0f25fbe793075926ff20d76d0d88515831c07dd9 Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Wed, 22 Feb 2012 15:40:33 +0300 Subject: [PATCH 08/11] Updated version and CHANGELOG. --- CHANGELOG.md | 4 ++++ lib/google/api_client/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bcb850d2..bef698d4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/google/api_client/version.rb b/lib/google/api_client/version.rb index 38e2ec5d4..133ed3c00 100644 --- a/lib/google/api_client/version.rb +++ b/lib/google/api_client/version.rb @@ -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 From 0dee0cbc521c9a765aa27dd39797dcdb4820cb8a Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Wed, 22 Feb 2012 15:41:18 +0300 Subject: [PATCH 09/11] Updated gemspec. --- google-api-client.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-api-client.gemspec b/google-api-client.gemspec index f5e44586e..33db9b052 100644 --- a/google-api-client.gemspec +++ b/google-api-client.gemspec @@ -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"] From 1e5f09a594a1be9e6dce0ce7c311cab27bd2c3fb Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Mon, 27 Feb 2012 12:13:42 +0100 Subject: [PATCH 10/11] Cleaning up the changes made by @vapir. --- lib/google/api_client.rb | 81 ++++++++++++++++++----- lib/google/api_client/discovery/schema.rb | 8 ++- 2 files changed, 69 insertions(+), 20 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 94285acb8..ab97de942 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -47,8 +47,6 @@ module Google #
  • :oauth_1
  • #
  • :oauth_2
  • # - # @option options [String] :baseURI ("https://www.googleapis.com/discovery/v1") - # The base API URI 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,14 +55,22 @@ 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)| accu[key.to_s] = value accu end - # Almost all API usage will have this base URI - self.baseURI = options["baseURI"] || "https://www.googleapis.com/discovery/v1" + # 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. @@ -161,22 +167,57 @@ 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 :baseURI - ## # The user agent used by the client. # # @return [String] # The user agent string used in the User-Agent header. attr_accessor :user_agent - - def relative_uri(path, expand={}) - Addressable::Template.new(baseURI+path).expand(expand) + + ## + # 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 ## @@ -184,7 +225,7 @@ module Google # # @return [Addressable::URI] The URI of the directory document. def directory_uri - relative_uri('/apis') + return resolve_uri(self.discovery_path + '/apis') end ## @@ -209,9 +250,13 @@ module Google def discovery_uri(api, version=nil) api = api.to_s version = version || 'v1' - return @discovery_uris["#{api}:#{version}"] ||= (begin - relative_uri("/apis/{api}/{version}/rest", 'api' => api, 'version' => version) - end) + return @discovery_uris["#{api}:#{version}"] ||= ( + resolve_uri( + self.discovery_path + '/apis/{api}/{version}/rest', + 'api' => api, + 'version' => version + ) + ) end ## diff --git a/lib/google/api_client/discovery/schema.rb b/lib/google/api_client/discovery/schema.rb index 0bcf8b7fb..f8a34b6b0 100644 --- a/lib/google/api_client/discovery/schema.rb +++ b/lib/google/api_client/discovery/schema.rb @@ -47,8 +47,12 @@ 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'].is_a?(String) - reference = data['$ref'] + if data.kind_of?(Hash) && data['$ref'] + 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 From 2b9ac92b7e4702857c827b58739d321a23a02fe9 Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Mon, 27 Feb 2012 12:23:33 +0100 Subject: [PATCH 11/11] More carefully resolving the discovery-of-discovery issue. --- lib/google/api_client/discovery/schema.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/google/api_client/discovery/schema.rb b/lib/google/api_client/discovery/schema.rb index f8a34b6b0..eebf7dddb 100644 --- a/lib/google/api_client/discovery/schema.rb +++ b/lib/google/api_client/discovery/schema.rb @@ -47,7 +47,8 @@ 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'] + 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