class Tumugi::Plugin::GoogleDriveFileTarget

Attributes

file_id[R]
mime_type[R]
name[R]
parents[R]

Public Class Methods

new(file_id: nil, name:, parents: nil, mime_type: nil, fs: nil) click to toggle source
Calls superclass method
# File lib/tumugi/plugin/target/google_drive_file.rb, line 14
def initialize(file_id: nil, name:, parents: nil, mime_type: nil, fs: nil)
  @fs = fs unless fs.nil?
  @file_id = file_id
  @name = name
  @parents = parents
  @mime_type = mime_type

  super(@file_id)
end

Public Instance Methods

exist?() click to toggle source
# File lib/tumugi/plugin/target/google_drive_file.rb, line 44
def exist?
  if file_id
    fs.exist?(file_id)
  else
    !!find_by_name(name)
  end
end
fs() click to toggle source
# File lib/tumugi/plugin/target/google_drive_file.rb, line 24
def fs
  @fs ||= Tumugi::Plugin::GoogleDrive::FileSystem.new(Tumugi.config.section('google_drive'))
end
open(mode="r", mime_type: @mime_type, &block) click to toggle source
# File lib/tumugi/plugin/target/google_drive_file.rb, line 28
def open(mode="r", mime_type: @mime_type, &block)
  if mode.include? 'r'
    if file_id.nil?
      file = find_by_name(name)
      @file_id = file.id unless file.nil?
    end
    fs.download(file_id, mode: mode, mime_type: mime_type, &block)
  elsif mode.include? 'w'
    file = Tumugi::Plugin::GoogleDrive::AtomicFile.new(name, fs, file_id: file_id, parents: @parents, mime_type: mime_type)
    file.open(&block)
    @file_id = file.id
  else
    raise Tumugi::TumugiError.new("Invalid mode: #{mode}")
  end
end
to_s() click to toggle source
# File lib/tumugi/plugin/target/google_drive_file.rb, line 52
def to_s
  s = "file_id: #{file_id}, name: #{name}"
  s += ", parents: #{parents}" if parents
  s
end
url() click to toggle source
# File lib/tumugi/plugin/target/google_drive_file.rb, line 58
def url
  file = fs.get_file_metadata(file_id)
  file.web_view_link
end

Private Instance Methods

find_by_name(n) click to toggle source
# File lib/tumugi/plugin/target/google_drive_file.rb, line 65
def find_by_name(n)
  query =  "name='#{n}'"
  ps = parents
  if parents.is_a?(String)
    ps = [parents]
  end
  if parents
    query += " and ("
    query += "#{ps.map{|p| "'#{p}' in parents"}.join(" or ")}"
    query += ")"
  end
  files = fs.list_files(query: query, page_size: 2).files
  if files.size == 0
    nil
  elsif files.size == 1
    files.first
  else
    raise Tumugi::TumugiError.new("Multiple files find for query: #{query}")
  end
end