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.



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

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



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

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.



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

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.



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

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")


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

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

#remove(file) ⇒ Object

Removes the given GoogleDrive::File from the collection.



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

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

#resource_idObject



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

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

#root?Boolean

Returns true if this is a root collection

Returns:

  • (Boolean)


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

def root?
  self.document_feed_url == ROOT_URL
end

#spreadsheets(params = {}) ⇒ Object

Returns all the spreadsheets in the collection.



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

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.



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

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

#subcollections(params = {}) ⇒ Object

Returns all its subcollections.



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

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.



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

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