Class: Crane::Ftp
- Inherits:
-
Object
- Object
- Crane::Ftp
- Defined in:
- lib/crane/ftp.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#connection_error ⇒ Object
Returns the value of attribute connection_error.
Instance Method Summary collapse
- #chdir(var) ⇒ Object
- #close ⇒ Object
- #connect(c = {}) ⇒ Object
-
#initialize(c = false) ⇒ Ftp
constructor
A new instance of Ftp.
-
#mkdir(dir) ⇒ Object
Accepts deep directories as parameter.
- #putbinaryfile(f, f2) ⇒ Object
- #pwd ⇒ Object
- #remote_dir_exists?(remote_dir = false) ⇒ Boolean
Constructor Details
#initialize(c = false) ⇒ Ftp
Returns a new instance of Ftp.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/crane/ftp.rb', line 10 def initialize c = false @connection = nil @existent_dirs = [] @nlst = {} unless c config = Config.load_config unless config.empty? c = {} c[:ftp] = config[:ftp] unless config[:ftp].empty? end end connect(c) if c self end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
8 9 10 |
# File 'lib/crane/ftp.rb', line 8 def connection @connection end |
#connection_error ⇒ Object
Returns the value of attribute connection_error.
8 9 10 |
# File 'lib/crane/ftp.rb', line 8 def connection_error @connection_error end |
Instance Method Details
#chdir(var) ⇒ Object
83 84 85 |
# File 'lib/crane/ftp.rb', line 83 def chdir var @connection.chdir var end |
#close ⇒ Object
75 76 77 |
# File 'lib/crane/ftp.rb', line 75 def close @connection.close end |
#connect(c = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/crane/ftp.rb', line 25 def connect c = {} port = c[:port] || 21 c = c[:ftp] if c.include? :ftp return false unless c.has_key? :host @connection = Net::FTP.new @connection_error = false @connection.passive = true begin Timeout.timeout(10) do @connection.connect c[:host], port end rescue Timeout::Error @connection_error = "Timeout on connection" rescue @connection_error = "Error on connection FTP" end begin Timeout.timeout(20) do @connection.login c[:username], c[:password] end rescue Timeout::Error @connection_error = "Timeout on authentication (20 seconds)" rescue Net::FTPPermError @connection_error = "Invalid username/password" rescue @connection_error = "Unknown error related to username/password" end return false if @connection_error true end |
#mkdir(dir) ⇒ Object
Accepts deep directories as parameter
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/crane/ftp.rb', line 88 def mkdir dir each_dir = dir.split("/") return false if dir.empty? last_dir = "" each_dir.each { |d| next if ["", ".", ".."].include? d slashed_d = "/" + d if d[0,1] != "/" current_dir = last_dir + slashed_d if @existent_dirs.include? current_dir last_dir << slashed_d next else @connection.chdir last_dir unless last_dir.empty? if @nlst.include? last_dir nlst = @nlst[last_dir] else nlst = @connection.nlst end @nlst[last_dir] = nlst if nlst.include? d @existent_dirs << current_dir last_dir << slashed_d next else @connection.mkdir d @connection.chdir current_dir last_dir << slashed_d @existent_dirs << current_dir end end } @connection.chdir pwd end |
#putbinaryfile(f, f2) ⇒ Object
79 80 81 |
# File 'lib/crane/ftp.rb', line 79 def putbinaryfile f, f2 @connection.putbinaryfile f, f2 end |
#pwd ⇒ Object
126 127 128 |
# File 'lib/crane/ftp.rb', line 126 def pwd @connection.pwd end |
#remote_dir_exists?(remote_dir = false) ⇒ Boolean
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/crane/ftp.rb', line 60 def remote_dir_exists? remote_dir = false return nil if Config.load_config.empty? connect(Config.CONFIG[:ftp]) if @connection.nil? begin Timeout.timeout(10) do @connection.chdir(remote_dir || Config.CONFIG[:ftp][:remote_root_dir]) end rescue return false end @connection.close true end |