Class: Feed::TransmitFtp

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

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TransmitFtp

Returns a new instance of TransmitFtp.



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

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

Instance Method Details

#process(upload_or_file_path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/feed/transmit_ftp.rb', line 19

def process(upload_or_file_path)
  result_hsh = {}
  result_hsh[:status] = :error # Default
  result_hsh[:transmitted] = false #Default
  begin
    local_file_path = nil
    file_name = nil
    if upload_or_file_path.is_a?(Upload)
      local_file_path = upload_or_file_path.attachment.path
      file_name = upload_or_file_path.attachment_name
    else
      local_file_path = upload_or_file_path
      file_name = File.basename(local_file_path)
    end
    result_hsh[:file_name] = file_name
    Net::FTP.open(@hostname) do |ftp|
      ftp.passive = @ftp_passive
      @logger.info "Starting Transmission of #{file_name} to #{@hostname}/#{@remote_directory}"
      ftp.(@username, @password)
      @logger.info "Logged in with #{@username}"
      if @remote_directory.present?
        @logger.info "Changing to remote directory #{@remote_directory}"
        ftp.chdir(@remote_directory)
      end
      @logger.info "Transfer started"
      ftp.puttextfile(local_file_path, file_name)
      @logger.info "Transfer of #{local_file_path} complete"
      ftp.quit
    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