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

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, new_upload_io

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_media, #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.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/google_drive/collection.rb', line 20

def add(file)
  new_child = @session.drive.children.insert.request_schema.new({
      "id" => file.id,
  })
  @session.execute!(
      :api_method => @session.drive.children.insert,
      :body_object => new_child,
      :parameters => {
          "folderId" => self.id,
          "childId" => file.id,
      })
  return nil
end

#contents_urlObject

Returns URL of the deprecated contents feed.



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

def contents_url
  self.document_feed_url + "/contents"
end

#create_subcollection(title) ⇒ Object

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



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

def create_subcollection(title)
  file = @session.drive.files.insert.request_schema.new({
      "title" => title,
      "mimeType" => "application/vnd.google-apps.folder",
      "parents" => [{"id" => self.id}],
  })
  api_result = @session.execute!(
      :api_method => @session.drive.files.insert,
      :body_object => file)
  return @session.wrap_api_file(api_result.data)
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.



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

def documents(params = {}, &block)
  return 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.



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

def file_by_title(title)
  return 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/v2/reference/files/list

e.g.

# Gets all the files in collection, including subcollections.
collection.files
# Gets only files with title "hoge".
collection.files("q" => "title = 'hoge'")
# Same as above with a placeholder.
collection.files("q" => ["title = ?", "hoge"])

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



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

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

#remove(file) ⇒ Object

Removes the given GoogleDrive::File from the collection.



48
49
50
51
52
53
54
55
56
# File 'lib/google_drive/collection.rb', line 48

def remove(file)
  @session.execute!(
      :api_method => @session.drive.children.delete,
      :parameters => {
          "folderId" => self.id,
          "childId" => file.id,
      })
  return nil
end

#root?Boolean

Returns true if this is a root collection

Returns:

  • (Boolean)


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

def root?
  return self.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.



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

def spreadsheets(params = {}, &block)
  return 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.



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

def subcollection_by_title(title)
  return 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.



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

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