If you're looking for an elegant way to generate CSV files from your index views (or search views or anything else for that matter) you should look no further than this post on StackOverflow by rwc9u.
The gist is to add a format line in the respond_to section of the index method in your controller that caters to CSV. Then create an index.csv.erb file where you can generate the actual CSV using inline ruby like you would for e.g. your index.html.erb view. Actually retrieving the data in CSV format from your application involves adding a ".csv" extension to the end of your normal index path e.g. /test/widgets becomes /test/widgets.csv to return CSV.
Keep in mind that you'll need to restart your rails server if you create the initializer file that he suggests.
Monday, August 30, 2010
Friday, August 27, 2010
Exception handling for Net::SSH
I'm writing a bit of Ruby automation code which requires me to connect to multiple servers using ssh and gather specific information from them using a loop like so:
However, my test script kept dying at various points due to issues with the ssh connection to specific servers.
Naturally I thought of wrapping the Net::SSH.start call in a begin/rescue/end but couldn't for the life of me find any information about the exceptions that the start method could throw. Finally after a bit of digging on google I came across this page which details them rather handily :-) In short, here's how I have it wrapped now:
This works fabulously since I don't really need to handle the exceptions but would like to know about them.
hostList.each do |h| Net::SSH.start(h, "user", :password => "password", :timeout => 10) do |ssh| end end
However, my test script kept dying at various points due to issues with the ssh connection to specific servers.
Naturally I thought of wrapping the Net::SSH.start call in a begin/rescue/end but couldn't for the life of me find any information about the exceptions that the start method could throw. Finally after a bit of digging on google I came across this page which details them rather handily :-) In short, here's how I have it wrapped now:
hostList.each do |h| begin Net::SSH.start(h, "user", :password => "password", :timeout => 10) do |ssh| end rescue Timeout::Error puts " Timed out" rescue Errno::EHOSTUNREACH puts " Host unreachable" rescue Errno::ECONNREFUSED puts " Connection refused" rescue Net::SSH::AuthenticationFailed puts " Authentication failure" end end
This works fabulously since I don't really need to handle the exceptions but would like to know about them.
Subscribe to:
Posts (Atom)