Class: Eco::API::Common::Session::SFTP
- Defined in:
- lib/eco/api/common/session/sftp.rb
Instance Method Summary collapse
-
#download(files, local_folder: nil) ⇒ Object
Downloads the files specified to a local folder.
- #entries(path) ⇒ Object
-
#files(path, pattern: nil) ⇒ Array<Net::SFTP::Protocol::V01::Name>
Files of the remote directory.
-
#folders(path, pattern: nil) ⇒ Array<Net::SFTP::Protocol::V01::Name>
Folders of the remote directory.
- #host ⇒ Object
-
#initialize(enviro:) ⇒ SFTP
constructor
A new instance of SFTP.
- #move(fullname_source, fullname_dest, flags = 0x0001, override: true, &callback) ⇒ Object
- #sftp_session ⇒ Object
Constructor Details
Instance Method Details
#download(files, local_folder: nil) ⇒ Object
Downloads the files specified to a local folder
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/eco/api/common/session/sftp.rb', line 74 def download(files, local_folder: nil) puts "Creating local files:" [files].flatten.compact.map do |fullname| basename = windows_basename(fullname) dest_fullname = File.join(local_folder || ".", basename) puts " • #{dest_fullname}" sftp_session.download(fullname, dest_fullname) end.each do |dw| # run SSH event loop while dw.active? dw.wait end end |
#entries(path) ⇒ Object
33 34 35 |
# File 'lib/eco/api/common/session/sftp.rb', line 33 def entries(path) sftp_session.dir.entries(path).sort_by {|rf| rf.name} end |
#files(path, pattern: nil) ⇒ Array<Net::SFTP::Protocol::V01::Name>
Files of the remote directory.
42 43 44 45 46 |
# File 'lib/eco/api/common/session/sftp.rb', line 42 def files(path, pattern: nil) entries = entries(path).select {|remote_file| remote_file.file?} return entries unless pattern entries.select {|remote_file| remote_file.name =~ pattern} end |
#folders(path, pattern: nil) ⇒ Array<Net::SFTP::Protocol::V01::Name>
Folders of the remote directory.
53 54 55 56 57 |
# File 'lib/eco/api/common/session/sftp.rb', line 53 def folders(path, pattern: nil) entries = entries(path).select {|remote_file| remote_file.directory?} return entries unless pattern entries.select {|remote_file| remote_file.name =~ pattern} end |
#host ⇒ Object
11 12 13 |
# File 'lib/eco/api/common/session/sftp.rb', line 11 def host @host ||= fetch_host end |
#move(fullname_source, fullname_dest, flags = 0x0001, override: true, &callback) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/eco/api/common/session/sftp.rb', line 60 def move(fullname_source, fullname_dest, flags=0x0001, override: true, &callback) begin sftp_session.rename!(fullname_source, fullname_dest, flags, &callback) rescue Net::SFTP::StatusException => e raise unless override sftp_session.remove(fullname_dest) sftp_session.rename!(fullname_source, fullname_dest, flags, &callback) end end |
#sftp_session ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/eco/api/common/session/sftp.rb', line 16 def sftp_session require "net/sftp" begin @sftp_session ||= Net::SFTP.start( host, fetch_user, ** ) rescue Exception => e msg = "Could not open SFTP session. Possible misconfiguration: #{e}" logger.error(msg) raise end @sftp_session end |