Class: Remi::Extractor::SftpFile

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

Constant Summary collapse

N_RETRY =
3

Instance Attribute Summary collapse

Attributes inherited from FileSystem

#group_by, #local_path, #logger, #most_recent_by, #most_recent_only, #pattern, #remote_path

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) ⇒ SftpFile

Returns a new instance of SftpFile.



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

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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/remi/extractor/sftp_file.rb', line 13

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



15
16
17
# File 'lib/remi/extractor/sftp_file.rb', line 15

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



16
17
18
# File 'lib/remi/extractor/sftp_file.rb', line 16

def port
  @port
end

#usernameObject (readonly)

Returns the value of attribute username.



14
15
16
# File 'lib/remi/extractor/sftp_file.rb', line 14

def username
  @username
end

Instance Method Details

#all_entriesObject

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



33
34
35
# File 'lib/remi/extractor/sftp_file.rb', line 33

def all_entries
  @all_entries ||= all_entries!
end

#all_entries!Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/remi/extractor/sftp_file.rb', line 37

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

#extractObject

Public: Called to extract files from the source filesystem.

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



21
22
23
24
25
26
27
28
29
30
# File 'lib/remi/extractor/sftp_file.rb', line 21

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