class FileConvert::Conversion

Attributes

body[R]
file[R]

Public Class Methods

new(client, remote_file, mime_type) click to toggle source

Downloads remote file from Google Drive with given mime_type

@param [FileConvert::Client] client @param [FileConvert::File] remote_file @param [String] mime_type

Target mime_type the file gets converted to

@return [FileConvert::File] remote_file with requested conversions

# File lib/file_convert/conversion.rb, line 15
def initialize(client, remote_file, mime_type)
  @client = client
  @remote_file = remote_file
  @original_upload_result = remote_file.original_file
  @remote_file_data = remote_file.data
  @mime_type = mime_type

  # Fail if upload errored
  fail data_error_exception if @original_upload_result.error?
  # Fail if requested mime-type is not available
  fail missing_mime_type_exception unless export_links.key?(mime_type)

  @file = fetch_file
  @body = @file.body
  @remote_file.add_conversion(mime_type, self)
end

Public Instance Methods

save(target_path) click to toggle source

Saves result as file @param [String] target_path

@return [FileConvert::Conversion]

# File lib/file_convert/conversion.rb, line 37
def save(target_path)
  ::File.open(target_path, 'w') { |file| file.write(@file.body) }
end

Private Instance Methods

connection_error_exception(result) click to toggle source
# File lib/file_convert/conversion.rb, line 69
def connection_error_exception(result)
  Exception::DownloadConnectionError.new result
end
data_error_exception() click to toggle source
# File lib/file_convert/conversion.rb, line 61
def data_error_exception
  Exception::UploadConnectionError.new @original_upload_result
end
fetch_file() click to toggle source

Actually downloads the file Raises if request was not successfull

@return [Google::APIClient::Result]

# File lib/file_convert/conversion.rb, line 55
def fetch_file
  @client.execute(uri: export_links[@mime_type]).tap do |result|
    fail connection_error_exception(result) unless result.success?
  end
end
missing_mime_type_exception() click to toggle source
# File lib/file_convert/conversion.rb, line 65
def missing_mime_type_exception
  Exception::MissingConversionMimeType.new @mime_type, export_links.keys
end