Class: Spectre::FTP::FTPConnection
- Inherits:
-
Object
- Object
- Spectre::FTP::FTPConnection
- Includes:
- Delegate
- Defined in:
- lib/spectre/ftp.rb
Instance Method Summary collapse
- #can_connect? ⇒ Boolean
- #close ⇒ Object
- #connect! ⇒ Object
- #download(remotefile, to: File.basename(remotefile)) ⇒ Object
-
#initialize(host, username, password, opts, logger) ⇒ FTPConnection
constructor
A new instance of FTPConnection.
- #list ⇒ Object
- #password(pass) ⇒ Object
- #upload(localfile, to: File.basename(localfile)) ⇒ Object
- #username(user) ⇒ Object
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
43 44 45 46 47 48 |
# File 'lib/spectre/ftp.rb', line 43 def can_connect? connect! true rescue StandardError false end |
#close ⇒ Object
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.login @__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. to}'" ) @__session.getbinaryfile(remotefile, to) end |
#list ⇒ Object
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. 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 |