2016-01-28 14:19:51 -06:00
|
|
|
namespace :usda do
|
|
|
|
|
2018-09-14 19:32:49 -05:00
|
|
|
task :list_branded_nutrients do
|
|
|
|
require 'csv'
|
|
|
|
|
|
|
|
NutrientCollector = Struct.new(:name, :total_count, :unit_frequencies) do
|
|
|
|
def record(name, unit)
|
|
|
|
self.name = name
|
|
|
|
self.total_count = (self.total_count || 0) + 1
|
|
|
|
self.unit_frequencies ||= Hash.new { |h, k| h[k] = 0 }
|
|
|
|
self.unit_frequencies[unit] += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
totals = Hash.new { |h, k| h[k] = NutrientCollector.new() }
|
|
|
|
|
|
|
|
f = CSV.open(Rails.root.join('vendor', 'data', 'usda_branded', 'Nutrients.csv'), 'r:iso-8859-1:utf-8')
|
|
|
|
f.each do |row|
|
|
|
|
code = row[1]
|
|
|
|
name = row[2]
|
|
|
|
unit = row[5]
|
|
|
|
totals[code].record(name, unit)
|
|
|
|
end
|
|
|
|
|
|
|
|
totals.each do |k, v|
|
|
|
|
puts "#{k}, #{v.name}: #{v.total_count} #{v.unit_frequencies.map { |k,v| "#{k}: #{v}" }.join('; ')}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-01-28 14:19:51 -06:00
|
|
|
desc 'Empties usda_foods table, imports all data, and then updates any linked ingredients'
|
|
|
|
task import: :environment do
|
2016-01-30 21:51:32 -06:00
|
|
|
require 'usda_importer'
|
2016-01-28 18:18:45 -06:00
|
|
|
importer = UsdaImporter.new(Rails.root.join('vendor', 'data', 'usda'))
|
2016-01-28 14:19:51 -06:00
|
|
|
importer.import
|
|
|
|
end
|
|
|
|
end
|