Ensures that the loaded files are closed

This commit is contained in:
Tim Emiola 2015-02-24 14:57:12 -08:00
parent 7adcf42958
commit 21b0a3dead
1 changed files with 7 additions and 4 deletions

View File

@ -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