lib/eco/api/usecases/default_cases/samples/sftp_case.rb in eco-helpers-3.0.19 vs lib/eco/api/usecases/default_cases/samples/sftp_case.rb in eco-helpers-3.0.20

- old
+ new

@@ -5,11 +5,11 @@ require_relative 'cli/sftp_cli' name "sftp" type :other - CONST_REFERRAL = /^(?:::)?(?:[A-Z][a-zA-Z0-9_]*(?:::[A-Z][a-zA-Z0-9_]*)*)$/.freeze + CONST_REFERRAL = /^(?:::)?(?:[A-Z][a-zA-Z0-9_]*(?:::[A-Z][a-zA-Z0-9_]*)*)$/ def main(session, options, _usecase) options[:end_get] = false raise "The SFTP is not configured" unless session.sftp? @@ -29,20 +29,21 @@ # Can't pass this via CLI option, as it breaks the regular expression def file_pattern(require: true) fpc = file_pattern_const return fpc if fpc - return unless require + return unless require msg = "(#{self.class}) You should redefine the file_pattern function " msg << "as a RegEx expression that matches the target remote file" raise WrongConst, msg end def file_pattern_const if (fpc = options.dig(:sftp, :file_pattern_const)) raise WrongConst, "(#{self.class}) Invalid file pattern const referral: #{fpc}" unless fpc.match(CONST_REFERRAL) + begin self.eval(fpc) rescue NameError self.class.const_get(fpc) end @@ -52,10 +53,11 @@ end # Ex: "/IN/Personnel" def remote_subfolder(require: true) rm_sf = options.dig(:sftp, :remote_subfolder) + return rm_sf if rm_sf return unless require msg = "(#{self.class}) You should redefine remote_subfolder " msg << "as the folder where the target file sits. Ex: /IN/Personnel" @@ -76,11 +78,11 @@ def local_folder options.dig(:sftp, :local_folder) || "." end - def with_remote_files(folder: self.remote_folder, pattern: self.file_pattern) + def with_remote_files(folder: remote_folder, pattern: file_pattern) sftp.files(folder, pattern: pattern).each do |remote_file| yield(remote_file) if block_given? end rescue ArgumentError raise @@ -98,35 +100,40 @@ with_remote_files {|file| puts file.longname} end def get_files with_remote_files.tap do |files| - unless files.empty? - file_names = files.map {|file| to_remote_path(file.name)} - puts "Getting the following files into the local folder '#{local_folder}':" - puts file_names - sftp.download(file_names, local_folder: local_folder) - end + next if files.empty? + + file_names = files.map {|file| to_remote_path(file.name)} + + puts "Getting the following files into the local folder '#{local_folder}':" + puts file_names + + sftp.download(file_names, local_folder: local_folder) end end def get_last with_remote_files.last.tap do |file| - if file - file_name = to_remote_path(file.name) - puts "Getting the following file: #{file_name}" - sftp.download(file_name, local_folder: local_folder) - end + next unless file + + file_name = to_remote_path(file.name) + puts "Getting the following file: #{file_name}" + + sftp.download(file_name, local_folder: local_folder) end end def archive_files with_remote_files do |file| source = to_remote_path(file.name) # should probably be file.longname dest = to_remote_path("#{archive_subfolder}/#{file.name}") move_file(source, dest) end.tap do |files| - puts "Moved the file(s) to the #{archive_subfolder} folder" unless files.empty? + next if files.empty? + + puts "Moved the file(s) to the #{archive_subfolder} folder" end end def move_file(source, dest) sftp.move(source, dest, 0x0001) do |response|