Class: Ftpro

Inherits:
Net::FTP
  • Object
show all
Defined in:
lib/ftpro.rb

Instance Method Summary collapse

Instance Method Details

#directory?(dir) ⇒ Boolean

Check if dir is a directory type

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ftpro.rb', line 25

def directory?(dir)
  current_dir = pwd
  begin
    chdir(dir)
    return true
  rescue
    return false
  ensure
    chdir(current_dir) ## Go back former dir
  end
end

#exist?(dir) ⇒ Boolean

Check if dir exist

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/ftpro.rb', line 43

def exist?(dir)
  return true if !nlst(dir).empty? ## File or not empty directory
  ## Check if a empty directory
  directory?(dir)
end

#file?(file_path) ⇒ Boolean

Check if file_path is a file type

Returns:

  • (Boolean)


38
39
40
# File 'lib/ftpro.rb', line 38

def file?(file_path)
  nlst(file_path)[0] == file_path
end

#mkdir_p(new_dir) ⇒ Object

FileUtils function mirror



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

def mkdir_p(new_dir)
  dir_parts = new_dir.split("/")
  dir_parts.length.times do |i|
    current_dir = dir_parts[0..i].join("/")
    mkdir(current_dir) unless exist?(current_dir)
  end
  return new_dir if exist?(new_dir)
end

#nlst_a(dir = nil) ⇒ Object

Function like nlst and also show hide files



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

def nlst_a(dir = nil)
  cmd = "NLST -a"
  rule_out1, rule_out2 =
    dir.nil? ? [".", ".."] : ["/.", "/.."]
  if dir
    cmd = cmd + " " + dir
  end
  files = []
  retrlines(cmd) do |line|
    files.push(line) if !line.end_with?(rule_out1, rule_out2)
  end
  return files
end

#put_r(local_dir, remote_dir) ⇒ Object

Upload local directory to ftp server with local directory name



60
61
62
63
# File 'lib/ftpro.rb', line 60

def put_r(local_dir, remote_dir)
  return false if !File.directory?(local_dir)
  upload_dir(local_dir, remote_dir)
end

#rm_r(dir) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ftpro.rb', line 65

def rm_r(dir)
  dir_files = scan_ftp_dir(dir)
  ## empty fodler by the reverse of scan result
  (dir_files.length - 1).downto(0) do |f|
    if file?(dir_files[f])
      delete(dir_files[f])
    else
      rmdir(dir_files[f])
    end
  end
end