Class: Feed::TransmitSftp
- Inherits:
-
Object
- Object
- Feed::TransmitSftp
- Defined in:
- app/services/feed/transmit_sftp.rb
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
-
#build_options ⇒ Object
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.
-
#initialize(options = {}) ⇒ TransmitSftp
constructor
A new instance of TransmitSftp.
- #process(upload) ⇒ Object
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(={}) @logger = .delete(:logger) @hostname = [:hostname] @username = .delete(:username) @password = .delete(:password) @remote_directory = [:remote_directory] @options = end |
Instance Method Details
#build_options ⇒ Object
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 { 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..path file_name = upload. result_hsh[:file_name] = file_name result_hsh[:transmitted] = false #Default result_hsh[:status] = :error # Default Net::SFTP.start(@hostname,@username, ) 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.}" result_hsh[:message] = msg ErrorReporting.error(exc, msg) end Result.new(**result_hsh) end |