Class: Factorix::Transfer::Uploader::ProgressIO

Inherits:
Object
  • Object
show all
Defined in:
lib/factorix/transfer/uploader.rb

Overview

Wrapper IO that tracks read progress

Instance Method Summary collapse

Constructor Details

#initialize(io, total_size) {|Integer| ... } ⇒ ProgressIO

Returns a new instance of ProgressIO.

Parameters:

  • io (IO)

    underlying IO stream

  • total_size (Integer)

    total size in bytes

Yields:

  • (Integer)

    current number of bytes read



138
139
140
141
142
143
# File 'lib/factorix/transfer/uploader.rb', line 138

def initialize(io, total_size, &block)
  @io = io
  @total_size = total_size
  @current = 0
  @callback = block
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close underlying IO



167
# File 'lib/factorix/transfer/uploader.rb', line 167

def close = @io.close

#eof?Boolean

Check if at end of stream

Returns:

  • (Boolean)

    true if at EOF



162
# File 'lib/factorix/transfer/uploader.rb', line 162

def eof? = @io.eof?

#read(length = nil, outbuf = nil) ⇒ String?

Read data from underlying IO and track progress

Parameters:

  • length (Integer, nil) (defaults to: nil)

    number of bytes to read

  • outbuf (String, nil) (defaults to: nil)

    output buffer

Returns:

  • (String, nil)

    read data or nil at EOF



150
151
152
153
154
155
156
157
# File 'lib/factorix/transfer/uploader.rb', line 150

def read(length=nil, outbuf=nil)
  data = @io.read(length, outbuf)
  if data
    @current += data.bytesize
    @callback&.call(@current)
  end
  data
end

#rewindvoid

This method returns an undefined value.

Rewind underlying IO



172
173
174
175
# File 'lib/factorix/transfer/uploader.rb', line 172

def rewind
  @io.rewind
  @current = 0
end

#sizeInteger

Get current size

Returns:

  • (Integer)

    total size



180
# File 'lib/factorix/transfer/uploader.rb', line 180

def size = @total_size