adds method path= to proof if file can be accessed

This commit is contained in:
Volker Zöpfel 2013-11-14 08:31:21 +01:00
parent 146ccad83a
commit 10a337b807
1 changed files with 7 additions and 6 deletions

View File

@ -24,7 +24,7 @@ module Google
# #
class FileStorage class FileStorage
# @return [String] Path to the credentials file. # @return [String] Path to the credentials file.
attr_accessor :path attr_reader :path
# @return [Signet::OAuth2::Client] Path to the credentials file. # @return [Signet::OAuth2::Client] Path to the credentials file.
attr_reader :authorization attr_reader :authorization
@ -41,17 +41,18 @@ module Google
## ##
# Attempt to read in credentials from the specified file. # Attempt to read in credentials from the specified file.
# TODO: add methods to:
# check if file exists
# check if file is writeable
# check json structure is valid
def load_credentials def load_credentials
cached_credentials = JSON.load(self.path) cached_credentials = JSON.load(path)
@authorization = Signet::OAuth2::Client.new(cached_credentials) @authorization = Signet::OAuth2::Client.new(cached_credentials)
@authorization.issued_at = Time.at(cached_credentials['issued_at']) @authorization.issued_at = Time.at(cached_credentials['issued_at'])
self.refresh_authorization if @authorization.expired? self.refresh_authorization if @authorization.expired?
end end
def path=(value)
@path=value
raise "Credentials on path #{path} not accessible" unless File.exists?(@path) && File.readable?(@path) && File.writable?(@path)
end
## ##
# refresh credentials and save them to file # refresh credentials and save them to file
def refresh_authorization def refresh_authorization