What I’m looking to do is take all my product ID’s in my sampleid text doc and have my script rotate through each one creating the product URL with that ID. I also want to have the id and the full url output into a csv file but my code below seems to only produce the last id in my doc… I’m assuming that its not moving to the next row for on each_line do
#!/usr/bin/ruby
# ProductID script
puts "Enter current token"
auth_token = gets.chomp
File.open("sampleid.txt", "r").each_line do |line|
productNumber = line
restcall = "mywebsite.com&token=" + auth_token + "&something.com/" + productNumber + ""
require 'csv'
CSV.open("/Users/Documents/scripts/ruby/rb_output.csv", "wb") do |csv|
csv << ["Product ID", "Full Call"]
csv << [productNumber, restcall]
end
end
You’re opening the file freshly for every line, which means you’re re-writing the file for every iteration of .each_line. Try this: