class RailsDrive::Handler

Attributes

auth_client[R]
credentials[R]

Public Class Methods

new(user_id = 'default') click to toggle source
# File lib/rails_drive/handler.rb, line 14
def initialize(user_id = 'default')
  # Token store
  FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))
  client_id    = Auth::ClientId.from_file CLIENT_SECRETS_PATH
  token_store  = Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
  @user_id     = user_id
  # credentials
  @authorizer  = Auth::UserAuthorizer.new(client_id, SCOPE, token_store, CALLBACK_URI)
  @credentials = @authorizer.get_credentials(@user_id)
  # serive
  @service     = Google::Apis::DriveV3::DriveService.new
  @service.client_options.application_name = 'seo-report'
  @service.authorization = @credentials
end

Public Instance Methods

get_and_store_credentials_from_code(code) click to toggle source
# File lib/rails_drive/handler.rb, line 33
def get_and_store_credentials_from_code(code)
  @credentials = @authorizer.get_and_store_credentials_from_code(
    user_id: @user_id, code: code, base_url: BASE_URL)
  @service.authorization = @credentials
end
get_authorization_url() click to toggle source
# File lib/rails_drive/handler.rb, line 29
def get_authorization_url
  @authorizer.get_authorization_url base_url: BASE_URL
end
get_folder(name) click to toggle source
# File lib/rails_drive/handler.rb, line 52
def get_folder(name)
  options = {
    q: "mimeType='application/vnd.google-apps.folder' and name='#{name}'",
    spaces: 'drive',
    fields:'nextPageToken, files(id, name, webViewLink)'
  }
  @service.list_files options
end
has_credentials?() click to toggle source
# File lib/rails_drive/handler.rb, line 61
def has_credentials?
  @credentials
end
list_files(options={}) click to toggle source
# File lib/rails_drive/handler.rb, line 39
def list_files(options={})
  # List files under a folder
  conditions = []
  conditions << "'#{options[:folder]}' in parents" if options.has_key? :folder
  conditions << "name = '#{options[:file_name]}'" if options.has_key? :file_name
  options = {
    q: conditions.join(" and "),
    spaces: 'drive',
    fields:'nextPageToken, files(id, name, webViewLink)'
  }
  @service.list_files options
end