Skip to content

Instantly share code, notes, and snippets.

@avescodes
Created May 17, 2009 03:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save avescodes/112904 to your computer and use it in GitHub Desktop.
Save avescodes/112904 to your computer and use it in GitHub Desktop.
require 'fastercsv'
task 'usda:import' => ['usda:import:food_items','usda:import:food_groups','usda:import:nutrients','usda:import:food_item_nutrients', 'usda:import:weights']
namespace :usda do
namespace :import do
task :food_items => :environment do
puts "Importing Food Items..."
fname = File.join(RAILS_ROOT,"/db/usda_sr21/FOOD_DES.txt")
csv = FasterCSV.new(File.read(fname),:col_sep => '^', :quote_char => '~')
FoodItem.transaction do
csv.each do |row|
id = row[0]
data = {
:food_group_id => row[1],
:name => row[2],
:common_names => row[4],
:manufacturer_name => row[5] }
if(f = FoodItem.find_by_id(id))
f.update_attributes!(data)
else
f=FoodItem.new(data)
f.id=id
f.save!
end
end
end
end
task :food_groups => :environment do
puts "Importing Food Groups..."
fname = File.join(RAILS_ROOT,"/db/usda_sr21/FD_GROUP.txt")
csv = FasterCSV.new(File.read(fname),:col_sep => '^', :quote_char => '~')
FoodGroup.transaction do
csv.each do |row|
id = row[0]
data = { :name => row[1] }
if(f = FoodGroup.find_by_id(id))
f.update_attributes!(data)
else
f=FoodGroup.new(data)
f.id=id
f.save!
end
end
end
end
task :nutrients => :environment do
puts "Importing Nutrients"
fname = File.join(RAILS_ROOT,"/db/usda_sr21/NUTR_DEF.txt")
csv = FasterCSV.new(File.read(fname),:col_sep => '^', :quote_char => '~')
Nutrient.transaction do
csv.each do |row|
id = row[0]
data = {:unit => row[1],
:tagname => row[2],
:description => row[3] }
if(f = Nutrient.find_by_id(id))
f.update_attributes!(data)
else
f=Nutrient.new(data)
f.id=id
f.save!
end
end
end
end
task :food_item_nutrients => :environment do
puts "Importing Food Item Nutrients"
fname = File.join(RAILS_ROOT,"/db/usda_sr21/NUT_DATA.txt")
csv = FasterCSV.new(File.read(fname),:col_sep => '^', :quote_char => '~')
progress = Progress.new(`wc -l #{fname}`.scan(/\d+/)[0].to_i)
FoodItemNutrient.transaction do
csv.each do |row|
progress.tick
id = row[0]
data = {:food_item_id => row[0],
:nutrient_id => row[1],
:amount => row[2]}
if(f = FoodItemNutrient.find_by_food_item_id_and_nutrient_id(id,row[1]))
f.update_attributes!(data)
else
f=FoodItemNutrient.new(data)
f.save!
end
end
end
end
task :weights => :environment do
puts "Importing Weights"
fname = File.join(RAILS_ROOT,"/db/usda_sr21/WEIGHT.txt")
csv = FasterCSV.new(File.read(fname),:col_sep => '^', :quote_char => '~')
Weight.transaction do
csv.each do |row|
# id = row[0]
data = {:food_item_id => row[0],
:amount => row[2],
:description => row[3],
:grams => row[4]}
# if(f = Weight.find_by_food_item_id(id))
# f.update_attributes!(data)
# else
f=Weight.new(data)
f.save!
# end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment