Class: FileTransfer::Ftp

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

Instance Attribute Summary

Attributes inherited from Generic

#host, #password, #port, #timeout_seconds, #username

Instance Method Summary collapse

Methods inherited from Generic

#to_s

Constructor Details

#initialize(options = {}) ⇒ Ftp

Returns a new instance of Ftp.



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

def initialize(options = {})
  super(options)
end

Instance Method Details

#closeObject



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

def close
  if @ftp && !@ftp.closed?
    timeout(30) do
      @ftp.close
    end
  end
end

#download(from_path, to_path) ⇒ Object



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

def download(from_path, to_path)
  from_path = split_path(from_path)
  connect if @ftp.closed?
  timeout(timeout_seconds) 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)


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

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



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

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



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

def move(from_path, to_path)
  from_path = split_path(from_path)
  to_path = split_path(to_path)
  connect if @ftp.closed?
  timeout(timeout_seconds) 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

#upload(from_path, to_path) ⇒ Object



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

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