class DevTrainingBot::GoogleDriveService

Constants

APPLICATION_NAME
CLIENT_SECRETS_PATH
CREDENTIALS_PATH
DOC_URL
FORMATS
OOB_URI
SCOPE

Attributes

service[R]

Public Class Methods

formats() click to toggle source
# File lib/dev_training_bot/services/google_drive_service.rb, line 29
def self.formats
  FORMATS.keys
end
mime(format) click to toggle source
# File lib/dev_training_bot/services/google_drive_service.rb, line 33
def self.mime(format)
  FORMATS[format]
end
new() click to toggle source
# File lib/dev_training_bot/services/google_drive_service.rb, line 37
def initialize
  @service = Google::Apis::DriveV3::DriveService.new
  @service.client_options.application_name = APPLICATION_NAME
  @service.authorization = authorize
end

Private Instance Methods

authorize() click to toggle source

Ensure valid credentials, either by restoring from the saved credentials files or intitiating an OAuth2 authorization. If authorization is required, the user's default browser will be launched to approve the request.

@return [Google::Auth::UserRefreshCredentials] OAuth2 credentials

# File lib/dev_training_bot/services/google_drive_service.rb, line 51
def authorize
  client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
  authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
  user_id = 'default'
  credentials = authorizer.get_credentials(user_id)

  if credentials.nil?
    url = authorizer.get_authorization_url(base_url: OOB_URI)

    puts 'Open the following URL in the browser and enter the ' \
      "resulting code after authorization:\n" + url
    code = STDIN.gets

    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI
    )
  end

  credentials
end