Class: Anyfetch::SFTP

Inherits:
Object
  • Object
show all
Defined in:
lib/anyfetch/sftp.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}) ⇒ SFTP

Returns a new instance of SFTP.



5
6
7
8
# File 'lib/anyfetch/sftp.rb', line 5

def initialize(uri, options = {})
  @uri = uri
  @options = options
end

Instance Method Details

#openObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/anyfetch/sftp.rb', line 10

def open
  user = @options.delete(:user) || @uri.user
  options = { password: @uri.password }.merge(@options)

  filename = ::File.basename(@uri.path)

  tempfile = Tempfile.new(filename)
  tempfile.binmode

  Net::SFTP.start(@uri.host, user, options) do |sftp|
    sftp.file.open(@uri.path, "r") do |file|
      while !file.eof? do
        tempfile << file.read(32 * 1024 * 1024)
      end
    end
  end

  tempfile.extend(OriginalFilename::Path)
end