Class: Spectre::FTP::FTPConnection

Inherits:
Object
  • Object
show all
Includes:
Delegate
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.



11
12
13
14
15
16
17
18
19
# File 'lib/spectre/ftp.rb', line 11

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)


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

def can_connect?
  connect!
  true
rescue StandardError
  false
end

#closeObject



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

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

  @__session.close
end

#connect!Object



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

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



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

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



69
70
71
72
73
74
# File 'lib/spectre/ftp.rb', line 69

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

#password(pass) ⇒ Object



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

def password pass
  @__password = pass
end

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



60
61
62
63
64
65
66
67
# File 'lib/spectre/ftp.rb', line 60

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



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

def username user
  @__username = user
end