class DriveService
DriveService
module implements a service that interfaces with Google Drive using Google Drive API.
Public Class Methods
new(filename = nil)
click to toggle source
Creates a new Google Drive Shell object.
@param [String] filename
filename of json containing credentials downloaded from Google.
# File lib/gdsh/drive.rb, line 18 def initialize(filename = nil) # default to per-file permissions @oauth_scope = 'https://www.googleapis.com/auth/drive.file' @redirect_uri = 'urn:ietf:wg:oauth:2.0:oob' @filename = filename if filename && File.exist?(filename) credentials_from_file else credentials_from_stdin end end
Public Instance Methods
credentials_from_file()
click to toggle source
# File lib/gdsh/drive.rb, line 31 def credentials_from_file File.open(@filename, 'r') do |f| buffer = f.read credentials = JSON.parse(buffer) @client_id = credentials['installed']['client_id'] @client_secret = credentials['installed']['client_secret'] end end
credentials_from_stdin()
click to toggle source
Get credentials from shell if no credentials file was specified.
# File lib/gdsh/drive.rb, line 90 def credentials_from_stdin # get preset if exists @client_id ||= '' @client_secret ||= '' # Ask from user otherwise if @client_id == '' print 'Please enter your client id: ' @client_id = $stdin.gets.chomp end if @client_secret == '' print 'Please enter your client secret: ' @client_secret = $stdin.gets.chomp end end
init_client()
click to toggle source
# File lib/gdsh/drive.rb, line 40 def init_client # Create a new API client & load the Google Drive API @client = Google::APIClient.new # Request authorization @client.authorization.client_id = @client_id @client.authorization.client_secret = @client_secret @client.authorization.scope = @oauth_scope @client.authorization.redirect_uri = @redirect_uri end
puts_refresh_error()
click to toggle source
# File lib/gdsh/drive.rb, line 51 def puts_refresh_error puts 'Could not refresh token from saved session.'.colorize(:red) end
write_session_info_to_file()
click to toggle source
# File lib/gdsh/drive.rb, line 107 def write_session_info_to_file return if @client.nil? f = File.new('.session.yaml', 'w') f.write(@client.to_yaml) f.close end