Class: FileTransfer::Ftp

Inherits:
Generic show all
Defined in:
lib/file_transfer/ftp.rb

Instance Attribute Summary

Attributes inherited from Generic

#host, #password, #port, #username

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Ftp

Returns a new instance of Ftp.



6
7
8
9
10
# File 'lib/file_transfer/ftp.rb', line 6

def initialize(options = {})
  super(options)
  @ftp = Net::FTP.new
  @ftp.passive = true
end

Instance Method Details

#closeObject



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

def close
  unless @ftp.closed?
    timeout(30) do
      @ftp.close
    end
  end
end

#download(from_path, to_path) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/file_transfer/ftp.rb', line 33

def download(from_path, to_path)
  from_path = split_path(from_path)
  connect if @ftp.closed?
  timeout(300) do
    @ftp.chdir from_path[:file_path]
    @ftp.getbinaryfile(to_path, from_path[:file_name])
    "#{from_path[:file_path]}/#{from_path[:file_name]}"
  end
end

#exist?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
# File 'lib/file_transfer/ftp.rb', line 53

def exist?(file_path)
  connect if @ftp.closed?
  timeout(60) do
    result = @ftp.list "#{file_path}"
    result && result.size > 0
  end
end

#list(dir, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/file_transfer/ftp.rb', line 12

def list(dir, options = {})
  connect if @ftp.closed?
  timeout(60) do
    @ftp.chdir dir
    result = @ftp.nlst
    if options.has_key? :file_type
      result = result.select { |file_name| file_name.end_with? options[:file_type] }
    end
    result
  end
end

#move(from_path, to_path) ⇒ Object



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

def move(from_path, to_path)
  from_path = split_path(from_path)
  to_path = split_path(to_path)
  connect if @ftp.closed?
  timeout(60) do
    @ftp.chdir from_path[:file_path]
    @ftp.rename from_path[:file_name], "#{to_path[:file_path]}/#{to_path[:file_name]}" if exist?("#{from_path[:file_name]}/#{from_path[:file_path]}")
  end
end

#to_sObject



69
70
71
# File 'lib/file_transfer/ftp.rb', line 69

def to_s
  "FtpClient {@host => #{@host},  @username => #{@username}, @password => ***}"
end

#upload(from_path, to_path) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/file_transfer/ftp.rb', line 24

def upload(from_path, to_path)
  to_path = split_path(to_path)
  connect if @ftp.closed?
  timeout(300) do
    @ftp.chdir to_path[:file_path]
    @ftp.putbinaryfile(from_path)
  end
end