lib/eco/api/usecases/default_cases/samples/sftp_case.rb in eco-helpers-2.5.10 vs lib/eco/api/usecases/default_cases/samples/sftp_case.rb in eco-helpers-2.6.0

- old
+ new

@@ -1,7 +1,11 @@ class Eco::API::UseCases::DefaultCases::Samples::Sftp < Eco::API::Common::Loaders::UseCase - name "sftp-sample" + class WrongConst < ArgumentError; end + class MissRemoteFolder < ArgumentError; end + + require_relative 'cli/sftp_cli' + name "sftp" type :other CONST_REFERRAL = /^(?:::)?(?:[A-Z][a-zA-Z0-9_]*(?:::[A-Z][a-zA-Z0-9_]*)*)$/ def main(session, options, usecase) @@ -20,28 +24,34 @@ end private # Can't pass this via CLI option, as it breaks the regular expression - def file_pattern + def file_pattern(require: true) fpc = file_pattern_const return fpc if fpc - raise "You should redefine the file_pattern function as a RegEx expression that matches the target remote file" + raise WrongConst, "(#{self.class}) You should redefine the file_pattern function as a RegEx expression that matches the target remote file" if require end def file_pattern_const if fpc = options.dig(:sftp, :file_pattern_const) - raise "Invalid file pattern const referral: #{fpc}" unless fpc.match(CONST_REFERRAL) - self.eval(fpc) + 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 end + rescue NameError + raise WrongConst, "(#{self.class}) Unknown constant: #{fpc}" end # Ex: "/IN/Personnel" - def remote_subfolder + def remote_subfolder(require: true) rm_sf = options.dig(:sftp, :remote_subfolder) return rm_sf if rm_sf - raise "You should redefine remote_subfolder as the folder where the target file sits. Ex: /IN/Personnel" + raise MissRemoteFolder, "(#{self.class}) You should redefine remote_subfolder as the folder where the target file sits. Ex: /IN/Personnel" if require end # `remote_target_folder` overrides `sftp_config.remote_folder` as well as `remote_subfolder` # `remote_folder` overrides `sftp_config.remote_folder` but NOT `remote_subfolder` def remote_folder @@ -61,11 +71,13 @@ def with_remote_files begin sftp.files(remote_folder, pattern: file_pattern).each do |remote_file| yield(remote_file) if block_given? end - rescue Net::SFTP::StatusException => e - logger.error("SFTP samplecase: There was an error trying to access the remote folder '#{remote_folder}'") + rescue ArgumentError + raise + rescue ::Net::SFTP::StatusException => e + logger.error("(#{self.class}) There was an error trying to access the remote folder '#{remote_folder}'") [] end end def list_folder