class Libis::Ingester::FtpsService

Attributes

host[RW]
password[RW]
port[RW]
user[RW]

Public Class Methods

new(_host, _port, _user, _password) click to toggle source

Create FTP service param [String] host param [Integer] port param [String] user param [String] password

# File lib/libis/ingester/ftps_service.rb, line 12
def initialize(_host, _port, _user, _password)
  @host = _host
  @port = _port
  @user = _user
  @password = _password
  @ftp_service = DoubleBagFTPS.new
  connect
end

Public Instance Methods

del_dir(remote_path) click to toggle source

Delete a directory param [String] remote_path remote directory

# File lib/libis/ingester/ftps_service.rb, line 70
def del_dir(remote_path)
  check do
    ftp_service.rmdir(remote_path)
  end
end
del_file(remote_path) click to toggle source

Delete a file param [String] remote_path remote file path

# File lib/libis/ingester/ftps_service.rb, line 62
def del_file(remote_path)
  check do
    ftp_service.delete(remote_path)
  end
end
del_tree(remote_path) click to toggle source

Delete a directory param [String] remote_path remote directory

# File lib/libis/ingester/ftps_service.rb, line 78
def del_tree(remote_path)
  ls(remote_path).map do |file|
    is_file?(file) ? del_file(file) : del_tree(file)
  end
  del_dir(remote_path)
end
exist?(remote_path) click to toggle source
# File lib/libis/ingester/ftps_service.rb, line 85
def exist?(remote_path)
  check do
    begin
      ftp_service.size(remote_path)
      true
    rescue ::Net::FTPReplyError, ::Net::FTPPermError
      return false
    end
  end
end
get_file(remote_path, local_path, mode = :binary) click to toggle source

Download a file param [String] remote_path remote file path param [String] local_path param [Symbol] mode :binary or :text

# File lib/libis/ingester/ftps_service.rb, line 34
def get_file(remote_path, local_path, mode = :binary)
  check do
    mode == :binary ?
        ftp_service.getbinaryfile(remote_path, local_path) :
        ftp_service.gettextfile(remote_path, local_path)
  end
end
is_file?(remote_path) click to toggle source

Check if remote path is a file (or a directory) param [String] remote_path return [Boolean] true if file, false if directory

# File lib/libis/ingester/ftps_service.rb, line 99
def is_file?(remote_path)
  ftp_service.size(remote_path).is_a?(Numeric) ? true : false rescue false
end
ls(dir) click to toggle source

Get directory listing param [String] dir return [Array<String>]

# File lib/libis/ingester/ftps_service.rb, line 24
def ls(dir)
  check do
    ftp_service.nlst(dir)
  end
end
put_file(remote_path, data, mode = :text) click to toggle source

Upload a file param [String] remote_path remote file path param [Object] data param [Symbol] mode :binary or :text

# File lib/libis/ingester/ftps_service.rb, line 46
def put_file(remote_path, data, mode = :text)
  tempfile = Tempfile.new('ftp_upload')
  mode == :text ?
      data.each { |line| tempfile.puts(line) } :
      tempfile.write(data)
  tempfile.close
  check do
    mode == :text ?
        ftp_service.puttextfile(tempfile.path, remote_path) :
        ftp_service.putbinaryfile(tempfile.path, remote_path)
  end
  tempfile.unlink
end

Protected Instance Methods

check() { || ... } click to toggle source

Tries to execute ftp commands; reconnects and tries again if connection timed out

# File lib/libis/ingester/ftps_service.rb, line 113
def check
  begin
    yield
  rescue Errno::ETIMEDOUT, Net::FTPConnectionError
    disconnect
    connect
    yield
  end
end
connect() click to toggle source

Connect to FTP server

# File lib/libis/ingester/ftps_service.rb, line 124
def connect
  ftp_service.open_timeout = 10.0
  ftp_service.ftps_mode = DoubleBagFTPS::EXPLICIT
  ftp_service.ssl_context = DoubleBagFTPS.create_ssl_context(verify_mode: OpenSSL::SSL::VERIFY_NONE)
  ftp_service.connect(host, port)
  ftp_service.login user, password
  ftp_service.passive = true
  ftp_service.read_timeout = 120.0
end
disconnect() click to toggle source

Disconnect from FTP server

# File lib/libis/ingester/ftps_service.rb, line 135
def disconnect
  ftp_service.close
rescue
  # do nothing
end
ftp_service() click to toggle source

@return [DoubleBagFTPS]

# File lib/libis/ingester/ftps_service.rb, line 108
def ftp_service
  @ftp_service
end