class Spectre::FTP::FTPConnection

Public Class Methods

new(host, username, password, opts, logger) click to toggle source
# File lib/spectre/ftp.rb, line 12
def initialize host, username, password, opts, logger
  @__logger = logger
  @__session = nil

  @__host = host
  @__username = username
  @__password = password
  @__opts = opts
end

Public Instance Methods

can_connect?() click to toggle source
# File lib/spectre/ftp.rb, line 34
def can_connect?
  begin
    connect!
    return true
  rescue
    return false
  end
end
close() click to toggle source
# File lib/spectre/ftp.rb, line 29
def close
  return unless @__session and not @__session.closed?
  @__session.close
end
connect!() click to toggle source
# File lib/spectre/ftp.rb, line 22
def connect!
  return unless @__session == nil or @__session.closed?
  @__logger.info "Connecting to '#{@__host}' with user '#{@__username}'"
  @__session = Net::FTP.new(@__host, @__opts)
  @__session.login @__username, @__password
end
download(remotefile, to: File.basename(remotefile)) click to toggle source
# File lib/spectre/ftp.rb, line 43
def download remotefile, to: File.basename(remotefile)
  connect!
  @__logger.info "Downloading '#{@__username}@#{@__host}:#{File.join @__session.pwd, remotefile}' to '#{File.expand_path to}'"
  @__session.getbinaryfile(remotefile, to)
end
list() click to toggle source
# File lib/spectre/ftp.rb, line 55
def list
  connect!
  file_list = @__session.list
  @__logger.info "Listing file in #{@__session.pwd}\n#{file_list}"
  file_list
end
upload(localfile, to: File.basename(localfile)) click to toggle source
# File lib/spectre/ftp.rb, line 49
def upload localfile, to: File.basename(localfile)
  connect!
  @__logger.info "Uploading '#{File.expand_path localfile}' to '#{@__username}@#{@__host}:#{File.join @__session.pwd, to}'"
  @__session.putbinaryfile(localfile, to)
end