diff --git a/app/models/usda_food.rb b/app/models/usda_food.rb index e9fb5db..824406e 100644 --- a/app/models/usda_food.rb +++ b/app/models/usda_food.rb @@ -13,4 +13,10 @@ class UsdaFood < ActiveRecord::Base end end + def density_best_guess + usda_food_weights.each do |w| + + end + end + end \ No newline at end of file diff --git a/app/models/usda_food_weight.rb b/app/models/usda_food_weight.rb index a57bd6a..fdd05d0 100644 --- a/app/models/usda_food_weight.rb +++ b/app/models/usda_food_weight.rb @@ -2,4 +2,22 @@ class UsdaFoodWeight < ActiveRecord::Base belongs_to :usda_food + def calculate_density + return nil if gram_weight.blank? || description.blank? || amount.blank? + + begin + value_unit = UnitConversion.parse(amount, description) + if value_unit.volume? + density_value = gram_weight.to_d / value_unit.raw_value + density_units = "g/#{value_unit.unit.unit}" + density = UnitConversion.parse(density_value, density_units) + return density.convert('oz/cup') + else + return nil + end + rescue UnitConversion::UnparseableUnitError + return nil + end + end + end \ No newline at end of file diff --git a/lib/usda_importer.rb b/lib/usda_importer.rb index dc274ec..7822f6c 100644 --- a/lib/usda_importer.rb +++ b/lib/usda_importer.rb @@ -167,11 +167,11 @@ class UsdaImporter file_info = FILES[name] obj = food - if file_info[:map_into] - obj = food.send(file_info[:map_into]).build - end - rows.each do |row| + if file_info[:map_into] + obj = food.send(file_info[:map_into]).build + end + file_info[:map].each do |db, col| obj.send("#{db}=", row[col]) end @@ -190,47 +190,6 @@ class UsdaImporter sorted_files.each { |k, v| `rm #{v}` } end - # UsdaFood.delete_all - # - # food_data_lookup = {} - # - # CSV.open(File.join(@directory, 'FOOD_DES.txt'), 'r:iso-8859-1:utf-8', csv_options(FOOD_DATA_COLUMNS)) do |csv| - # csv.each do |row| - # food_data_lookup[row['NDB_No']] = row.to_h - # end - # end - # - # CSV.open(File.join(@directory, 'ABBREV.txt'), 'r:iso-8859-1:utf-8', csv_options(ABBREV_COLUMNS)) do |csv| - # csv.each_slice(500) do |slice| - # UsdaFood.transaction do - # - # attributes = slice.map do |row| - # attrs = Hash[ABBREV_COLUMN_MAP.map { |db, col| [db, row[col]] }] - # lookup = food_data_lookup[attrs[:ndbn]] - # if lookup - # extra_attrs = Hash[FOOD_DATA_COLUMN_MAP.map { |db, col| [db, lookup[col]] }] - # attrs.merge!(extra_attrs) - # end - # attrs - # end - # - # UsdaFood.create(attributes) - # - # end - # end - # end - # - # usda_items = Hash[UsdaFood.where(ndbn: Ingredient.select(:ndbn)).map { |uf| [uf.ndbn, uf] }] - # - # Ingredient.where('ndbn IS NOT NULL').each do |i| - # item = usda_items[i.ndbn] - # - # if item - # i.set_usda_food(item) - # i.save - # end - # end - end def build_enumerator(opened_files) diff --git a/spec/factories/usda_food_weights.rb b/spec/factories/usda_food_weights.rb new file mode 100644 index 0000000..229c420 --- /dev/null +++ b/spec/factories/usda_food_weights.rb @@ -0,0 +1,10 @@ +FactoryGirl.define do + + factory :usda_food_weight do + amount 1 + description 'cup' + gram_weight 350 + usda_food + end + +end diff --git a/spec/lib/usda_importer_spec.rb b/spec/lib/usda_importer_spec.rb index 782b5cf..d76844f 100644 --- a/spec/lib/usda_importer_spec.rb +++ b/spec/lib/usda_importer_spec.rb @@ -1,5 +1,16 @@ require 'rails_helper' +require 'usda_importer' RSpec.describe UsdaImporter do + it 'imports' do + i = UsdaImporter.new(Rails.root.join('spec', 'test_data')) + i.import + + expect(UsdaFood.count).to eq 2 + butter = UsdaFood.where(ndbn: '01001').first + expect(butter).not_to be_nil + expect(butter.usda_food_weights.count).to eq 4 + end + end \ No newline at end of file diff --git a/spec/models/usda_food_weight_spec.rb b/spec/models/usda_food_weight_spec.rb new file mode 100644 index 0000000..0b0728f --- /dev/null +++ b/spec/models/usda_food_weight_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe UsdaFood do + + describe '#calculate_density' do + + it 'returns nil for invalid values' do + expect(build(:usda_food_weight, description: nil).calculate_density).to be_nil + expect(build(:usda_food_weight, amount: nil).calculate_density).to be_nil + expect(build(:usda_food_weight, gram_weight: nil).calculate_density).to be_nil + expect(build(:usda_food_weight, description: 'cats').calculate_density).to be_nil + end + + it 'returns a density in oz / cup' do + expect(build(:usda_food_weight).calculate_density.raw_value).to be_within(0.1).of(12.3) + expect(build(:usda_food_weight).calculate_density.unit.to_s).to eq 'ounce/cup' + end + + end + +end \ No newline at end of file