Class: FtpMultipartDownload::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/ftp_multipart_download/part.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Part

Returns a new instance of Part.

Parameters:

  • connection (Net::FTP)

    an instance of Net::FTP.



7
8
9
# File 'lib/ftp_multipart_download/part.rb', line 7

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/ftp_multipart_download/part.rb', line 3

def connection
  @connection
end

Instance Method Details

#download(remotefile, localfile, blocksize, offset, limit) ⇒ Object

download a part of file.

Parameters:

  • remotefile (String)

    remote file path.

  • localfile (String)

    local file path.

  • blocksize (Integer)

    block size.

  • offset (Integer)

    offset

  • limit (Integer)

    limit



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ftp_multipart_download/part.rb', line 19

def download(remotefile, localfile, blocksize, offset, limit)
  old_resume, connection.resume = connection.resume, true

  File.open(localfile, 'wb') do |f|
    retrieved = 0
    connection.retrbinary("RETR #{remotefile}", blocksize, offset) do |chunk|
      retrievable = [limit - retrieved, chunk.bytesize].min
      break if retrievable <= 0

      retrieved += f.write(chunk[0, retrievable])
    end
  end
ensure
  connection.resume = old_resume
end