Skip to content

Instantly share code, notes, and snippets.

@jordanhudgens
Created June 3, 2020 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanhudgens/25e240ed127e0d135c5c9fe3b92be636 to your computer and use it in GitHub Desktop.
Save jordanhudgens/25e240ed127e0d135c5c9fe3b92be636 to your computer and use it in GitHub Desktop.
begin
Net::SFTP.start(HOSTNAME, USERNAME, :password => PASSWORD) do |sftp|
sftp.file.open("products.csv", "r") do |f|
puts "Opening file..."
# Old solution:
# data = SmarterCSV.process(f, value_converters: { sku: StringConverter })
# puts "Count: #{data.size}"
# product_iterator data
# New attempt(s)
CSV.foreach(f.read, headers: true, converters: [->(v) { String(v) rescue v }]) do |row|
puts "Row: #{row.inspect}"
end
CSV.foreach(f.read, headers: true) do |row|
puts "Row: #{row.inspect}"
end
CSV.foreach(f, headers: true) do |row|
puts "Row: #{row.inspect}"
end
end
# Another attempt
# file = sftp.file.open("/home/packtshophacker/product-files/products.csv", "r")
# puts file.gets => this outputs the headers for the top of the file
#
#
# CSV.foreach(f, headers: true) do |row|
# puts "Row: #{row.inspect}"
# end
#
# file.close
end
rescue Net::SFTP::StatusException => e
puts "Error: ", e.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment