views:

370

answers:

2

Two questions:

  1. How can I import a file from a web address, without a form?

    Example: Organisation.import(:from => 'http://wufoo.com/report.csv')

  2. How can I use xml builder without pulling from the db?

More Info

My company uses wufoo for web forms. The data from wufoo is exported as csv files. To get the data into my company's cms, it needs to be formatted as xml. I don't need to store any of the data, aside from the url to the csv file. I thought this might work well as a simple rails app.

+1  A: 

Use open-uri (http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/) to fetch the file, and ruby's csv library to parse it. Or, use csv-mapper which is nice and simple (http://csv-mapper.rubyforge.org/).

insane.dreamer
A: 

Here is a way:

require 'rio'
require 'fastercsv'

url = 'http://remote-url.com/file.csv'
people = FasterCSV.parse(rio(url).read)

xml = ''
1.upto(people.size-1) do |row_idx|
  xml << "  <record>\n"
  people[0].each_with_index do |column, col_idx|
    xml << "    <#{column.parameterize}>#{people[row_idx][col_idx]}</#{column.parameterize}>\n"
  end
  xml << "  </record>\n"
end
Jeffrey