Class: FtpService

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#closeObject

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, options = {})
  response = download_and_read_response(remote_path, options)
  if options[:gpg_passphrase]
    RubyGpg.decrypt_string(response, options[: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, options = {})
  request = TempfileHelper.write(request, 'request')
  remote_temp_path = remote_path + ".tmp"
  if options[:gpg_recipient]
    encrypt(request, options[: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