Class: Feed::TransmitSftp

Inherits:
Object
  • Object
show all
Defined in:
app/services/feed/transmit_sftp.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TransmitSftp

Returns a new instance of TransmitSftp.



10
11
12
13
14
15
16
17
# File 'app/services/feed/transmit_sftp.rb', line 10

def initialize(options={})
  @logger = options.delete(:logger)
  @hostname = options[:hostname]
  @username = options.delete(:username)
  @password = options.delete(:password)
  @remote_directory = options[:remote_directory]
  @options = options
end

Instance Method Details

#build_optionsObject

Here we build an option hash for the SSH connection, any of these value
can be added
http://net-ssh.github.io/net-ssh/Net/SSH.html



22
23
24
25
26
27
28
# File 'app/services/feed/transmit_sftp.rb', line 22

def build_options
  {
    password: @password,
    auth_methods: ['password'], #Use password explicitly, don't do public/private key
    config: false #Don't read local options
  }
end

#process(upload) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/feed/transmit_sftp.rb', line 30

def process(upload)
  result_hsh = {}
  begin
    local_file_path = upload.attachment.path
    file_name = upload.attachment_name
    result_hsh[:file_name] = file_name
    result_hsh[:transmitted] = false #Default
    result_hsh[:status] = :error # Default
    Net::SFTP.start(@hostname,@username, build_options) do |sftp|
      @logger.info "Starting Transmission of #{file_name} to #{@hostname}/#{@remote_directory}"
      remote_file_path = [@remote_directory,file_name].compact.join('/')
      @logger.info "Transfer started from #{local_file_path} to #{remote_file_path}"
      sftp.upload!(local_file_path, remote_file_path)
      @logger.info "Transfer of #{local_file_path} complete"
    end
    result_hsh[:transmitted] = true
    result_hsh[:status] = :ok
  rescue StandardError => exc
    msg = "Failed to transmit. #{@options.inspect} - #{exc.message}"
    result_hsh[:message] = msg
    ErrorReporting.error(exc, msg)
  end
  Result.new(**result_hsh)
end