class Euromail::SFTPService

Attributes

application[R]
current_mode[R]
customer[R]
host[R]
password[R]
username[R]

Public Class Methods

new(application, customer, host, username, password, net_ssh_options={}) click to toggle source
# File lib/euromail/sftp_service.rb, line 8
def initialize application, customer, host, username, password, net_ssh_options={}
  @application = application
  @customer = customer
  @host = host
  @username = username
  @password = password
  @net_ssh_options = net_ssh_options
  @current_mode = :production
end

Public Instance Methods

connect(&block) click to toggle source

Setup a connection to the sftp server. Operations can be defined in the block passed to this method: euromail.connect do |connection|

connection.upload('some data', '1')
connection.upload('more data', '2')
connection.remove('3')

end

# File lib/euromail/sftp_service.rb, line 54
def connect &block
  options = {password: password }.merge(@net_ssh_options)
  Net::SFTP.start(host, username, options) do |sftp|
    connection = Euromail::SFTPConnection.new(self, sftp)
    block.call(connection)
  end
end
development_mode!() click to toggle source
# File lib/euromail/sftp_service.rb, line 23
def development_mode!
  self.extend(Euromail::SFTPDevelopment::ServiceMethods)
  @current_mode = :development
end
filename(identifier) click to toggle source

Generate a filename based on the application, customer and some unique identifier. The identifier is not allowed to be blank since this risks previous files from being deleted.

# File lib/euromail/sftp_service.rb, line 64
def filename identifier
  raise "An identifier is required" if identifier.to_s == ''
  "./#{application}_#{customer}_#{identifier}.pdf"
end
remove!(identifier) click to toggle source

Attempt to remove the file for the given identifier.

# File lib/euromail/sftp_service.rb, line 42
def remove! identifier
  connect do |connection|
    connection.remove( identifier )
  end
end
test_mode!() click to toggle source
# File lib/euromail/sftp_service.rb, line 18
def test_mode!
  self.extend(Euromail::SFTPTest::ServiceMethods)
  @current_mode = :test
end
upload!(pdf_data, identifier) click to toggle source

Attempt to remove the file for the given identifier. If the upload fails or is aborted, this method attempts to remove the incomplete file from the remote server.

# File lib/euromail/sftp_service.rb, line 30
def upload! pdf_data, identifier
  begin
    connect do |connection|
      connection.upload(pdf_data, identifier)
    end
  rescue => e
    remove!(identifier)
    raise e
  end
end