Class: GoogleDrive::Collection

Inherits:
File
  • Object
show all
Includes:
Util
Defined in:
lib/google_drive/collection.rb

Overview

Represents a folder in Google Drive.

Use GoogleDrive::Session#root_collection, GoogleDrive::Collection#subcollections, or GoogleDrive::Session#collection_by_url to get GoogleDrive::Collection object.

Constant Summary

Constants included from Util

Util::EXT_TO_CONTENT_TYPE, Util::IMPORTABLE_CONTENT_TYPE_MAP

Instance Attribute Summary

Attributes inherited from File

#api_file

Instance Method Summary collapse

Methods included from Util

concat_url, construct_and_query, construct_query, convert_params, delegate_api_methods, encode_query, get_singleton_class, h

Methods inherited from File

#acl, #acl_feed_url, #available_content_types, #copy, #delete, #document_feed_url, #download_to_file, #download_to_io, #download_to_string, #export_as_file, #export_as_string, #export_to_io, #human_url, #initialize, #inspect, #reload_metadata, #rename, #resource_id, #resource_type, #title, #update_from_file, #update_from_io, #update_from_string

Constructor Details

This class inherits a constructor from GoogleDrive::File

Instance Method Details

#add(file) ⇒ Object

Adds the given GoogleDrive::File to the folder.



21
22
23
24
25
26
# File 'lib/google_drive/collection.rb', line 21

def add(file)
  @session.drive.update_file(
    file.id, add_parents: id, fields: '', supports_team_drives: true
  )
  nil
end

#contents_urlObject

Returns URL of the deprecated contents feed.



148
149
150
# File 'lib/google_drive/collection.rb', line 148

def contents_url
  document_feed_url + '/contents'
end

#create_subcollection(title) ⇒ Object Also known as: create_subfolder

Creates a sub-folder with given title. Returns GoogleDrive::Collection object.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/google_drive/collection.rb', line 37

def create_subcollection(title)
   = {
    name: title,
    mime_type: 'application/vnd.google-apps.folder',
    parents: [id]
  }
  file = @session.drive.create_file(
    , fields: '*', supports_team_drives: true
  )
  @session.wrap_api_file(file)
end

#documents(params = {}, &block) ⇒ Object

Returns all the Google Docs documents in the folder.

By default, it returns the first 100 documents. See document of GoogleDrive::Session#files method for how to get all documents.



109
110
111
# File 'lib/google_drive/collection.rb', line 109

def documents(params = {}, &block)
  files_with_type('application/vnd.google-apps.document', params, &block)
end

#file_by_title(title) ⇒ Object Also known as: file_by_name

Returns a file (can be a spreadsheet, document, subfolder or other files) in the folder which exactly matches title as GoogleDrive::File. Returns nil if not found. If multiple folders with the title are found, returns one of them.

If given an Array, does a recursive subfolder traversal.



129
130
131
# File 'lib/google_drive/collection.rb', line 129

def file_by_title(title)
  file_by_title_with_type(title, nil)
end

#files(params = {}, &block) ⇒ Object Also known as: contents

Returns all the files (including spreadsheets, documents, subfolders) in the folder. You can specify parameters documented at developers.google.com/drive/v3/web/search-parameters

e.g.

# Gets all the files in the folder, including subfolders.
collection.files

# Gets only files with title "hoge".
collection.files(q: "name = 'hoge'")

# Same as above with a placeholder.
collection.files(q: ["name = ?", "hoge"])

By default, it returns the first 100 files. See document of GoogleDrive::Session#files method for how to get all files.



73
74
75
# File 'lib/google_drive/collection.rb', line 73

def files(params = {}, &block)
  files_with_type(nil, params, &block)
end

#remove(file) ⇒ Object

Removes the given GoogleDrive::File from the folder.



29
30
31
32
33
# File 'lib/google_drive/collection.rb', line 29

def remove(file)
  @session.drive.update_file(
    file.id, remove_parents: id, fields: '', supports_team_drives: true
  )
end

#root?Boolean

Returns true if this is a root folder.

Returns:

  • (Boolean)


52
53
54
# File 'lib/google_drive/collection.rb', line 52

def root?
  !api_file.parents || api_file.parents.empty?
end

#spreadsheets(params = {}, &block) ⇒ Object

Returns all the spreadsheets in the folder.

By default, it returns the first 100 spreadsheets. See document of GoogleDrive::Session#files method for how to get all spreadsheets.



101
102
103
# File 'lib/google_drive/collection.rb', line 101

def spreadsheets(params = {}, &block)
  files_with_type('application/vnd.google-apps.spreadsheet', params, &block)
end

#subcollection_by_title(title) ⇒ Object Also known as: subfolder_by_name

Returns its subfolder whose title exactly matches title as GoogleDrive::Collection. Returns nil if not found. If multiple folders with the title are found, returns one of them.

If given an Array, does a recursive subfolder traversal.



141
142
143
# File 'lib/google_drive/collection.rb', line 141

def subcollection_by_title(title)
  file_by_title_with_type(title, 'application/vnd.google-apps.folder')
end

#subcollections(params = {}, &block) ⇒ Object Also known as: subfolders

Returns all its subfolders.

By default, it returns the first 100 subfolders. See document of GoogleDrive::Session#files method for how to get all subfolders.



117
118
119
# File 'lib/google_drive/collection.rb', line 117

def subcollections(params = {}, &block)
  files_with_type('application/vnd.google-apps.folder', params, &block)
end

#upload_from_file(path, title = nil, params = {}) ⇒ Object

Uploads a file to this folder. See Session#upload_from_file for details.



78
79
80
81
# File 'lib/google_drive/collection.rb', line 78

def upload_from_file(path, title = nil, params = {})
  params = { parents: [id] }.merge(params)
  @session.upload_from_file(path, title, params)
end

#upload_from_io(io, title = 'Untitled', params = {}) ⇒ Object

Uploads a file to this folder. See Session#upload_from_io for details.



84
85
86
87
# File 'lib/google_drive/collection.rb', line 84

def upload_from_io(io, title = 'Untitled', params = {})
  params = { parents: [id] }.merge(params)
  @session.upload_from_io(io, title, params)
end

#upload_from_string(content, title = 'Untitled', params = {}) ⇒ Object

Uploads a file to this folder. See Session#upload_from_string for details.



90
91
92
93
# File 'lib/google_drive/collection.rb', line 90

def upload_from_string(content, title = 'Untitled', params = {})
  params = { parents: [id] }.merge(params)
  @session.upload_from_string(content, title, params)
end