Class: Remi::Extractor::SftpFileNew

Inherits:
FileSystem show all
Defined in:
lib/remi/extractor/sftp_file_new.rb

Constant Summary collapse

N_RETRY =
3

Instance Attribute Summary

Attributes inherited from FileSystem

#logger

Instance Method Summary collapse

Methods inherited from FileSystem

#entries, #matching_entries, #most_recent_matching_entry, #most_recent_matching_entry_in_group

Constructor Details

#initialize(*args, **kargs) ⇒ SftpFileNew

Returns a new instance of SftpFileNew.



8
9
10
11
# File 'lib/remi/extractor/sftp_file_new.rb', line 8

def initialize(*args, **kargs)
  super
  init_sftp_file(*args, **kargs)
end

Instance Method Details

#all_entriesObject

Public: Returns an array of all FileSystemEntry instances that are in the remote_path.



28
29
30
# File 'lib/remi/extractor/sftp_file_new.rb', line 28

def all_entries
  @all_entries ||= all_entries!
end

#all_entries!Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/remi/extractor/sftp_file_new.rb', line 32

def all_entries!
  sftp_entries = connection { |sftp| sftp.dir.entries(@remote_path.dirname) }
  sftp_entries.map do |entry|
    # Early versions of the protocol don't support create time, fake it with modified time?
    FileSystemEntry.new(
      name: File.join(@remote_path.dirname, entry.name),
      create_time: entry.respond_to?(:createtime) ? entry.createtime : entry.mtime,
      modified_time: entry.mtime
    )
  end
end

#extractObject

Public: Called to extract files from the source filesystem.

Returns an array with containing the paths to all files extracted.



16
17
18
19
20
21
22
23
24
25
# File 'lib/remi/extractor/sftp_file_new.rb', line 16

def extract
  connection do |sftp|
    entries.map do |entry|
      local_file = File.join(@local_path, entry.name)
      @logger.info "Downloading #{entry.name} to #{local_file}"
      retry_download { sftp.download!(File.join(@remote_path, entry.name), local_file) }
      local_file
    end
  end
end