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 collapse

ROOT_URL =

:nodoc:

"#{DOCS_BASE_URL}/folder%3Aroot"

Constants included from Util

Util::DOCS_BASE_URL, Util::EXT_TO_CONTENT_TYPE

Instance Attribute Summary

Attributes inherited from File

#document_feed_url

Instance Method Summary collapse

Methods included from Util

concat_url, encode_query, h, to_v3_url

Methods inherited from File

#acl, #acl_feed_url, #available_content_types, #delete, #document_feed_entry, #download_to_file, #download_to_io, #download_to_string, #human_url, #initialize, #inspect, #rename, #resource_type, #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.



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

def add(file)
  header = {"GData-Version" => "3.0", "Content-Type" => "application/atom+xml"}
  xml = <<-"EOS"
    <entry xmlns="http://www.w3.org/2005/Atom">
      <id>#{h(file.document_feed_url)}</id>
    </entry>
  EOS
  @session.request(
      :post, self.contents_url, :data => xml, :header => header, :auth => :writely)
  return nil
end

#contents_urlObject



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

def contents_url
  if self.root?
    # The root collection doesn't have document feed.
    return concat_url(ROOT_URL, "/contents")
  else
    return self.document_feed_entry.css(
        "content[type='application/atom+xml;type=feed']")[0]["src"]
  end
end

#create_subcollection(title) ⇒ Object

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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/google_drive/collection.rb', line 61

def create_subcollection(title)
  header = {"GData-Version" => "3.0", "Content-Type" => "application/atom+xml"}
  xml = <<-EOS
    <entry xmlns="http://www.w3.org/2005/Atom">
      <category scheme="http://schemas.google.com/g/2005#kind"
        term="http://schemas.google.com/docs/2007#folder"/>
      <title>#{h(title)}</title>
    </entry>
  EOS
  doc = @session.request(
      :post, contents_url, :data => xml, :header => header, :auth => :writely)
  return @session.entry_element_to_file(doc)
end

#documents(params = {}) ⇒ Object

Returns all the Google Docs documents in the collection.



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

def documents(params = {})
  return files_with_type("document", params)
end

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

Returns all the files (including spreadsheets, documents, subcollections) in the collection.

You can specify query parameters described at developers.google.com/google-apps/documents-list/#getting_a_list_of_documents_and_files

e.g.

# Gets all the files in collection, including subcollections.
collection.files

# Gets only files with title "hoge".
collection.files("title" => "hoge", "title-exact" => "true")


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

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

#remove(file) ⇒ Object

Removes the given GoogleDrive::File from the collection.



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

def remove(file)
  url = to_v3_url("#{contents_url}/#{file.resource_id}")
  @session.request(:delete, url, :auth => :writely, :header => {"If-Match" => "*"})
end

#resource_idObject



43
44
45
# File 'lib/google_drive/collection.rb', line 43

def resource_id
  return self.root? ? nil : super
end

#root?Boolean

Returns true if this is a root collection

Returns:

  • (Boolean)


82
83
84
# File 'lib/google_drive/collection.rb', line 82

def root?
  self.document_feed_url == ROOT_URL
end

#spreadsheets(params = {}) ⇒ Object

Returns all the spreadsheets in the collection.



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

def spreadsheets(params = {})
  return files_with_type("spreadsheet", params)
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.



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

def subcollection_by_title(title)
  return subcollections("title" => title, "title-exact" => "true")[0]
end

#subcollections(params = {}) ⇒ Object

Returns all its subcollections.



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

def subcollections(params = {})
  return files_with_type("folder", params)
end

#title(params = {}) ⇒ Object

Title of the collection.

Set params[:reload] to true to force reloading the title.



34
35
36
37
38
39
40
41
# File 'lib/google_drive/collection.rb', line 34

def title(params = {})
  if self.root?
    # The root collection doesn't have document feed.
    return nil
  else
    return super
  end
end