Class: IOP::SFTPFileReader

Inherits:
RandomAccessReader show all
Includes:
Feed
Defined in:
lib/iop/net/sftp.rb

Overview

Note:

this class depends on external +net-sftp+ gem.

Feed class to read file from SFTP server.

This class an adapter for +Net::SFTP::Session+ class.

Use case: retrieve current user's ~/.profile file from SFTP server running on local machine and and compute its MD5 hash sum.

require 'iop/digest'
require 'iop/net/sftp'
( IOP::SFTPFileReader.new('localhost', '.profile') | (d = IOP::DigestComputer.new(Digest::MD5.new)) ).process!
puts d.digest.hexdigest

Since:

  • 0.2

Instance Attribute Summary

Attributes included from Feed

#downstream

Instance Method Summary collapse

Methods included from Feed

#process, #|

Methods inherited from RandomAccessReader

#seek!

Constructor Details

#initialize(sftp, file, size: nil, offset: nil, block_size: DEFAULT_BLOCK_SIZE, **options) ⇒ SFTPFileReader

Creates class instance.

sftp can be either a +String+ of +Net::SFTP::Session+ instance. If it is a string a corresponding +Net::SFTP::Session+ instance will be created with options passed to its constructor.

If sftp is a string, a created SFTP session is managed, e.g. it is closed after the process is complete, otherwise supplied object is left as is and no closing is performed. This allows to reuse SFTP session for a sequence of operations.

Refer to +Net::SFTP+ documentation for available options.

Parameters:

  • sftp (String, Net::SFTP::Session)

    SFTP server to connect to

  • file (String)

    file name to process

  • size (Integer) (defaults to: nil)

    total number of bytes to read; +nil+ value instructs to read until end-of-data is reached

  • offset (Integer) (defaults to: nil)

    offset in bytes from the stream start to seek to; +nil+ value means no seeking is performed

  • block_size (Integer) (defaults to: DEFAULT_BLOCK_SIZE)

    size of blocks to process data with

  • options (Hash)

    extra keyword parameters passed to +Net::SFTP::Session+ constructor, such as username, password etc.

Since:

  • 0.2



73
74
75
76
77
78
# File 'lib/iop/net/sftp.rb', line 73

def initialize(sftp, file, size: nil, offset: nil, block_size: DEFAULT_BLOCK_SIZE, **options)
  super(size: size, offset: offset, block_size: block_size)
  @options = options
  @file = file
  @sftp = sftp
end

Instance Method Details

#process!Object

Since:

  • 0.2



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/iop/net/sftp.rb', line 80

def process!
  setup
  begin
    @io = @sftp.open!(@file, 'r')
    begin
      super
    ensure
      @sftp.close(@io)
    end
  ensure
    cleanup
  end
end