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

Make file operation work with CSV library #81

Merged
merged 7 commits into from
Dec 14, 2018

Conversation

jstuckey
Copy link
Contributor

@jstuckey jstuckey commented Nov 3, 2018

This is to fix the issue reported in #65.

The two missing pieces of the IO-like object needed by the CSV library are a limit argument for the #getsmethod and a #rewind method. I've attempted to match the interface described by Ruby's IO class.

The flexibility of arguments that #gets accepts makes for some messy conditional code. Hopefully the changes I've made are clear. According to the docs, the first argument can have the following behavior:

  • Separator string: read until separator is found
  • Zero-length string: read until end of paragraph
  • Integer: read N bytes
  • Nil: read until end of file

These can also be paired with a second limit argument.

I've added some tests and also tested with the following code:

require 'csv'

$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/lib"
require 'net/sftp'

Net::SFTP.start("my_hostname", "my_username", password: "my_password") do |sftp|
  stream = sftp.file.open("my_filepath.csv")
  CSV.new(stream).each { |row| puts row }
end

Ruby's IO class defines the #gets method as having an optional limit
argument. This can either be passed in as an integer for the first
argument or passed in addition to the delimiter. This makes argument
handling a little awkward unfortunately.
The previous commit added a limit argument to #gets as defined by Ruby's
IO class. This does the same for readline since it also accepts a limit
argument in the IO class. Fortunately we simply forward these params to
gets.
Add a sentence documenting the new limit argument. Took this sentence
from Ruby's IO documentation.

https://ruby-doc.org/core-2.5.1/IO.html#method-i-gets
A separator string could be provided that was not found in the stream.
In that scenario, we were ignoring the limit argument. This change
respects the limit argument even when the separator isn't found.
Ruby's IO documentation says that if nil is passed as the separator
argument, the IO object should read to the end of the file or until the
limit argument is reached.
This is another method an IO-like object should provide. It resets the
beginning state of the file.
@mfazekas mfazekas merged commit f5b9b4d into net-ssh:master Dec 14, 2018
jstuckey added a commit to jstuckey/net-sftp that referenced this pull request Dec 14, 2018
The #rewind method was added in two PRs:

net-ssh#81
net-ssh#83

I kept the latter one because it resuses existing code. I also grouped
the tests together.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants