Class: Remi::Extractor::SftpFile

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

Defined Under Namespace

Classes: FileNotFoundError, SortDesc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials:, remote_file:, remote_folder: '', local_folder: Settings.work_dir, port: nil, most_recent_only: false, group_by: nil, most_recent_by: :createtime, logger: Remi::Settings.logger) ⇒ SftpFile

Returns a new instance of SftpFile.



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

def initialize(credentials:, remote_file:, remote_folder: '', local_folder: Settings.work_dir, port: nil, most_recent_only: false, group_by: nil, most_recent_by: :createtime, logger: Remi::Settings.logger)
  @credentials = credentials
  @remote_file = remote_file
  @remote_folder = remote_folder
  @local_folder = local_folder
  @port = port || (credentials && credentials[:port]) || '22'
  @most_recent_only = most_recent_only
  @group_by = group_by
  @most_recent_by = most_recent_by
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



44
45
46
# File 'lib/remi/extractor/sftp_file.rb', line 44

def logger
  @logger
end

Instance Method Details

#all_entries(remote_folder = @remote_folder) ⇒ Object



61
62
63
# File 'lib/remi/extractor/sftp_file.rb', line 61

def all_entries(remote_folder = @remote_folder)
  @all_entries ||= connection { |sftp| sftp.dir.entries(File.join("/", remote_folder)) }
end

#download(entries_to_download, remote_folder: @remote_folder, local_folder: @local_folder, ntry: 3) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/remi/extractor/sftp_file.rb', line 99

def download(entries_to_download, remote_folder: @remote_folder, local_folder: @local_folder, ntry: 3)
  connection do |sftp|
    entries_to_download.map do |entry|
      local_file = File.join(local_folder, entry.name)
      @logger.info "Downloading #{entry.name} to #{local_file}"
      retry_download(ntry) { sftp.download!(File.join(remote_folder, entry.name), local_file) }
      local_file
    end
  end
end

#extractObject

Raises:



46
47
48
49
# File 'lib/remi/extractor/sftp_file.rb', line 46

def extract
  raise FileNotFoundError, "File not found: #{@remote_file}" if to_download.size == 0
  download(to_download)
end

#matching_entries(match_name = @remote_file) ⇒ Object



65
66
67
# File 'lib/remi/extractor/sftp_file.rb', line 65

def matching_entries(match_name = @remote_file)
  all_entries.select { |e| match_name.match e.name }
end

#most_recent_entry(entries = matching_entries) ⇒ Object



69
70
71
# File 'lib/remi/extractor/sftp_file.rb', line 69

def most_recent_entry(entries = matching_entries)
  entries.sort_by { |e| sort_files_by(e) }.reverse!.first
end

#most_recent_in_group(match_group = @group_by) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/remi/extractor/sftp_file.rb', line 81

def most_recent_in_group(match_group = @group_by)
  entries_with_group = matching_entries.map do |entry|
    match = entry.name.match(match_group)
    next unless match

    group = match.to_a[1..-1]
    { group: group, entry: entry }
  end.compact
  entries_with_group.sort_by! { |e| [e[:group], SortDesc.new(sort_files_by(e[:entry]))] }

  last_group = nil
  entries_with_group.map do |entry|
    next unless entry[:group] != last_group
    last_group = entry[:group]
    entry[:entry]
  end.compact
end

#sort_files_by(entry) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/remi/extractor/sftp_file.rb', line 73

def sort_files_by(entry)
  if @most_recent_by == :filename
    entry.name
  else
    entry.attributes.send(@most_recent_by)
  end
end

#to_downloadObject



51
52
53
54
55
56
57
58
59
# File 'lib/remi/extractor/sftp_file.rb', line 51

def to_download
  if @group_by
    most_recent_in_group
  elsif @most_recent_only
    Array(most_recent_entry(matching_entries))
  else
    matching_entries
  end
end