Class: Feed::TransmitSftp
- Inherits:
-
Object
- Object
- Feed::TransmitSftp
- Defined in:
- app/services/feed/transmit_sftp.rb
Overview
Service object: transmit sftp.
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.
12 13 14 15 16 17 18 19 |
# File 'app/services/feed/transmit_sftp.rb', line 12 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
24 25 26 27 28 29 30 |
# File 'app/services/feed/transmit_sftp.rb', line 24 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/services/feed/transmit_sftp.rb', line 32 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 => e msg = "Failed to transmit. #{@options.inspect} - #{e.}" result_hsh[:message] = msg ErrorReporting.error(e, msg) end Result.new(**result_hsh) end |