Skip to content
This repository has been archived by the owner on Apr 4, 2018. It is now read-only.

Commit

Permalink
Extract token_file_exists? and load_file
Browse files Browse the repository at this point in the history
This makes mocking authentication a little easier.
  • Loading branch information
robyoung committed Dec 31, 2012
1 parent 61c2fa8 commit 96ff8c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lib/google_auth_bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class GoogleAuthentication
GOOGLE_REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

def self.create_from_config_file(scope, file_name, token_file)
config = YAML.load_file(file_name)
config = load_file(file_name)
client_id, client_secret = config[:google_client_id], config[:google_client_secret]
raise InvalidFileFormatError.new(token_file) if client_id.nil? or client_secret.nil?
GoogleAuthentication.new(
Expand All @@ -15,6 +15,10 @@ def self.create_from_config_file(scope, file_name, token_file)
client_secret,
token_file)
end

def self.load_file(file_name)
YAML.load_file(file_name)
end

def initialize(scope, client_id, client_secret, token_file)
@scope = scope
Expand All @@ -23,6 +27,10 @@ def initialize(scope, client_id, client_secret, token_file)
@token_file = token_file
end

def token_file_exists?
File.exists? @token_file
end

def get_tokens(authorization_code=nil)
client = Google::APIClient.new
setup_credentials(client, authorization_code)
Expand All @@ -38,10 +46,10 @@ def get_auth_url
end

def load_token_from_file
raise FileNotFoundError.new(@token_file) unless File.exists? @token_file
raise FileNotFoundError.new(@token_file) unless token_file_exists?

begin
token_data = YAML.load_file(@token_file)
token_data = GoogleAuthentication.load_file(@token_file)
token_data[:refresh_token] or raise
rescue
raise InvalidFileFormatError.new(@token_file)
Expand Down Expand Up @@ -74,7 +82,7 @@ def setup_credentials(client, code)
end

def refresh_tokens(client)
if File.exist? @token_file
if token_file_exists?
client.authorization.update_token!(refresh_token: load_token_from_file)
tokens = client.authorization.fetch_access_token
else
Expand Down
2 changes: 1 addition & 1 deletion lib/google_auth_bridge/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GoogleAuthenticationBridge
VERSION = "0.1.1"
VERSION = "0.1.2"
end

0 comments on commit 96ff8c6

Please sign in to comment.