Class: GoogleDrive::File

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Util
Defined in:
lib/google_drive/file.rb

Overview

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.

In addition to the methods below, properties defined here are also available as attributes: developers.google.com/drive/v3/reference/files#resource

e.g.,

file.mime_type  # ==> "text/plain"

Direct Known Subclasses

Collection, Spreadsheet

Constant Summary

Constants included from Util

Util::EXT_TO_CONTENT_TYPE, Util::IMPORTABLE_CONTENT_TYPE_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

concat_url, construct_and_query, construct_query, convert_params, delegate_api_methods, encode_query, get_singleton_class, h

Constructor Details

#initialize(session, api_file) ⇒ File

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of File.



27
28
29
30
31
32
# File 'lib/google_drive/file.rb', line 27

def initialize(session, api_file)
  @session = session
  @api_file = api_file
  @acl = nil
  delegate_api_methods(self, @api_file, [:title])
end

Instance Attribute Details

#api_fileObject (readonly)

Wrapped Google::APIClient::Schema::Drive::V3::File object.



35
36
37
# File 'lib/google_drive/file.rb', line 35

def api_file
  @api_file
end

Instance Method Details

#acl(params = {}) ⇒ Object

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.

e.g.

# Dumps people who have access:
for entry in file.acl
  p [entry.type, entry.email_address, entry.role]
  # => e.g. ["user", "[email protected]", "owner"]
end

# Shares the file with new people:
# NOTE: This sends email to the new people.
file.acl.push(
    {type: "user", email_address: "[email protected]", role: "reader"})
file.acl.push(
    {type: "user", email_address: "[email protected]", role: "writer"})

# Changes the role of a person:
file.acl[1].role = "writer"

# Deletes an ACL entry:
file.acl.delete(file.acl[1])


227
228
229
230
# File 'lib/google_drive/file.rb', line 227

def acl(params = {})
  @acl = Acl.new(@session, self) if !@acl || params[:reload]
  @acl
end

#acl_feed_urlObject

Deprecated ACL feed URL of the file.



54
55
56
# File 'lib/google_drive/file.rb', line 54

def acl_feed_url
  document_feed_url + '/acl'
end

#available_content_typesObject

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

This returns zero or one file type. You may be able to download the file in other formats using export_as_file, export_as_string, or export_to_io.



81
82
83
# File 'lib/google_drive/file.rb', line 81

def available_content_types
  api_file.web_content_link ? [api_file.mime_type] : []
end

#copy(title) ⇒ Object Also known as: duplicate

Creates copy of this file with the given title.



196
197
198
199
# File 'lib/google_drive/file.rb', line 196

def copy(title)
  api_file = @session.drive.copy_file(id, { name: title }, {})
  @session.wrap_api_file(api_file)
end

#delete(permanent = false) ⇒ Object

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



178
179
180
181
182
183
184
185
# File 'lib/google_drive/file.rb', line 178

def delete(permanent = false)
  if permanent
    @session.drive.delete_file(id)
  else
    @session.drive.update_file(id, { trashed: true }, {})
  end
  nil
end

#document_feed_urlObject

URL of feed used in the deprecated document list feed API.



49
50
51
# File 'lib/google_drive/file.rb', line 49

def document_feed_url
  'https://docs.google.com/feeds/default/private/full/' + CGI.escape(resource_id)
end

#download_to_file(path, params = {}) ⇒ Object

Downloads the file to a local file. e.g.

file.download_to_file("/path/to/hoge.txt")

To export the file in other formats, use export_as_file.



89
90
91
# File 'lib/google_drive/file.rb', line 89

def download_to_file(path, params = {})
  @session.drive.get_file(id, {download_dest: path}.merge(params))
end

#download_to_io(io, params = {}) ⇒ Object

Downloads the file and writes it to io.

To export the file in other formats, use export_to_io.



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

def download_to_io(io, params = {})
  @session.drive.get_file(id, {download_dest: io}.merge(params))
end

#download_to_string(params = {}) ⇒ Object

Downloads the file and returns as a String.

To export the file in other formats, use export_as_string.



96
97
98
99
100
# File 'lib/google_drive/file.rb', line 96

def download_to_string(params = {})
  sio = StringIO.new
  download_to_io(sio, params)
  sio.string
end

#export_as_file(path, format = nil) ⇒ Object

Export the file to path in content type format. If format is nil, it is guessed from the file name.

e.g.,

spreadsheet.export_as_file("/path/to/hoge.csv")
spreadsheet.export_as_file("/path/to/hoge", "text/csv")

If you want to download the file in the original format, use download_to_file instead.



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/google_drive/file.rb', line 117

def export_as_file(path, format = nil)
  unless format
    format = EXT_TO_CONTENT_TYPE[::File.extname(path).downcase]
    unless format
      fail(ArgumentError,
           ("Cannot guess format from the file name: %s\n" \
            'Specify format argument explicitly.') %
           path)
    end
  end
  export_to_dest(path, format)
end

#export_as_string(format) ⇒ Object

Export the file as String in content type format.

e.g.,

spreadsheet.export_as_string("text/csv")

If you want to download the file in the original format, use download_to_string instead.



136
137
138
139
140
# File 'lib/google_drive/file.rb', line 136

def export_as_string(format)
  sio = StringIO.new
  export_to_dest(sio, format)
  sio.string
end

#export_to_io(io, format) ⇒ Object

Export the file to io in content type format.

If you want to download the file in the original format, use download_to_io instead.



145
146
147
# File 'lib/google_drive/file.rb', line 145

def export_to_io(io, format)
  export_to_dest(io, format)
end

#human_urlObject

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

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



72
73
74
# File 'lib/google_drive/file.rb', line 72

def human_url
  api_file.web_view_link
end

#inspectObject



232
233
234
# File 'lib/google_drive/file.rb', line 232

def inspect
  "\#<%p id=%p title=%p>" % [self.class, id, title]
end

#reload_metadataObject

Reloads file metadata such as title and acl.



38
39
40
41
# File 'lib/google_drive/file.rb', line 38

def 
  @api_file = @session.drive.get_file(id, fields: '*')
  @acl = Acl.new(@session, self) if @acl
end

#rename(title) ⇒ Object Also known as: title=

Renames title of the file.



188
189
190
191
# File 'lib/google_drive/file.rb', line 188

def rename(title)
  @session.drive.update_file(id, { name: title }, {})
  nil
end

#resource_idObject

Returns resource_type + “:” + id.



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

def resource_id
  '%s:%s' % [resource_type, id]
end

#resource_typeObject

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



59
60
61
# File 'lib/google_drive/file.rb', line 59

def resource_type
  mime_type.slice(/^application\/vnd.google-apps.(.+)$/, 1) || 'file'
end

#title(params = {}) ⇒ Object

Title of the file.



64
65
66
67
# File 'lib/google_drive/file.rb', line 64

def title(params = {})
   if params[:reload]
  api_file.name
end

#update_from_file(path, params = {}) ⇒ Object

Updates the file with the content of the local file.

e.g.

file.update_from_file("/path/to/hoge.txt")


161
162
163
164
165
166
167
# File 'lib/google_drive/file.rb', line 161

def update_from_file(path, params = {})
  # Somehow it doesn't work if I specify the file name directly as upload_source.
  open(path, 'rb') do |f|
    update_from_io(f, params)
  end
  nil
end

#update_from_io(io, params = {}) ⇒ Object

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



170
171
172
173
174
# File 'lib/google_drive/file.rb', line 170

def update_from_io(io, params = {})
  params = { upload_source: io }.merge(params)
  @session.drive.update_file(id, nil, params)
  nil
end

#update_from_string(content, params = {}) ⇒ Object

Updates the file with content.

e.g.

file.update_from_string("Good bye, world.")


153
154
155
# File 'lib/google_drive/file.rb', line 153

def update_from_string(content, params = {})
  update_from_io(StringIO.new(content), params)
end