Merge pull request #4 from vapir/baseURI_and_fixups
Base uri and fixups
This commit is contained in:
commit
49ca24c16f
|
@ -47,8 +47,8 @@ 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] :host ("www.googleapis.com")
|
# @option options [String] :baseURI ("https://www.googleapis.com/discovery/v1")
|
||||||
# The API hostname used by the client. This rarely needs to be changed.
|
# 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
|
||||||
|
@ -63,8 +63,9 @@ module Google
|
||||||
accu[key.to_s] = value
|
accu[key.to_s] = value
|
||||||
accu
|
accu
|
||||||
end
|
end
|
||||||
# Almost all API usage will have a host of 'www.googleapis.com'.
|
# Almost all API usage will have this base URI
|
||||||
self.host = options["host"] || 'www.googleapis.com'
|
self.baseURI = options["baseURI"] || "https://www.googleapis.com/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.
|
||||||
application_string = (
|
application_string = (
|
||||||
|
@ -80,7 +81,7 @@ module Google
|
||||||
).strip
|
).strip
|
||||||
# The writer method understands a few Symbols and will generate useful
|
# The writer method understands a few Symbols and will generate useful
|
||||||
# default authentication mechanisms.
|
# default authentication mechanisms.
|
||||||
self.authorization = options["authorization"] || :oauth_2
|
self.authorization = options.key?("authorization") ? options["authorization"] : :oauth_2
|
||||||
self.key = options["key"]
|
self.key = options["key"]
|
||||||
self.user_ip = options["user_ip"]
|
self.user_ip = options["user_ip"]
|
||||||
@discovery_uris = {}
|
@discovery_uris = {}
|
||||||
|
@ -165,7 +166,7 @@ module Google
|
||||||
#
|
#
|
||||||
# @return [String]
|
# @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
|
attr_accessor :baseURI
|
||||||
|
|
||||||
##
|
##
|
||||||
# The user agent used by the client.
|
# The user agent used by the client.
|
||||||
|
@ -173,16 +174,17 @@ module Google
|
||||||
# @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)
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns the URI for the directory document.
|
# Returns the URI for the directory document.
|
||||||
#
|
#
|
||||||
# @return [Addressable::URI] The URI of the directory document.
|
# @return [Addressable::URI] The URI of the directory document.
|
||||||
def directory_uri
|
def directory_uri
|
||||||
template = Addressable::Template.new(
|
relative_uri('/apis')
|
||||||
"https://{host}/discovery/v1/apis"
|
|
||||||
)
|
|
||||||
return template.expand({"host" => self.host})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -208,15 +210,7 @@ module Google
|
||||||
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}"] ||= (begin
|
||||||
template = Addressable::Template.new(
|
relative_uri("/apis/{api}/{version}/rest", 'api' => api, 'version' => version)
|
||||||
"https://{host}/discovery/v1/apis/" +
|
|
||||||
"{api}/{version}/rest"
|
|
||||||
)
|
|
||||||
template.expand({
|
|
||||||
"host" => self.host,
|
|
||||||
"api" => api,
|
|
||||||
"version" => version
|
|
||||||
})
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -596,7 +590,7 @@ module Google
|
||||||
unless headers.kind_of?(Enumerable)
|
unless headers.kind_of?(Enumerable)
|
||||||
# We need to use some Enumerable methods, relying on the presence of
|
# We need to use some Enumerable methods, relying on the presence of
|
||||||
# the #each method.
|
# the #each method.
|
||||||
class <<headers
|
class << headers
|
||||||
include Enumerable
|
include Enumerable
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -43,7 +43,7 @@ module Google
|
||||||
def initialize(document_base, discovery_document)
|
def initialize(document_base, discovery_document)
|
||||||
@document_base = Addressable::URI.parse(document_base)
|
@document_base = Addressable::URI.parse(document_base)
|
||||||
@discovery_document = discovery_document
|
@discovery_document = discovery_document
|
||||||
metaclass = (class <<self; self; end)
|
metaclass = (class << self; self; end)
|
||||||
self.discovered_resources.each do |resource|
|
self.discovered_resources.each do |resource|
|
||||||
method_name = Google::INFLECTOR.underscore(resource.name).to_sym
|
method_name = Google::INFLECTOR.underscore(resource.name).to_sym
|
||||||
if !self.respond_to?(method_name)
|
if !self.respond_to?(method_name)
|
||||||
|
|
|
@ -172,12 +172,14 @@ module Google
|
||||||
query_parameters = parameters.reject do |k, v|
|
query_parameters = parameters.reject do |k, v|
|
||||||
template_variables.include?(k)
|
template_variables.include?(k)
|
||||||
end
|
end
|
||||||
if query_parameters.size > 0
|
# encode all non-template parameters
|
||||||
uri.query_values = (uri.query_values || []) + query_parameters
|
params = ""
|
||||||
|
unless query_parameters.empty?
|
||||||
|
params = "?" + Addressable::URI.form_encode(query_parameters)
|
||||||
end
|
end
|
||||||
# Normalization is necessary because of undesirable percent-escaping
|
# Normalization is necessary because of undesirable percent-escaping
|
||||||
# during URI template expansion
|
# during URI template expansion
|
||||||
return uri.normalize
|
return uri.normalize + params
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -47,7 +47,7 @@ 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']
|
if data.kind_of?(Hash) && data['$ref'].is_a?(String)
|
||||||
reference = data['$ref']
|
reference = data['$ref']
|
||||||
reference = '#' + reference if reference[0..0] != '#'
|
reference = '#' + reference if reference[0..0] != '#'
|
||||||
data.merge({
|
data.merge({
|
||||||
|
|
|
@ -16,15 +16,19 @@
|
||||||
module Google
|
module Google
|
||||||
class APIClient
|
class APIClient
|
||||||
module ENV
|
module ENV
|
||||||
OS_VERSION = if RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/
|
OS_VERSION = begin
|
||||||
# TODO(bobaman)
|
if RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/
|
||||||
# Confirm that all of these Windows environments actually have access
|
# TODO(bobaman)
|
||||||
# to the `ver` command.
|
# Confirm that all of these Windows environments actually have access
|
||||||
`ver`.sub(/\s*\[Version\s*/, '/').sub(']', '').strip
|
# to the `ver` command.
|
||||||
elsif RUBY_PLATFORM =~ /darwin/i
|
`ver`.sub(/\s*\[Version\s*/, '/').sub(']', '').strip
|
||||||
"Mac OS X/#{`sw_vers -productVersion`}"
|
elsif RUBY_PLATFORM =~ /darwin/i
|
||||||
else
|
"Mac OS X/#{`sw_vers -productVersion`}"
|
||||||
`uname -sr`.sub(' ', '/')
|
else
|
||||||
|
`uname -sr`.sub(' ', '/')
|
||||||
|
end
|
||||||
|
rescue Exception
|
||||||
|
RUBY_PLATFORM
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue