Class: Crackup::Driver::FtpDriver

Inherits:
Object
  • Object
show all
Includes:
Crackup::Driver
Defined in:
lib/crackup/drivers/ftp.rb

Overview

FTP storage driver for Crackup.

Author

Ryan Grove ([email protected])

Copyright

Copyright © 2006 Ryan Grove. All rights reserved.

License

New BSD License (opensource.org/licenses/bsd-license.php)

Instance Attribute Summary

Attributes included from Crackup::Driver

#url

Instance Method Summary collapse

Methods included from Crackup::Driver

get_driver, #get_path

Constructor Details

#initialize(url) ⇒ FtpDriver

Connects to the FTP server specified in url.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/crackup/drivers/ftp.rb', line 17

def initialize(url)
  super(url)
  
  # Parse URL.

  begin
    uri = URI::parse(url)
  rescue => e
    raise Crackup::StorageError, "Invalid URL: #{url}: #{e}"
  end
  
  @ftp = Net::FTP.new
  @ftp.passive = true

  begin
    @ftp.connect(uri.host, uri.port.nil? ? 21 : uri.port)
  rescue => e
    raise Crackup::StorageError, "FTP connect failed: #{e}"
  end
  
  begin
    @ftp.(uri.user.nil? ? 'anonymous' : uri.user, uri.password)
  rescue => e
    raise Crackup::StorageError, "FTP login failed: #{e}"
  end
end

Instance Method Details

#delete(url) ⇒ Object

Deletes the file at the specified url.



44
45
46
47
48
49
50
# File 'lib/crackup/drivers/ftp.rb', line 44

def delete(url)
  @ftp.delete(get_path(url))
  return true
  
rescue => e
  raise Crackup::StorageError, "Unable to delete #{url}: #{e}"
end

#get(url, local_filename) ⇒ Object

Downloads the file at url to local_filename.



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

def get(url, local_filename)
  @ftp.getbinaryfile(get_path(url), local_filename)
  return true

rescue => e
  raise Crackup::StorageError, "Unable to download #{url}: #{e}"
end

#put(url, local_filename) ⇒ Object

Uploads the file at local_filename to url.



62
63
64
65
66
67
68
# File 'lib/crackup/drivers/ftp.rb', line 62

def put(url, local_filename)
  @ftp.putbinaryfile(local_filename, get_path(url))
  return true
  
rescue => e
  raise Crackup::StorageError, "Unable to upload #{url}: #{e}"
end