Class: FileTransfer::Sftp

Inherits:
Generic
  • Object
show all
Defined in:
lib/file_transfer/sftp.rb

Instance Attribute Summary collapse

Attributes inherited from Generic

#host, #keys, #password, #port, #timeout_seconds, #username

Instance Method Summary collapse

Methods inherited from Generic

#move, #to_s

Constructor Details

#initialize(options = {}) ⇒ Sftp

Returns a new instance of Sftp.



8
9
10
11
# File 'lib/file_transfer/sftp.rb', line 8

def initialize(options = {})
  options[:port] ||= 22
  super(options)
end

Instance Attribute Details

#sftpObject (readonly)

Returns the value of attribute sftp.



6
7
8
# File 'lib/file_transfer/sftp.rb', line 6

def sftp
  @sftp
end

Instance Method Details

#closeObject



72
73
74
# File 'lib/file_transfer/sftp.rb', line 72

def close
 # do nothing
end

#download(from_path, to_path) ⇒ Object

Downloads data from SFTP server

Parameters:

  • from_path (String)

    locale directory

  • to_path (String)

    remote directory



21
22
23
24
25
# File 'lib/file_transfer/sftp.rb', line 21

def download(from_path, to_path)
  open_connection do
    sftp.download! from_path, to_path
  end
end

#exist?(path) ⇒ Boolean

Checks if remote file exists

Parameters:

  • path (String)

    remote directory

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/file_transfer/sftp.rb', line 64

def exist?(path)
  open_connection(60) do
    sftp.stat(path) do |response|
      response.ok?
    end
  end
end

#list(path) ⇒ Array<String>

Gets directory listing from SFTP server

Parameters:

  • path (String)

    remote directory

Returns:

  • (Array<String>)

    listing with all file paths



39
40
41
42
43
# File 'lib/file_transfer/sftp.rb', line 39

def list(path)
  open_connection(60) do
    sftp.dir.entries path
  end
end

#open_connection(conn_timeout = timeout_seconds) { ... } ⇒ Object

Opens new connection, if not already open

Parameters:

  • conn_timeout (Integer) (defaults to: timeout_seconds)

    IDLE timeout in seconds

Yields:

  • executes operation within open connection on server



79
80
81
82
83
84
# File 'lib/file_transfer/sftp.rb', line 79

def open_connection(conn_timeout = timeout_seconds)
  connect if closed?
  timeout(conn_timeout) do
    yield if block_given?
  end
end

#remove(path) ⇒ Object

Deletes file / folder from SFTP server

Parameters:

  • path (String)

    remote directory



47
48
49
50
51
# File 'lib/file_transfer/sftp.rb', line 47

def remove(path)
  open_connection(60) do
    sftp.remove! path
  end
end

#rename(from_path, to_path) ⇒ Object

Moves file / folder from SFTP server

Parameters:

  • from_path (String)

    remote source directory

  • to_path (String)

    remote destination directory



56
57
58
59
60
# File 'lib/file_transfer/sftp.rb', line 56

def rename(from_path, to_path)
  open_connection(60) do
    sftp.rename! from_path, to_path
  end
end

#test_connectionObject

Connects to server



14
15
16
# File 'lib/file_transfer/sftp.rb', line 14

def test_connection
 open_connection
end

#upload(from_path, to_path) ⇒ Object

Uploads data to SFTP server

Parameters:

  • from_path (String)

    locale directory

  • to_path (String)

    remote directory



30
31
32
33
34
# File 'lib/file_transfer/sftp.rb', line 30

def upload(from_path, to_path)
  open_connection do
    sftp.upload! from_path, to_path
  end
end