Class: Spectre::FTP::FTPConnection

Inherits:
DslClass
  • Object
show all
Defined in:
lib/spectre/ftp.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, username, password, opts, logger) ⇒ FTPConnection

Returns a new instance of FTPConnection.



12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#can_connect?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/spectre/ftp.rb', line 44

def can_connect?
  begin
    connect!
    return true
  rescue
    return false
  end
end

#closeObject



38
39
40
41
42
# File 'lib/spectre/ftp.rb', line 38

def close
  return unless @__session and not @__session.closed?

  @__session.close
end

#connect!Object



30
31
32
33
34
35
36
# File 'lib/spectre/ftp.rb', line 30

def connect!
  return unless @__session == nil or @__session.closed?

  @__logger.info "Connecting to '#{@__host}' with user '#{@__username}'"
  @__session = Net::FTP.new(@__host, @__opts)
  @__session. @__username, @__password
end

#download(remotefile, to: File.basename(remotefile)) ⇒ Object



53
54
55
56
57
# File 'lib/spectre/ftp.rb', line 53

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

#listObject



65
66
67
68
69
70
# File 'lib/spectre/ftp.rb', line 65

def list
  connect!
  file_list = @__session.list
  @__logger.info("Listing files in #{@__session.pwd}\n#{file_list}")
  file_list
end

#password(pass) ⇒ Object



26
27
28
# File 'lib/spectre/ftp.rb', line 26

def password pass
  @__password = pass
end

#upload(localfile, to: File.basename(localfile)) ⇒ Object



59
60
61
62
63
# File 'lib/spectre/ftp.rb', line 59

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

#username(user) ⇒ Object



22
23
24
# File 'lib/spectre/ftp.rb', line 22

def username user
  @__username = user
end