Memoizes on_gce?

- also fixes an error in the gemspec.
This commit is contained in:
Tim Emiola 2015-02-11 20:52:47 -08:00
parent eea40d1d1e
commit e01b14cba8
2 changed files with 18 additions and 12 deletions

View File

@ -5,7 +5,7 @@ require 'googleauth/version'
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = 'googleauth' s.name = 'googleauth'
s.version = GoogleAuth::VERSION s.version = Google::Auth::VERSION
s.authors = ['Tim Emiola'] s.authors = ['Tim Emiola']
s.email = 'temiola@google.com' s.email = 'temiola@google.com'
s.homepage = 'https://github.com/google/google-auth-library-ruby' s.homepage = 'https://github.com/google/google-auth-library-ruby'
@ -13,7 +13,7 @@ Gem::Specification.new do |s|
s.description = <<-eos s.description = <<-eos
Allows simple authorization for accessing Google APIs. Allows simple authorization for accessing Google APIs.
Provide support Application Default Credentials, as described at Provide support Application Default Credentials, as described at
https://developers.google.com/accounts/docs/application-default-credentials https://developers.google.com/accounts/xdocs/application-default-credentials
eos eos
s.files = `git ls-files`.split("\n") s.files = `git ls-files`.split("\n")
@ -27,6 +27,7 @@ Gem::Specification.new do |s|
s.add_dependency 'faraday', '~> 0.9' s.add_dependency 'faraday', '~> 0.9'
s.add_dependency 'logging', '~> 1.8' s.add_dependency 'logging', '~> 1.8'
s.add_dependency 'jwt', '~> 1.2.1' s.add_dependency 'jwt', '~> 1.2.1'
s.add_dependency 'memoist', '~> 0.11.0'
s.add_dependency 'multi_json', '1.10.1' s.add_dependency 'multi_json', '1.10.1'
s.add_dependency 'signet', '~> 0.6.0' s.add_dependency 'signet', '~> 0.6.0'

View File

@ -29,6 +29,7 @@
require 'faraday' require 'faraday'
require 'googleauth/signet' require 'googleauth/signet'
require 'memoist'
module Google module Google
# Module Auth provides classes that provide Google-specific authorization # Module Auth provides classes that provide Google-specific authorization
@ -43,16 +44,20 @@ module Google
'instance/service-accounts/default/token' 'instance/service-accounts/default/token'
COMPUTE_CHECK_URI = 'http://169.254.169.254' COMPUTE_CHECK_URI = 'http://169.254.169.254'
# Detect if this appear to be a GCE instance, by checking if metadata class << self
# is available extend Memoist
def self.on_gce?(options = {}) # Detect if this appear to be a GCE instance, by checking if metadata
c = options[:connection] || Faraday.default_connection # is available
resp = c.get(COMPUTE_CHECK_URI) def on_gce?(options = {})
return false unless resp.status == 200 c = options[:connection] || Faraday.default_connection
return false unless resp.headers.key?('Metadata-Flavor') resp = c.get(COMPUTE_CHECK_URI)
return resp.headers['Metadata-Flavor'] == 'Google' return false unless resp.status == 200
rescue Faraday::ConnectionFailed return false unless resp.headers.key?('Metadata-Flavor')
return false return resp.headers['Metadata-Flavor'] == 'Google'
rescue Faraday::ConnectionFailed
return false
end
memoize :on_gce?
end end
# Overrides the super class method to change how access tokens are # Overrides the super class method to change how access tokens are