Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cat import_experts.rake
#
#use FasterCSV for importing from our file
require 'fastercsv'
# We are working from an FMP export that should contain the following fields:
#
# Person id
# Caltech UID
# First name (with middle initial)
# Last name
# title
# Department
# campus phone exension
# email
# Expertise
# Expertise fields
# research web site
# personal web site
namespace :experts do
FILEDIR = "/tmp/mr"
EXPERTS_FILE = "#{FILEDIR}/ExpertsDataCleaned.csv"
EXPERTS_FILE_UNCLEAN = "#{FILEDIR}/ExpertsData.csv"
desc "Strip out some characters that cause the CVS parser to choke"
task :clean do
File.open(EXPERTS_FILE_UNCLEAN) do |file|
outfile = File.new(EXPERTS_FILE, "w")
while line = file.gets
line.gsub!("\324","'")
line.gsub!("\325","'")
line.gsub!("\322",'""')
line.gsub!("\323",'""')
line.gsub!("\321","--")
line.gsub!("\311","...")
# puts line
outfile.print line
end
end
end
desc "Load all experts"
task :import => :environment do
# before we do anything else, clean out the table
ActiveRecord::Base.connection.execute("DELETE FROM experts")
# now parse the data file and put rows in database
FasterCSV.foreach(EXPERTS_FILE) do |row|
# Process date_published
link = ""
link << "Research URL: "+row[10]+"<br />\n" unless row[10].nil?
link << "Personal URL: "+row[11]+"\n" unless row[11].nil?
expertise = ""
expertise << row[9]+"\n"
expertise << "<br />\n"+row[8]+"\n" unless row[8].nil?
Expert.create(:first_name => row[2],
:last_name => row[3],
:title => row[4],
:phone => row[6],
:email => row[7],
:expertise => expertise,
:link => link)
end
end
desc "Does all both steps in order"
task :all => :environment do
puts "First clean up the file"
`rake experts:clean`
puts "Now import in to import table"
`rake experts:import`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment