net-ssh / net-sftp

Pure Ruby implementation of an SFTP (protocols 1-6) client.
http://net-ssh.github.io/
MIT License
287 stars 131 forks source link

can't write CSV directly onto sftp.file.open #123

Open dan-corneanu opened 3 years ago

dan-corneanu commented 3 years ago

io = sftp.file.open claims to produce an IO-like object. However, trying to use it as output for CSV gives an error. Here is an example

require 'net/sftp'
require 'csv'

Net::SFTP.start(
      'localhost',
      'foo',
      password: 'pass',
    ) do |sftp|
      ftp_upload_path = 'upload/test_file.csv'
      sftp.file.open(ftp_upload_path, "w+") do |file|
        csv = CSV.new(file)
        csv << ["row", "of", "CSV", "data"]
      end
    end

This generate the following error

NoMethodError: undefined method `<<' for #<Net::SFTP::Operations::File:0x007f830419c718>
    from /Users/cdan/.rbenv/versions/2.3.0/lib/ruby/2.3.0/csv.rb:1692:in `<<'

The easy fix would be to probably implement Net::SFTP::Operations::File#<< in terms of Net::SFTP::Operations::File#write

This is similar.related to #65 .

dan-corneanu commented 3 years ago

I've aliased << to write in Net::SFTP::Operations::File See PR #124