Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO doesn't work with CSV #65

Closed
kkuchta opened this issue Aug 4, 2017 · 4 comments
Closed

IO doesn't work with CSV #65

kkuchta opened this issue Aug 4, 2017 · 4 comments

Comments

@kkuchta
Copy link

kkuchta commented Aug 4, 2017

io = sftp.file.open claims to produce an IO-like object. Unfortunately, it doesn't seem to be quite io-like enough for ruby's built-in CSV library. If I do CSV.new(io), I get an error:

ArgumentError: wrong number of arguments (given 2, expected 0..1)
from /Users/kevin/.rvm/gems/ruby-2.3.1/gems/net-sftp-2.1.2/lib/net/sftp/operations/file.rb:85:in `gets'

Tracing this through, it looks like CSV is ultimately trying to call io.get(separator, limit) (which is supported in the full io spec, but sftp's io-like object only allows one argument.

It's pretty simple to reproduce, but here's a test case anyway:

require 'net/sftp'
require 'csv'

Net::SFTP.start(
  '0.0.0.0',
  '...',
  password: '...',
  port: 2222
) do |sftp|
  filepath = './upload/test_file.csv'
  stream = sftp.file.open(filepath)
  csv = CSV.new(stream) # Errors here: wrong number of arguments (given 2, expected 0..1)
end

This isn't an urgent bug for me— I'll just ditch streams entirely and read the whole (small) file into memory before parsing it :). Just thought I'd log this in case someone else runs into the same issue (or in case I'm missing something and it's not a bug).

@kwerle
Copy link

kwerle commented Dec 14, 2017

Yup. I'll be doing the same workaround for the same issue.

@jstuckey
Copy link
Contributor

I ran into this issue as well.

A workaround using streams is to provide a row separator to CSV::new (assuming you know it in advance). This skips over the problematic code in CSV#init_separators that tries to automatically detect the separator using io.get(separator, limit).

stream = sftp.file.open(filepath)
csv = CSV.new(stream, row_sep: "\n")

@mfazekas
Copy link
Collaborator

mfazekas commented Oct 28, 2018

Thanks for the report! PR-s are welcome!

@jstuckey
Copy link
Contributor

jstuckey commented Nov 3, 2018

@mfazekas I'll give it a shot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants