pull from csv file

Ruby n00b here. I use a Redmine server. Redmine uses Ruby on Rails, so the server has that installed. Is it possible to make an HTML or other page that can be visited that, when visited, pulls information from another page (in this case it is a CSV file with a fixed URL) and prints it on the page for the visitor to see? I managed to accomplish this with PHP using fopen and fgetcsv, but I’d rather not have to install PHP and just use the existing Ruby.

The most naiive implementation might be something like this:

add a route:

# config/routes.rb
get '/show_csv' => 'show_csv#index'

a controller:

# app/controllers/show_csv_controller.rb
require 'open-uri'
require 'csv'
class ShowCsvController < ApplicationController
  def index
    source = ::URI.open('https://people.sc.fsu.edu/~jburkardt/data/csv/hurricanes.csv')
    @csv_data = CSV.parse(source.read, liberal_parsing: true)

and a view:

# app/views/show_csv/index.html.erb
  <% is_first_row = true
     @csv_data.each do |row| %>
      <% row.each do |cell|
           if is_first_row %>
          <th><%= cell %></th>
        <% else %>
          <td><%= cell.strip %></td>
        <% end %>
      <% end
         is_first_row = false %>
  <% end %>

and then navigate to: http://localhost:3000/show_csv

1 Like