Skip to content

Commit

Permalink
Get root collection and its contents
Browse files Browse the repository at this point in the history
  • Loading branch information
evgenyneu committed Jun 1, 2012
1 parent dd5554f commit 0f8be38
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
39 changes: 33 additions & 6 deletions lib/google_drive/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ module GoogleDrive

# Use GoogleDrive::Session#collection_by_url to get GoogleDrive::Collection object.
class Collection
ROOT_URL = 'https://docs.google.com/feeds/default/private/full/folder%3Aroot'

include(Util)
def initialize(session, collection_feed_url) #:nodoc:

def initialize(session, collection_feed_url = ROOT_URL) #:nodoc:
@session = session
@collection_feed_url = collection_feed_url
end
Expand All @@ -34,21 +35,47 @@ def add(file)
return nil
end

# Returns all the files in the collection.
def files
# Returns the child resources in the collection (spreadsheets, documents, folders).
#
# ==== Parameters
#
# * +params+ is used to filter the returned resources as described at
# https://developers.google.com/google-apps/documents-list/#getting_a_list_of_documents_and_files
#
# * +type+ can be 'spreadsheet', 'document', 'folder' etc.
# If +type+ parameter is absent or nil the method will return
# all types of resources including folders.
#
# ==== Examples:
#
# # Gets all resources in collection, *including folders*
# contents
#
# # gets only resources with title "hoge"
# contents "title" => "hoge", "title-exact" => "true"
#
# contents {}, "spreadsheet" # all speadsheets
# contents {}, "document" # all text documents
# contents {}, "folder" # all folders
def contents(params = {}, type = nil)
contents_url = concat_url(@collection_feed_url, "/contents")
unless type.nil?
contents_url << "/-/#{type}"
end
contents_url = concat_url contents_url, "?" + encode_query(params)
header = {"GData-Version" => "3.0", "Content-Type" => "application/atom+xml"}
doc = @session.request(:get, contents_url, :header => header, :auth => :writely)
return doc.css("feed > entry").map(){ |e| @session.entry_element_to_file(e) }
end


alias_method :files, :contents

# Returns all the spreadsheets in the collection.
def spreadsheets
return self.files.select(){ |f| f.is_a?(Spreadsheet) }
end

# TODO Add other operations.

end

end
21 changes: 16 additions & 5 deletions lib/google_drive/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ def collection_by_url(url)
return Collection.new(self, url)
end

# Returns the root collection
def root_collection
Collection.new self
end

# Creates new spreadsheet and returns the new GoogleDrive::Spreadsheet.
#
# e.g.
Expand Down Expand Up @@ -352,13 +357,19 @@ def upload_raw(method, url, io, title = "Untitled", params = {}) #:nodoc:
end

def entry_element_to_file(entry) #:nodoc:
type, resourceId = entry.css("gd|resourceId").text.split ':'
title = entry.css("title").text
worksheets_feed_link = entry.css(
"link[rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']")[0]
if worksheets_feed_link
return Spreadsheet.new(self, worksheets_feed_link["href"], title)

case type
when 'folder'
url = "https://docs.google.com/feeds/default/private/full/folder%3A#{resourceId}"
Collection.new self, url
when 'spreadsheet'
worksheets_feed_link = entry.css(
"link[rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']")[0]
Spreadsheet.new(self, worksheets_feed_link["href"], title)
else
return GoogleDrive::File.new(self, entry)
GoogleDrive::File.new(self, entry)
end
end

Expand Down

0 comments on commit 0f8be38

Please sign in to comment.