From 1e5f09a594a1be9e6dce0ce7c311cab27bd2c3fb Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Mon, 27 Feb 2012 12:13:42 +0100 Subject: [PATCH] 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