Class: Ngdrive::GdriveUtil

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

Overview

google drive util to download files from folder recursively

Constant Summary collapse

SCOPE =

rubocop:disable Metrics/ClassLength

Google::Apis::DriveV3::AUTH_DRIVE_READONLY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGdriveUtil

Returns a new instance of GdriveUtil.



18
19
20
21
22
23
# File 'lib/ngdrive.rb', line 18

def initialize
  @config = YAML.load_file('./settings.yml')
  @drive_service = Google::Apis::DriveV3::DriveService.new
  @drive_service.client_options.application_name = @config['app']['name']
  @drive_service.authorization = authorize
end

Instance Attribute Details

#drive_serviceObject (readonly)

Returns the value of attribute drive_service.



16
17
18
# File 'lib/ngdrive.rb', line 16

def drive_service
  @drive_service
end

Instance Method Details

#download!(root, path = []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ngdrive.rb', line 53

def download!(root, path = [])
  return unless root

  folder = create_folder!(path)

  root[:files].each do |file|
    file_path = "#{folder}/#{file[:name]}"
    drive_service.get_file(file[:id], download_dest: file_path)
  end

  folders = root.keys.reject { |x| x == :files }
  folders.each do |folder_key|
    download!(root[folder_key], path + [folder_key])
  end
end

#download_filesObject



48
49
50
51
# File 'lib/ngdrive.rb', line 48

def download_files
  entries = get_entries(root_folder_id)
  download!(entries)
end

#download_folderObject



44
45
46
# File 'lib/ngdrive.rb', line 44

def download_folder
  @config['app']['download_dir']
end

#get_entries(folder_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ngdrive.rb', line 25

def get_entries(folder_id)
  all_files = fetch_files(folder_id)
  folders = get_folders(all_files['items'])
  files = get_files(all_files['items'])

  entries = {}
  entries[:files] = files.map { |x| { id: x['id'], name: x['title'] } }
  folders.each do |folder|
    title = folder['title']
    entries[title] = get_entries(folder['id'])
  end

  entries
end

#root_folder_idObject



40
41
42
# File 'lib/ngdrive.rb', line 40

def root_folder_id
  @config['gdrive']['folder_id']
end