Class: GoogleDrive::Collection

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

Overview

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 collection.



17
18
19
20
# File 'lib/google_drive/collection.rb', line 17

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

#contents_urlObject

Returns URL of the deprecated contents feed.



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

def contents_url
  document_feed_url + '/contents'
end

#create_subcollection(title) ⇒ Object

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



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

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

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

Returns all the Google Docs documents in the collection.

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



96
97
98
# File 'lib/google_drive/collection.rb', line 96

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

#file_by_title(title) ⇒ Object

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

If given an Array, does a recursive subcollection traversal.



114
115
116
# File 'lib/google_drive/collection.rb', line 114

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, subcollections) in the collection. You can specify parameters documented at developers.google.com/drive/v3/web/search-parameters

e.g.

# Gets all the files in the collection, including subcollections.
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.



60
61
62
# File 'lib/google_drive/collection.rb', line 60

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

#remove(file) ⇒ Object

Removes the given GoogleDrive::File from the collection.



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

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

#root?Boolean

Returns true if this is a root collection

Returns:

  • (Boolean)


39
40
41
# File 'lib/google_drive/collection.rb', line 39

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

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

Returns all the spreadsheets in the collection.

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



88
89
90
# File 'lib/google_drive/collection.rb', line 88

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

#subcollection_by_title(title) ⇒ Object

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

If given an Array, does a recursive subcollection traversal.



123
124
125
# File 'lib/google_drive/collection.rb', line 123

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

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

Returns all its subcollections.

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



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

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

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

Same as Session#upload_from_file. It uploads file to current collection



65
66
67
68
# File 'lib/google_drive/collection.rb', line 65

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

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

Same as Session#upload_from_io. It uploads file to current collection



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

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

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

Same as Session#upload_from_string. It uploads file to current collection



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

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