Permalink
Browse files

Extract token_file_exists? and load_file

This makes mocking authentication a little easier.
  • Loading branch information...
1 parent 61c2fa8 commit 96ff8c693bf0120b2ad78ca7c06935dbbb26418d @robyoung robyoung committed Dec 31, 2012
Showing with 13 additions and 5 deletions.
  1. +12 −4 lib/google_auth_bridge.rb
  2. +1 −1 lib/google_auth_bridge/version.rb
View
16 lib/google_auth_bridge.rb
@@ -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(
@@ -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
@@ -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)
@@ -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)
@@ -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
View
2 lib/google_auth_bridge/version.rb
@@ -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.