Class: MiniDeploy::ConnectFtp

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_deploy/connect_ftp.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, username, password, passive_mode = false) ⇒ ConnectFtp

Returns a new instance of ConnectFtp.



7
8
9
10
11
12
13
14
# File 'lib/mini_deploy/connect_ftp.rb', line 7

def initialize(host, username, password, passive_mode=false)
  @host            = host
  @username        = username
  @password        = password
  @passive_mode    = passive_mode
  @client          = Net::FTP.new(host)
  @connect_success = false
end

Instance Method Details

#connectObject



20
21
22
23
# File 'lib/mini_deploy/connect_ftp.rb', line 20

def connect
  @client.passive = true if @passive_mode
  @connect_success = @client. @username, @password
end

#connect_success?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/mini_deploy/connect_ftp.rb', line 16

def connect_success?
  @connect_success
end

#doneObject



68
69
70
# File 'lib/mini_deploy/connect_ftp.rb', line 68

def done
  @client.close
end

#download_to_temp_file(remote_path, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mini_deploy/connect_ftp.rb', line 25

def download_to_temp_file(remote_path, options={})
  temp_file = TemporaryFile.new
  happened_error = false

  @client.getbinaryfile(remote_path, temp_file.current_path)
rescue Net::FTPPermError
  happened_error = true
rescue StandardError
  happened_error = true
ensure
  temp_file.destroy if happened_error

  return happened_error ? nil : temp_file
end

#exists(remote_path) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/mini_deploy/connect_ftp.rb', line 48

def exists(remote_path)
  @client.size(remote_path)

  true
rescue Net::FTPPermError => e
  false
end

#list(arg = '*') ⇒ Object



64
65
66
# File 'lib/mini_deploy/connect_ftp.rb', line 64

def list(arg='*')
  @client.list(arg)
end

#remove(remote_path) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/mini_deploy/connect_ftp.rb', line 56

def remove(remote_path)
  @client.delete(remote_path)

  true
rescue Net::FTPPermError => e
  false
end

#upload(upload_local_source, remote_path, mode = :text) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/mini_deploy/connect_ftp.rb', line 40

def upload(upload_local_source, remote_path, mode=:text)
  if mode == :text
    @client.put(upload_local_source, remote_path)
  else
    @client.putbinaryfile(upload_local_source, remote_path)
  end
end