Class: Euromail::SFTPService

Inherits:
Object
  • Object
show all
Defined in:
lib/euromail/sftp_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application, customer, host, username, password, net_ssh_options = {}) ⇒ SFTPService

Returns a new instance of SFTPService.



8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



6
7
8
# File 'lib/euromail/sftp_service.rb', line 6

def application
  @application
end

#current_modeObject (readonly)

Returns the value of attribute current_mode.



6
7
8
# File 'lib/euromail/sftp_service.rb', line 6

def current_mode
  @current_mode
end

#customerObject (readonly)

Returns the value of attribute customer.



6
7
8
# File 'lib/euromail/sftp_service.rb', line 6

def customer
  @customer
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/euromail/sftp_service.rb', line 6

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



6
7
8
# File 'lib/euromail/sftp_service.rb', line 6

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



6
7
8
# File 'lib/euromail/sftp_service.rb', line 6

def username
  @username
end

Instance Method Details

#connect(&block) ⇒ Object

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



54
55
56
57
58
59
60
# 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!Object



23
24
25
26
# File 'lib/euromail/sftp_service.rb', line 23

def development_mode!
  self.extend(Euromail::SFTPDevelopment::ServiceMethods)
  @current_mode = :development
end

#filename(identifier) ⇒ Object

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.



64
65
66
67
# 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) ⇒ Object

Attempt to remove the file for the given identifier.



42
43
44
45
46
# File 'lib/euromail/sftp_service.rb', line 42

def remove! identifier
  connect do |connection|
    connection.remove( identifier )
  end
end

#test_mode!Object



18
19
20
21
# File 'lib/euromail/sftp_service.rb', line 18

def test_mode!
  self.extend(Euromail::SFTPTest::ServiceMethods)
  @current_mode = :test
end

#upload!(pdf_data, identifier) ⇒ Object

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.



30
31
32
33
34
35
36
37
38
39
# 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