From 21b0a3dead47ffdf37c77ca1d250f9b1f02a8855 Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Tue, 24 Feb 2015 14:57:12 -0800 Subject: [PATCH] Ensures that the loaded files are closed --- lib/googleauth/service_account.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/googleauth/service_account.rb b/lib/googleauth/service_account.rb index 0cd841e..4e8072b 100644 --- a/lib/googleauth/service_account.rb +++ b/lib/googleauth/service_account.rb @@ -77,7 +77,9 @@ module Google return nil unless ENV.key?(ENV_VAR) path = ENV[ENV_VAR] fail 'file #{path} does not exist' unless File.exist?(path) - return new(scope, File.open(path)) + File.open(path) do |f| + return new(scope, f) + end rescue StandardError => e raise "#{NOT_FOUND_ERROR}: #{e}" end @@ -86,13 +88,14 @@ module Google # # @param scope [string|array] the scope(s) to access def from_well_known_path(scope) - home_var = windows? ? 'APPDATA' : 'HOME' + home_var, base = windows? ? 'APPDATA' : 'HOME', WELL_KNOWN_PATH root = ENV[home_var].nil? ? '' : ENV[home_var] - base = WELL_KNOWN_PATH base = File.join('.config', base) unless windows? path = File.join(root, base) return nil unless File.exist?(path) - return new(scope, File.open(path)) + File.open(path) do |f| + return new(scope, f) + end rescue StandardError => e raise "#{WELL_KNOWN_ERROR}: #{e}" end