Class: Backup::GoogleDriveAuth
- Inherits:
-
Object
- Object
- Backup::GoogleDriveAuth
show all
- Includes:
- Config::Helpers
- Defined in:
- lib/backup/storage/google/google_drive_auth.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
included
Constructor Details
Returns a new instance of GoogleDriveAuth.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/backup/storage/google/google_drive_auth.rb', line 7
def initialize(options = {})
defaults = {
client_id: "",
client_secret: "",
api_version: "v2",
cache_path: ".cache",
scope: "https://www.googleapis.com/auth/drive",
authorization_uri: "https://accounts.google.com/o/oauth2/auth",
token_credential_uri: "https://accounts.google.com/o/oauth2/token",
redirect_uri: "urn:ietf:wg:oauth:2.0:oob"
}
@options = defaults.merge(options)
connect
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Backup::Config::Helpers
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
5
6
7
|
# File 'lib/backup/storage/google/google_drive_auth.rb', line 5
def client
@client
end
|
#drive ⇒ Object
Returns the value of attribute drive.
5
6
7
|
# File 'lib/backup/storage/google/google_drive_auth.rb', line 5
def drive
@drive
end
|
Instance Method Details
#auth ⇒ Object
35
36
37
|
# File 'lib/backup/storage/google/google_drive_auth.rb', line 35
def auth
file_storage.authorization
end
|
#authorize_and_cache ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/backup/storage/google/google_drive_auth.rb', line 39
def authorize_and_cache
require "timeout"
Logger.info "Creating a new authorization!"
client = Google::APIClient.new(
application_name: "Ruby backup to google drive",
application_version: "0.1.0"
)
authorization = Signet::OAuth2::Client.new(@options)
template = Backup::Template.new(
auth: authorization,
credential_store_file: credential_store_file
)
template.render("storage/google_drive/authorization_url.erb")
Timeout::timeout(180) {
authorization.code = STDIN.gets
}
authorization.fetch_access_token!
template.render("storage/google_drive/authorized.erb")
write_cache!(authorization)
template.render("storage/google_drive/cache_file_written.erb")
client.authorization = authorization
client
rescue => err
raise Error.wrap(err, "Could not authorize")
end
|
#authorized_client ⇒ Object
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/backup/storage/google/google_drive_auth.rb', line 75
def authorized_client
client = Google::APIClient.new(
application_name: "Ruby backup to google drive",
application_version: "0.1.0"
)
client.authorization = auth
client
end
|
#connect ⇒ Object
24
25
26
27
28
29
|
# File 'lib/backup/storage/google/google_drive_auth.rb', line 24
def connect
@client = (auth.nil? ? authorize_and_cache : authorized_client)
@drive = @client.discovered_api("drive", @options[:api_version])
rescue => err
raise Error.wrap(err, "Authorization Failed")
end
|
#credential_store_file ⇒ Object
86
87
88
89
|
# File 'lib/backup/storage/google/google_drive_auth.rb', line 86
def credential_store_file
path = @options[:cache_path].start_with?("/") ? @options[:cache_path] : File.join(Config.root_path, @options[:cache_path])
File.join(path, @options[:client_id] + @options[:client_secret])
end
|
#file_storage ⇒ Object
31
32
33
|
# File 'lib/backup/storage/google/google_drive_auth.rb', line 31
def file_storage
Google::APIClient::FileStorage.new(credential_store_file)
end
|
#write_cache!(authorization) ⇒ Object
91
92
93
94
|
# File 'lib/backup/storage/google/google_drive_auth.rb', line 91
def write_cache!(authorization)
FileUtils.mkdir_p File.dirname(credential_store_file)
file_storage.write_credentials(authorization)
end
|