Class: FlashFlow::Release::GoogleDrive

Inherits:
Object
  • Object
show all
Defined in:
lib/flash_flow/google_drive.rb

Constant Summary collapse

DRIVE =
Google::Apis::DriveV3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGoogleDrive

Returns a new instance of GoogleDrive.



12
13
14
15
16
# File 'lib/flash_flow/google_drive.rb', line 12

def initialize
  scopes = [DRIVE::AUTH_DRIVE]
  @client = DRIVE::DriveService.new
  @client.authorization = Google::Auth.get_application_default(scopes)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/flash_flow/google_drive.rb', line 8

def client
  @client
end

Instance Method Details

#find_files(query) ⇒ Object



24
25
26
27
# File 'lib/flash_flow/google_drive.rb', line 24

def find_files(query)
  response = @client.list_files(q: query)
  response.files
end

#set_file_permissions(file_id, config = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/flash_flow/google_drive.rb', line 29

def set_file_permissions(file_id, config={})
  @client.batch do
    %w(group user).each do |type|
      %w(reader writer).each do |role| # owner is currently not supported
        config.dig('permissions', type, role).to_s.split(',').each do |email|
          permission = DRIVE::Permission.new(role: role, type: type, email_address: email)
          @client.create_permission(file_id, permission, email_message: config[:email_body], send_notification_email: config['notify'])
        end
      end
    end
  end
end

#upload_file(local_file, config = {}) ⇒ Object



18
19
20
21
22
# File 'lib/flash_flow/google_drive.rb', line 18

def upload_file(local_file, config={})
   = DRIVE::File.new(name: File.basename(local_file), extension: 'pdf')
   = @client.create_file(, upload_source: local_file, content_type: 'application/pdf')
  set_file_permissions(.id, config)
end