Class: FtpService
- Inherits:
-
Object
- Object
- FtpService
- Includes:
- Encryption
- Defined in:
- lib/ftp_service.rb,
lib/ftp_service/encryption.rb
Overview
Class for dealing with a service that acts like a web service, except over FTP. Meaning an xml request is uploaded as a file, and the response xml is downloaded as another file.
Typical usage:
FtpService.open('host', 'user', 'pass') do |service|
path = '/the/remote/path'
service.write_request("#{path}/request.xml", '<foo>bar</foo>')
response = service.read_response("#{path}/response.xml")
end
or (the sucky way):
path = '/the/remote/path'
service = FtpService.new('host', 'user', 'pass')
service.write_request("#{path}/request.xml", '<foo>bar</foo>')
response = service.read_response("#{path}/response.xml")
service.close
Defined Under Namespace
Modules: Encryption
Class Method Summary collapse
-
.open(host, user, pass) ⇒ Object
Open a connection and pass an FtpService instance to the block.
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection to the FTP server.
-
#initialize(host, user, pass) ⇒ FtpService
constructor
Open a connection to the FTP server and return an FtpService object.
-
#read_response(remote_path, options = {}) ⇒ Object
Download the file at
remote_path
from the FTP server to a local temp file and return its contents. -
#write_request(request, remote_path, options = {}) ⇒ Object
Write
request
to a local temp file and upload it toremote_path
on the FTP server.
Constructor Details
#initialize(host, user, pass) ⇒ FtpService
Open a connection to the FTP server and return an FtpService object. The object must be closed explicitely. See FtpServier#open for a better way to do this that will automatically close the connection, even if an exception is raised.
31 32 33 |
# File 'lib/ftp_service.rb', line 31 def initialize(host, user, pass) @ftp = Net::FTP.open(host, user, pass) end |
Class Method Details
.open(host, user, pass) ⇒ Object
Open a connection and pass an FtpService instance to the block. The instance will be closed when the block finishes, or when an exception is raised.
38 39 40 41 42 43 44 45 |
# File 'lib/ftp_service.rb', line 38 def self.open(host, user, pass) instance = new(host, user, pass) begin yield(instance) ensure instance.close end end |
Instance Method Details
#close ⇒ Object
Close the connection to the FTP server.
94 95 96 |
# File 'lib/ftp_service.rb', line 94 def close @ftp.close end |
#read_response(remote_path, options = {}) ⇒ Object
Download the file at remote_path
from the FTP server to a local temp file and return its contents. If remote_path
doesn’t exist, keep trying for 2 minutes before raising a Timeout::Error.
response = ftp_service.read_response('/remote/path.xml')
If you expect the response to be encrypted for you with gpg:
response = ftp_service.read_response('/remote/path.xml.gpg', :gpg_passphrase => "my_passphrase")
You must have gpg
installed and have the necessarry private key. This uses the ruby_gpg
gem. To configure the gpg settings, see http://rdoc.info/projects/blaix/ruby_gpg.
84 85 86 87 88 89 90 91 |
# File 'lib/ftp_service.rb', line 84 def read_response(remote_path, = {}) response = download_and_read_response(remote_path, ) if [:gpg_passphrase] RubyGpg.decrypt_string(response, [:gpg_passphrase]) else response end end |
#write_request(request, remote_path, options = {}) ⇒ Object
Write request
to a local temp file and upload it to remote_path
on the FTP server.
ftp_service.write_request('<foo>bar</foo>', '/remote/path.xml')
You can encrypt the request using GPG:
ftp_service.write_request('<secret>stuff</secret>', '/remote/path.xml.gpg', :gpg_recipient => '[email protected]')
You must have gpg
installed and have a public key available for the intended recipient. This uses the ruby_gpg
gem. To configure the gpg settings, see http://rdoc.info/projects/blaix/ruby_gpg.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ftp_service.rb', line 59 def write_request(request, remote_path, = {}) request = TempfileHelper.write(request, 'request') remote_temp_path = remote_path + ".tmp" if [:gpg_recipient] encrypt(request, [:gpg_recipient]) @ftp.putbinaryfile(request.path, remote_temp_path) else @ftp.puttextfile(request.path, remote_temp_path) end @ftp.rename(remote_temp_path, remote_path) end |