class GoogleDrive::File

A file in Google Drive, including Google Docs document/spreadsheet/presentation.

Use GoogleDrive::Session#files or GoogleDrive::Session#file_by_title to get this object.

Attributes

document_feed_url[R]

URL of feed used in document list feed API.

Public Instance Methods

acl(params = {}) click to toggle source

Returns GoogleDrive::Acl object for the file.

With the object, you can see and modify people who can access the file. Modifications take effect immediately.

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

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])
# File lib/google_drive/file.rb, line 243
def acl(params = {})
  if !@acl || params[:reload]
    @acl = Acl.new(@session, self.acl_feed_url)
  end
  return @acl
end
acl_feed_url() click to toggle source

ACL feed URL of the file.

# File lib/google_drive/file.rb, line 88
def acl_feed_url
  orig_acl_feed_url = self.document_feed_entry_internal.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 "#{DOCS_BASE_URL}/#{$1}/acl"
    else
      raise(GoogleDrive::Error,
        "ACL feed URL is in unknown format: #{orig_acl_feed_url}")
  end
end
available_content_types() click to toggle source

Content types you can specify in methods #download_to_file, #download_to_string, #download_to_io .

# File lib/google_drive/file.rb, line 105
def available_content_types
  return self.document_feed_entry_internal.css("content").map(){ |c| c["type"] }
end
delete(permanent = false) click to toggle source

If permanent is false, moves the file to the trash. If permanent is true, deletes the file permanently.

# File lib/google_drive/file.rb, line 194
def delete(permanent = false)
  url = to_v3_url(self.document_feed_url)
  url = concat_url(url, "?delete=true") if permanent
  @session.request(:delete, url,
    :auth => :writely, :header => {"If-Match" => "*"})
end
document_feed_entry(params = {}) click to toggle source

<entry> element of document list feed as Nokogiri::XML::Element.

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

# File lib/google_drive/file.rb, line 48
def document_feed_entry(params = {})
  warn(
      "WARNING: GoogleDrive::file\#document_feed_entry is deprecated and will be removed " +
      "in the next version.")
  return self.document_feed_entry_internal(params)
end
download_to_file(path, params = {}) click to toggle source

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")
# File lib/google_drive/file.rb, line 114
def download_to_file(path, params = {})
  params = params.dup()
  if !params[:content_type]
    params[:content_type] = EXT_TO_CONTENT_TYPE[::File.extname(path).downcase]
    params[:content_type_is_hint] = true
  end
  open(path, "wb") do |f|
    download_to_io(f, params)
  end
end
download_to_io(io, params = {}) click to toggle source

Downloads the file and writes it to io.

# File lib/google_drive/file.rb, line 137
def download_to_io(io, params = {})
  all_contents = self.document_feed_entry_internal.css("content")
  if params[:content_type] && (!params[:content_type_is_hint] || all_contents.size > 1)
    contents = all_contents.select(){ |c| c["type"] == params[:content_type] }
  else
    contents = all_contents
  end
  if contents.size == 1
    url = contents[0]["src"]
  else
    if contents.empty?
      raise(GoogleDrive::Error,
          ("Downloading with content type %p not supported for this file. " +
           "Specify one of these to content_type: %p") %
          [params[:content_type], self.available_content_types])
    else
      raise(GoogleDrive::Error,
          ("Multiple content types are available for this file. " +
           "Specify one of these to content_type: %p") %
          [self.available_content_types])
    end
  end
  # TODO Use streaming if possible.
  body = @session.request(:get, url, :response_type => :raw, :auth => :writely)
  io.write(body)
end
download_to_string(params = {}) click to toggle source

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."
# File lib/google_drive/file.rb, line 130
def download_to_string(params = {})
  sio = StringIO.new()
  download_to_io(sio, params)
  return sio.string
end
human_url() click to toggle source

URL to view/edit the file in a Web browser.

e.g. “docs.google.com/file/d/xxxx/edit

# File lib/google_drive/file.rb, line 83
def human_url
  return self.document_feed_entry_internal.css("link[rel='alternate']")[0]["href"]
end
inspect() click to toggle source
# File lib/google_drive/file.rb, line 250
def inspect
  fields = {:document_feed_url => self.document_feed_url}
  fields[:title] = self.title if @document_feed_entry
  return "\#<%p %s>" % [self.class, fields.map(){ |k, v| "%s=%p" % [k, v] }.join(", ")]
end
rename(title) click to toggle source

Renames title of the file.

# File lib/google_drive/file.rb, line 202
def rename(title)
  edit_url = self.document_feed_entry_internal.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
  header = {"Content-Type" => "application/atom+xml;charset=utf-8", "If-Match" => "*"}
  @session.request(:put, edit_url, :data => xml, :auth => :writely, :header => header)
end
Also aliased as: title=
resource_id() click to toggle source

Resource ID.

# File lib/google_drive/file.rb, line 64
def resource_id
  return self.document_feed_entry_internal.css("gd|resourceId").text
end
resource_type() click to toggle source

The type of resourse. e.g. “document”, “spreadsheet”, “folder”

# File lib/google_drive/file.rb, line 69
def resource_type
  return self.resource_id.split(/:/)[0]
end
title(params = {}) click to toggle source

Title of the file.

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

# File lib/google_drive/file.rb, line 76
def title(params = {})
  return document_feed_entry_internal(params).css("title").text
end
title=(title)
Alias for: rename
update_from_file(path, params = {}) click to toggle source

Updates the file with the content of the local file.

e.g.

file.update_from_file("/path/to/hoge.txt")
# File lib/google_drive/file.rb, line 168
def update_from_file(path, params = {})
  params = {:file_name => ::File.basename(path)}.merge(params)
  open(path, "rb") do |f|
    update_from_io(f, params)
  end
end
update_from_io(io, params = {}) click to toggle source

Reads content from io and updates the file with the content.

# File lib/google_drive/file.rb, line 184
def update_from_io(io, params = {})
  params = {:header => {"If-Match" => "*"}}.merge(params)
  initial_url = self.document_feed_entry_internal.css(
      "link[rel='http://schemas.google.com/g/2005#resumable-edit-media']")[0]["href"]
  @document_feed_entry = @session.upload_raw(
      :put, initial_url, io, self.title, params)
end
update_from_string(content, params = {}) click to toggle source

Updates the file with content.

e.g.

file.update_from_string("Good bye, world.")
# File lib/google_drive/file.rb, line 179
def update_from_string(content, params = {})
  update_from_io(StringIO.new(content), params)
end