Class: Google::Apis::Core::UploadIO

Inherits:
Hurley::UploadIO
  • Object
show all
Defined in:
lib/google/apis/core/upload.rb

Overview

Extension of Hurley's UploadIO to add length accessor

Constant Summary collapse

OCTET_STREAM_CONTENT_TYPE =
'application/octet-stream'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_file(file_name, content_type: nil) ⇒ Google::Apis::Core::UploadIO

Create a new instance given a file path

Parameters:

  • file_name (String, File)

    Path to file

  • content_type (String) (defaults to: nil)

    Optional content type. If nil, will attempt to auto-detect

Returns:



48
49
50
51
52
53
54
# File 'lib/google/apis/core/upload.rb', line 48

def self.from_file(file_name, content_type: nil)
  if content_type.nil?
    type = MIME::Types.of(file_name)
    content_type = type.first.content_type unless type.nil? || type.empty?
  end
  new(file_name, content_type || OCTET_STREAM_CONTENT_TYPE)
end

.from_io(io, content_type: OCTET_STREAM_CONTENT_TYPE) ⇒ Google::Apis::Core::UploadIO

Wraps an IO stream in UploadIO

Parameters:

  • io (#read)

    IO to wrap

  • content_type (String) (defaults to: OCTET_STREAM_CONTENT_TYPE)

    Optional content type.

Returns:



62
63
64
# File 'lib/google/apis/core/upload.rb', line 62

def self.from_io(io, content_type: OCTET_STREAM_CONTENT_TYPE)
  new(io, content_type)
end

Instance Method Details

#lengthFixnum

Get the length of the stream

Returns:

  • (Fixnum)


38
39
40
# File 'lib/google/apis/core/upload.rb', line 38

def length
  io.respond_to?(:length) ? io.length : File.size(local_path)
end