Joseph McKenzie
Joseph McKenzie

Reputation: 87

I need to be able to read and write my csv file from github or dropbox

I am making a webpage for a company and I have it hosted on heroku, when I upload a file link to my csv on my local computer it works, but when using the deployed version it only works for a while as it stores the info in a temp file.It does not write to my csv on GitHub. M question is how to write to my GitHub using ruby and Sinatra . here is snippet of some of my code. any help would be great as this is due like now

get '/update_csv' do
csv = CSV.read("citywholesale.csv")

erb :update_csv, :locals =>{:csv => csv}
end

get '/upload' do
erb :upload, :locals =>{:message => "Please Enter all the required information."}
end

def newfilename(filename)
if filename.include?("dl=0") && filename.include?("dropbox")
filename.chomp!("dl=0")
filename << "raw=1"
end
filename
end

post '/upload' do

filename = params[:filename]
caption = params[:caption]
validity = params[:validity]
signtype = params[:signtype]
featured = params[:featured]

citywholesale = "citywholesale.csv"
citywholesale = File.open(citywholesale,'a')

 newfilename = newfilename(filename)
 citywholesale.write( newfilename + "," + caption + "," + validity + "," +        signtype + "," + featured + "\r")
 citywholesale.close
 redirect '/update_csv'
 end

post '/edit_upload' do
csv = CSV.read("citywholesale.csv")
csv_row = params[:edit].to_i
row = csv[csv_row]

erb :edit_upload, :locals =>{:id => csv_row, :image => row[0], 
                  :caption => row[1], :active => row[2], :signtype => row[3], :featured => row[4]}

end

Upvotes: 0

Views: 402

Answers (1)

user372495
user372495

Reputation: 710

Do you mean you have a repo on GitHub with a file you have to update? Let me first say that this is a bad idea, as GitHub is made to host version controlled code. A much better solution is to just store this CSV on that server or on something like Amazon's S3.

If, however, you are convinced that GitHub is the right way to go, the only way I would think you could do this is to use GitHub's Gists and use their API to do it. Here's the documentation:

https://developer.github.com/v3/gists/#edit-a-gist

Upvotes: 4

Related Questions