Class: FileTransfer::Sftp
Instance Attribute Summary collapse
-
#sftp ⇒ Object
readonly
Returns the value of attribute sftp.
Attributes inherited from Generic
#host, #keys, #password, #port, #timeout_seconds, #username
Instance Method Summary collapse
- #close ⇒ Object
-
#download(from_path, to_path) ⇒ Object
Downloads data from SFTP server.
-
#exist?(path) ⇒ Boolean
Checks if remote file exists.
-
#initialize(options = {}) ⇒ Sftp
constructor
A new instance of Sftp.
-
#list(path) ⇒ Array<String>
Gets directory listing from SFTP server.
-
#open_connection(conn_timeout = timeout_seconds) { ... } ⇒ Object
Opens new connection, if not already open.
-
#remove(path) ⇒ Object
Deletes file / folder from SFTP server.
-
#rename(from_path, to_path) ⇒ Object
Moves file / folder from SFTP server.
-
#test_connection ⇒ Object
Connects to server.
-
#upload(from_path, to_path) ⇒ Object
Uploads data to SFTP server.
Methods inherited from Generic
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( = {}) [:port] ||= 22 super() end |
Instance Attribute Details
#sftp ⇒ Object (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
#close ⇒ Object
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
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
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
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
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
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
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_connection ⇒ Object
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
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 |