Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moving some methods from Spreadsheet to File.

Adding documents.
  • Loading branch information...
commit 73ead7a153ba41042f2c660e2fda1f00fcaee929 1 parent c654a27
@gimite authored
View
118 lib/google_spreadsheet/file.rb
@@ -9,6 +9,10 @@
module GoogleSpreadsheet
+ # A file in Google Drive, including Google Docs document/spreadsheet/presentation.
+ #
+ # Use GoogleSpreadsheet::Session#files or GoogleSpreadsheet::Session#file_by_title to
+ # get this object.
class File
include(Util)
@@ -18,9 +22,17 @@ def initialize(session, entry) #:nodoc:
@document_feed_entry = entry
@document_feed_url = entry ? entry.css("link[rel='self']")[0]["href"] : nil
@title = entry ? entry.css("title")[0].text : nil
+ @acl = nil
end
- attr_reader(:document_feed_url, :document_feed_entry, :title)
+ # URL of feed used in document list feed API.
+ attr_reader(:document_feed_url)
+
+ # <entry> element of document list feed as Nokogiri::XML::Element.
+ attr_reader(:document_feed_entry)
+
+ # Title of the file.
+ attr_reader(:title)
# URL to view/edit the file in a Web browser.
#
@@ -29,10 +41,33 @@ def human_url
return self.document_feed_entry.css("link[rel='alternate']")[0]["href"]
end
+ # ACL feed URL of the file.
+ def acl_feed_url
+ orig_acl_feed_url = self.document_feed_entry.css(
+ "gd|feedLink[rel='http://schemas.google.com/acl/2007#accessControlList']")[0]["href"]
+ case orig_acl_feed_url
+ when %r{^https?://docs.google.com/feeds/default/private/full/.*/acl(\?.*)?$}
+ return orig_acl_feed_url
+ when %r{^https?://docs.google.com/feeds/acl/private/full/([^\?]*)(\?.*)?$}
+ # URL of old API version. Converts to v3 URL.
+ return "https://docs.google.com/feeds/default/private/full/#{$1}/acl"
+ else
+ raise(GoogleSpreadsheet::Error,
+ "ACL feed URL is in unknown format: #{orig_acl_feed_url}")
+ end
+ end
+
+ # Content types you can specify in methods download_to_file, download_to_string,
+ # download_to_io.
def available_content_types
return self.document_feed_entry.css("content").map(){ |c| c["type"] }
end
+ # Downloads the file to a local file.
+ #
+ # e.g.
+ # file.download_to_file("/path/to/hoge.txt")
+ # file.download_to_file("/path/to/hoge", :content_type => "text/plain")
def download_to_file(path, params = {})
params = params.dup()
if !params[:content_type]
@@ -44,12 +79,18 @@ def download_to_file(path, params = {})
end
end
+ # Downloads the file and returns as a String.
+ #
+ # e.g.
+ # file.download_to_string() #=> "Hello world."
+ # file.download_to_string(:content_type => "text/plain") #=> "Hello world."
def download_to_string(params = {})
sio = StringIO.new()
download_to_io(sio, params)
return sio.string
end
+ # Downloads the file and writes it to +io+.
def download_to_io(io, params = {})
all_contents = self.document_feed_entry.css("content")
if params[:content_type] && (!params[:content_type_is_hint] || all_contents.size > 1)
@@ -77,6 +118,10 @@ def download_to_io(io, params = {})
io.write(body)
end
+ # Updates the file with the content of the local file.
+ #
+ # e.g.
+ # file.update_from_file("/path/to/hoge.txt")
def update_from_file(path, params = {})
params = {:file_name => ::File.basename(path)}.merge(params)
open(path, "rb") do |f|
@@ -84,10 +129,15 @@ def update_from_file(path, params = {})
end
end
- def update_from_string(body, params = {})
- update_from_io(StringIO.new(body), params)
+ # Updates the file with +content+.
+ #
+ # e.g.
+ # file.update_from_string("Good bye, world.")
+ def update_from_string(content, params = {})
+ update_from_io(StringIO.new(content), params)
end
+ # Reads content from +io+ and updates the file with the content.
def update_from_io(io, params = {})
params = {:header => {"If-Match" => "*"}}.merge(params)
initial_url = self.document_feed_entry.css(
@@ -96,6 +146,68 @@ def update_from_io(io, params = {})
:put, initial_url, io, self.title, params)
end
+ # If +permanent+ is +false+, moves the file to the trash.
+ # If +permanent+ is +true+, deletes the file permanently.
+ def delete(permanent = false)
+ @session.request(:delete,
+ self.document_feed_url + (permanent ? "?delete=true" : ""),
+ :auth => :writely, :header => {"If-Match" => "*"})
+ end
+
+ # Renames title of the file.
+ def rename(title)
+
+ doc = @session.request(:get, self.document_feed_url, :auth => :writely)
+ edit_url = doc.css("link[rel='edit']").first["href"]
+ xml = <<-"EOS"
+ <atom:entry
+ xmlns:atom="http://www.w3.org/2005/Atom"
+ xmlns:docs="http://schemas.google.com/docs/2007">
+ <atom:title>#{h(title)}</atom:title>
+ </atom:entry>
+ EOS
+
+ @session.request(
+ :put, edit_url, :data => xml, :auth => :writely,
+ :header => {"Content-Type" => "application/atom+xml", "If-Match" => "*"})
+
+ end
+
+ alias title= rename
+
+ # Returns GoogleSpreadsheet::Acl object for the file.
+ #
+ # With the object, you can see and modify people who can access the file.
+ # Modifications take effect immediately.
+ #
+ # Set <tt>params[:reload]</tt> to true to force reloading the title.
+ #
+ # e.g.
+ # # Dumps people who have access:
+ # for entry in file.acl
+ # p [entry.scope_type, entry.scope, entry.role]
+ # # => e.g. ["user", "example1@gmail.com", "owner"]
+ # end
+ #
+ # # Shares the file with new people:
+ # # NOTE: This sends email to the new people.
+ # file.acl.push(
+ # {:scope_type => "user", :scope => "example2@gmail.com", :role => "reader"})
+ # file.acl.push(
+ # {:scope_type => "user", :scope => "example3@gmail.com", :role => "writer"})
+ #
+ # # Changes the role of a person:
+ # file.acl[1].role = "writer"
+ #
+ # # Deletes an ACL entry:
+ # file.acl.delete(file.acl[1])
+ def acl(params = {})
+ if !@acl || params[:reload]
+ @acl = Acl.new(@session, self.acl_feed_url)
+ end
+ return @acl
+ end
+
def inspect
return "\#<%p document_feed_url=%p>" % [self.class, self.document_feed_url]
end
View
35 lib/google_spreadsheet/session.rb
@@ -108,7 +108,7 @@ def auth_token(auth = :wise)
# When this function returns +true+, it tries again.
attr_accessor :on_auth_fail
- # Returns list of files for the user as array of GoogleSpreadsheet::File.
+ # Returns list of files for the user as array of GoogleSpreadsheet::File or its subclass.
# You can specify query parameters described at
# https://developers.google.com/google-apps/documents-list/#getting_a_list_of_documents_and_files
#
@@ -122,6 +122,9 @@ def files(params = {})
return doc.css("feed > entry").map(){ |e| entry_element_to_file(e) }
end
+ # Returns GoogleSpreadsheet::File or its subclass whose title exactly matches +title+.
+ # Returns nil if not found. If multiple files with the +title+ are found, returns
+ # one of them.
def file_by_title(title)
return files("title" => title, "title-exact" => "true")[0]
end
@@ -248,10 +251,31 @@ def create_spreadsheet(
end
- def upload_from_string(body, title = "Untitled", params = {})
- return upload_from_io(StringIO.new(body), title, params)
+ # Uploads a file with the given +title+ and +content+.
+ #
+ # e.g.
+ # # Uploads and converts to a Google Docs document:
+ # session.upload_from_string(
+ # "Hello world.", "Hello", :content_type => "text/plain")
+ #
+ # # Uploads without conversion:
+ # session.upload_from_string(
+ # "Hello world.", "Hello", :content_type => "text/plain", :convert => false)
+ def upload_from_string(content, title = "Untitled", params = {})
+ return upload_from_io(StringIO.new(content), title, params)
end
+ # Uploads a local file.
+ #
+ # e.g.
+ # # Uploads and converts to a Google Docs document:
+ # session.upload_from_file("/path/to/hoge.txt")
+ #
+ # # Uploads without conversion:
+ # session.upload_from_file("/path/to/hoge.txt", "Hoge", :convert => false)
+ #
+ # # Uploads with explicit content type:
+ # session.upload_from_file("/path/to/hoge", "Hoge", :content_type => "text/plain")
def upload_from_file(path, title = nil, params = {})
file_name = ::File.basename(path)
params = {:file_name => file_name}.merge(params)
@@ -260,6 +284,7 @@ def upload_from_file(path, title = nil, params = {})
end
end
+ # Uploads a file. Reads content from +io+.
def upload_from_io(io, title = "Untitled", params = {})
doc = request(:get, "https://docs.google.com/feeds/default/private/full?v=3",
:auth => :writely)
@@ -268,7 +293,7 @@ def upload_from_io(io, title = "Untitled", params = {})
return upload_raw(:post, initial_url, io, title, params)
end
- def upload_raw(method, url, io, title = "Untitled", params = {})
+ def upload_raw(method, url, io, title = "Untitled", params = {}) #:nodoc:
params = {:convert => true}.merge(params)
total_bytes = io.size - io.pos
@@ -320,7 +345,7 @@ def upload_raw(method, url, io, title = "Untitled", params = {})
end
- def entry_element_to_file(entry)
+ def entry_element_to_file(entry) #:nodoc:
title = entry.css("title").text
worksheets_feed_link = entry.css(
"link[rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']")[0]
View
79 lib/google_spreadsheet/spreadsheet.rb
@@ -24,7 +24,6 @@ def initialize(session, worksheets_feed_url, title = nil) #:nodoc:
super(session, nil)
@worksheets_feed_url = worksheets_feed_url
@title = title
- @acl = nil
end
# URL of worksheet-based feed of the spreadsheet.
@@ -79,22 +78,6 @@ def document_feed_url
return "https://docs.google.com/feeds/documents/private/full/spreadsheet%3A#{self.key}"
end
- # ACL feed URL of the spreadsheet.
- def acl_feed_url
- orig_acl_feed_url = document_feed_entry.css(
- "gd|feedLink[rel='http://schemas.google.com/acl/2007#accessControlList']")[0]["href"]
- case orig_acl_feed_url
- when %r{^https?://docs.google.com/feeds/default/private/full/.*/acl$}
- return orig_acl_feed_url
- when %r{^https?://docs.google.com/feeds/acl/private/full/([^\?]*)(\?.*)?$}
- # URL of old API version. Converts to v3 URL.
- return "https://docs.google.com/feeds/default/private/full/#{$1}/acl"
- else
- raise(GoogleSpreadsheet::Error,
- "ACL feed URL is in unknown format: #{orig_acl_feed_url}")
- end
- end
-
# <entry> element of spreadsheet feed as Nokogiri::XML::Element.
#
# Set <tt>params[:reload]</tt> to true to force reloading the feed.
@@ -135,34 +118,6 @@ def duplicate(new_title = nil)
return Spreadsheet.new(@session, ss_url, new_title)
end
- # If +permanent+ is +false+, moves the spreadsheet to the trash.
- # If +permanent+ is +true+, deletes the spreadsheet permanently.
- def delete(permanent = false)
- @session.request(:delete,
- self.document_feed_url + (permanent ? "?delete=true" : ""),
- :auth => :writely, :header => {"If-Match" => "*"})
- end
-
- # Renames title of the spreadsheet.
- def rename(title)
- doc = @session.request(:get, self.document_feed_url, :auth => :writely)
- edit_url = doc.css("link[rel='edit']").first["href"]
- xml = <<-"EOS"
- <atom:entry
- xmlns:atom="http://www.w3.org/2005/Atom"
- xmlns:docs="http://schemas.google.com/docs/2007">
- <atom:category
- scheme="http://schemas.google.com/g/2005#kind"
- term="http://schemas.google.com/docs/2007#spreadsheet" label="spreadsheet"/>
- <atom:title>#{h(title)}</atom:title>
- </atom:entry>
- EOS
-
- @session.request(:put, edit_url, :data => xml, :auth => :writely)
- end
-
- alias title= rename
-
# Exports the spreadsheet in +format+ and returns it as String.
#
# +format+ can be either "xls", "csv", "pdf", "ods", "tsv" or "html".
@@ -242,40 +197,6 @@ def add_worksheet(title, max_rows = 100, max_cols = 20)
return Worksheet.new(@session, self, url, title)
end
- # Returns GoogleSpreadsheet::Acl object for the spreadsheet.
- #
- # With the object, you can see and modify people who can access the spreadsheet.
- # Modifications take effect immediately.
- #
- # Set <tt>params[:reload]</tt> to true to force reloading the title.
- #
- # e.g.
- # # Dumps people who have access:
- # for entry in spreadsheet.acl
- # p [entry.scope_type, entry.scope, entry.role]
- # # => e.g. ["user", "example1@gmail.com", "owner"]
- # end
- #
- # # Shares the spreadsheet with new people:
- # # NOTE: This sends email to the new people.
- # spreadsheet.acl.push(
- # {:scope_type => "user", :scope => "example2@gmail.com", :role => "reader"})
- # spreadsheet.acl.push(
- # {:scope_type => "user", :scope => "example3@gmail.com", :role => "writer"})
- #
- # # Changes the role of a person:
- # spreadsheet.acl[1].role = "writer"
- #
- # # Deletes an ACL entry:
- # spreadsheet.acl.delete(spreadsheet.acl[1])
-
- def acl(params = {})
- if !@acl || params[:reload]
- @acl = Acl.new(@session, self.acl_feed_url)
- end
- return @acl
- end
-
# DEPRECATED: Table and Record feeds are deprecated and they will not be available after
# March 2012.
#
Please sign in to comment.
Something went wrong with that request. Please try again.