Cleaning up the changes made by @vapir.

This commit is contained in:
Bob Aman 2012-02-27 12:13:42 +01:00
parent af61568cbe
commit 1e5f09a594
2 changed files with 69 additions and 20 deletions

View File

@ -47,8 +47,6 @@ module Google
# <li><code>:oauth_1</code></li> # <li><code>:oauth_1</code></li>
# <li><code>:oauth_2</code></li> # <li><code>:oauth_2</code></li>
# </ul> # </ul>
# @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 # @option options [String] :application_name
# The name of the application using the client. # The name of the application using the client.
# @option options [String] :application_version # @option options [String] :application_version
@ -57,14 +55,22 @@ module Google
# ("{app_name} google-api-ruby-client/{version} {os_name}/{os_version}") # ("{app_name} google-api-ruby-client/{version} {os_name}/{os_version}")
# The user agent used by the client. Most developers will want to # The user agent used by the client. Most developers will want to
# leave this value alone and use the `:application_name` option instead. # 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={}) def initialize(options={})
# Normalize key to String to allow indifferent access. # Normalize key to String to allow indifferent access.
options = options.inject({}) do |accu, (key, value)| options = options.inject({}) do |accu, (key, value)|
accu[key.to_s] = value accu[key.to_s] = value
accu accu
end end
# Almost all API usage will have this base URI # Almost all API usage will have a host of 'www.googleapis.com'.
self.baseURI = options["baseURI"] || "https://www.googleapis.com/discovery/v1" 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 # Most developers will want to leave this value alone and use the
# application_name option. # application_name option.
@ -161,22 +167,57 @@ module Google
# @return [String] The user's IP address. # @return [String] The user's IP address.
attr_accessor :user_ip 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. # The user agent used by the client.
# #
# @return [String] # @return [String]
# The user agent string used in the User-Agent header. # The user agent string used in the User-Agent header.
attr_accessor :user_agent 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 end
## ##
@ -184,7 +225,7 @@ module Google
# #
# @return [Addressable::URI] The URI of the directory document. # @return [Addressable::URI] The URI of the directory document.
def directory_uri def directory_uri
relative_uri('/apis') return resolve_uri(self.discovery_path + '/apis')
end end
## ##
@ -209,9 +250,13 @@ module Google
def discovery_uri(api, version=nil) def discovery_uri(api, version=nil)
api = api.to_s api = api.to_s
version = version || 'v1' version = version || 'v1'
return @discovery_uris["#{api}:#{version}"] ||= (begin return @discovery_uris["#{api}:#{version}"] ||= (
relative_uri("/apis/{api}/{version}/rest", 'api' => api, 'version' => version) resolve_uri(
end) self.discovery_path + '/apis/{api}/{version}/rest',
'api' => api,
'version' => version
)
)
end end
## ##

View File

@ -47,8 +47,12 @@ module Google
# and excess object creation, but this hopefully shouldn't be an # and excess object creation, but this hopefully shouldn't be an
# issue since it should only be called only once per schema per # issue since it should only be called only once per schema per
# process. # process.
if data.kind_of?(Hash) && data['$ref'].is_a?(String) if data.kind_of?(Hash) && data['$ref']
reference = 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] != '#' reference = '#' + reference if reference[0..0] != '#'
data.merge({ data.merge({
'$ref' => reference '$ref' => reference