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_service.update_file(
    file.id, add_parents: id, fields: '', supports_team_drives: true
  )
  nil
end

#contents_urlObject

Returns URL of the deprecated contents feed.


168
169
170
# File 'lib/google_drive/collection.rb', line 168

def contents_url
  document_feed_url + '/contents'
end

#create_file(title, file_properties = {}) ⇒ Object

Creates a file with given title and properties in this folder. Returns objects with the following types: GoogleDrive::Spreadsheet, GoogleDrive::File, GoogleDrive::Collection

You can pass a MIME Type using the file_properties-function parameter, for example: create_file('Document Title', mime_type: 'application/vnd.google-apps.document')

A list of available Drive MIME Types can be found here: developers.google.com/drive/v3/web/mime-types


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/google_drive/collection.rb', line 58

def create_file(title, file_properties = {})
   = {
    name: title,
    parents: [id]
  }.merge(file_properties)

  file = @session.drive_service.create_file(
    , fields: '*', supports_team_drives: true
  )

  @session.wrap_api_file(file)
end

#create_spreadsheet(title, file_properties = {}) ⇒ Object

Creates a spreadsheet with given title in this folder. Returns GoogleDrive::Spreadsheet object.


45
46
47
# File 'lib/google_drive/collection.rb', line 45

def create_spreadsheet(title, file_properties = {})
  create_file(title, file_properties.merge(mime_type: 'application/vnd.google-apps.spreadsheet'))
end

#create_subcollection(title, file_properties = {}) ⇒ Object Also known as: create_subfolder

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


37
38
39
# File 'lib/google_drive/collection.rb', line 37

def create_subcollection(title, file_properties = {})
  create_file(title, file_properties.merge(mime_type: 'application/vnd.google-apps.folder'))
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.


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

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.


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

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.


93
94
95
# File 'lib/google_drive/collection.rb', line 93

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_service.update_file(
    file.id, remove_parents: id, fields: '', supports_team_drives: true
  )
end

#root?Boolean

Returns true if this is a root folder.


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

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.


121
122
123
# File 'lib/google_drive/collection.rb', line 121

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.


161
162
163
# File 'lib/google_drive/collection.rb', line 161

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.


137
138
139
# File 'lib/google_drive/collection.rb', line 137

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.


98
99
100
101
# File 'lib/google_drive/collection.rb', line 98

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.


104
105
106
107
# File 'lib/google_drive/collection.rb', line 104

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.


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

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