usda-nutrient-database 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rspec +2 -0
  4. data/Guardfile +9 -0
  5. data/db/migrate/1_create_usda_food_groups.rb +8 -0
  6. data/db/migrate/2_create_usda_foods.rb +19 -0
  7. data/db/migrate/3_create_usda_foods_nutrients.rb +28 -0
  8. data/db/migrate/4_create_usda_nutrients.rb +12 -0
  9. data/lib/tasks/usda_nutrient_database.rake +6 -0
  10. data/lib/usda-nutrient-database.rb +35 -0
  11. data/lib/usda_nutrient_database/configuration.rb +9 -0
  12. data/lib/usda_nutrient_database/food.rb +15 -0
  13. data/lib/usda_nutrient_database/food_group.rb +12 -0
  14. data/lib/usda_nutrient_database/foods_nutrient.rb +18 -0
  15. data/lib/usda_nutrient_database/import/base.rb +17 -0
  16. data/lib/usda_nutrient_database/import/downloader.rb +59 -0
  17. data/lib/usda_nutrient_database/import/food_groups.rb +19 -0
  18. data/lib/usda_nutrient_database/import/foods.rb +30 -0
  19. data/lib/usda_nutrient_database/import/foods_nutrients.rb +34 -0
  20. data/lib/usda_nutrient_database/import/nutrients.rb +20 -0
  21. data/lib/usda_nutrient_database/importer.rb +43 -0
  22. data/lib/usda_nutrient_database/nutrient.rb +17 -0
  23. data/lib/usda_nutrient_database/railtie.rb +9 -0
  24. data/lib/usda_nutrient_database/version.rb +3 -0
  25. data/spec/database.yml +19 -0
  26. data/spec/lib/usda_nutrient_database/food_group_spec.rb +6 -0
  27. data/spec/lib/usda_nutrient_database/food_spec.rb +8 -0
  28. data/spec/lib/usda_nutrient_database/foods_nutrient_spec.rb +9 -0
  29. data/spec/lib/usda_nutrient_database/import/downloader_spec.rb +46 -0
  30. data/spec/lib/usda_nutrient_database/import/food_groups_spec.rb +25 -0
  31. data/spec/lib/usda_nutrient_database/import/foods_nutrients_spec.rb +13 -0
  32. data/spec/lib/usda_nutrient_database/import/foods_spec.rb +16 -0
  33. data/spec/lib/usda_nutrient_database/import/nutrients_spec.rb +11 -0
  34. data/spec/lib/usda_nutrient_database/importer_spec.rb +18 -0
  35. data/spec/lib/usda_nutrient_database/nutrient_spec.rb +10 -0
  36. data/spec/schema.rb +52 -0
  37. data/spec/spec_helper.rb +63 -0
  38. data/spec/support/sr25.zip +0 -0
  39. data/spec/support/sr25/FD_GROUP.txt +2 -0
  40. data/spec/support/sr25/FOOD_DES.txt +4 -0
  41. data/spec/support/sr25/NUTR_DEF.txt +3 -0
  42. data/spec/support/sr25/NUT_DATA.txt +5 -0
  43. data/usda-nutrient-database.gemspec +21 -10
  44. metadata +201 -5
  45. data/lib/usda/nutrient/database.rb +0 -9
  46. data/lib/usda/nutrient/database/version.rb +0 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 904f2f00161a4f432173ab79541f0a30dfcf34fa
4
- data.tar.gz: 29f8f354f2b7c2f5a2dcfcde28d080776cba43ed
3
+ metadata.gz: cebda7a2b5846d466d3044269890e741582222b1
4
+ data.tar.gz: 3590220700d4755fed490d98e55d6247ec2c99d3
5
5
  SHA512:
6
- metadata.gz: 963fdefa3c576eb48c2d3b9486c00e2b30c9728fa30d4a268b50b521fd4105ffd67c51bafbe6c12e3a03b5169db320506ecb33b72fe388424392bc90e66cc143
7
- data.tar.gz: 67c3cb4451f8718ca739b298ea1886fe0dac0cd84d053096c602cfbb7ba736215d480f341de5b7fe1956d2a01a7229ea62e4a9e76c52e2e4fe964bebf98ebbac
6
+ metadata.gz: ab9b71ae50a77af706f1aad9da7293daa34060bade288d11820662f2c758c410d07806346fd590a13f9eab43e00c8d3ada8de9833a1a9e94cdf398180134c3d0
7
+ data.tar.gz: 388b9e1e6b72fab432c8b51f6f0e5973a7cf062f036d4e081c7079afc140a4aa620aae6b9589c8d9bd6e7f05a53cc46504573c272013284f581d1f0404cd5faf
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ spec/debug.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec, all_on_start: true, all_after_pass: true, keep_mode: :failed do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { 'spec' }
8
+ end
9
+
@@ -0,0 +1,8 @@
1
+ class CreateUsdaFoodGroups < ActiveRecord::Migration
2
+ def change
3
+ create_table :usda_food_groups, id: :code do |t|
4
+ t.string :code, null: false, index: true, uniq: true
5
+ t.string :description, null: false
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,19 @@
1
+ class CreateUsdaFoods < ActiveRecord::Migration
2
+ def change
3
+ create_table :usda_foods, id: false, primary_key: :nutrient_databank_number do |t|
4
+ t.string :nutrient_databank_number, null: false, index: true
5
+ t.string :food_group_code, index: true
6
+ t.string :long_description, null: false
7
+ t.string :short_description, null: false
8
+ t.string :common_names
9
+ t.string :manufacturer_name
10
+ t.boolean :survey
11
+ t.string :refuse_description
12
+ t.integer :percentage_refuse
13
+ t.float :nitrogen_factor
14
+ t.float :protein_factor
15
+ t.float :fat_factor
16
+ t.float :carbohydrate_factor
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ class CreateUsdaFoodsNutrients < ActiveRecord::Migration
2
+ def change
3
+ create_table :usda_foods_nutrients do |t|
4
+ t.string :nutrient_databank_number, null: false
5
+ t.string :nutrient_number, null: false
6
+ t.float :nutrient_value, null: false
7
+ t.integer :num_data_points, null: false
8
+ t.float :standard_error
9
+ t.string :src_code, null: false
10
+ t.string :derivation_code
11
+ t.string :ref_nutrient_databank_number
12
+ t.boolean :add_nutrient_mark
13
+ t.integer :num_studies
14
+ t.float :min
15
+ t.float :max
16
+ t.integer :degrees_of_freedom
17
+ t.float :lower_error_bound
18
+ t.float :upper_error_bound
19
+ t.string :statistical_comments
20
+ t.string :add_mod_date
21
+ t.string :confidence_code
22
+ end
23
+
24
+ add_index :usda_foods_nutrients,
25
+ [:nutrient_databank_number, :nutrient_number],
26
+ name: 'foods_nutrients_index'
27
+ end
28
+ end
@@ -0,0 +1,12 @@
1
+ class CreateUsdaNutrients < ActiveRecord::Migration
2
+ def change
3
+ create_table :nutrients, id: :nutrient_number do |t|
4
+ t.string :nutrient_number, null: false, index: true
5
+ t.string :units, null: false
6
+ t.string :tagname
7
+ t.string :nutrient_description, null: false
8
+ t.string :number_decimal_places, null: false
9
+ t.integer :sort_record_order, null: false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ namespace :usda do
2
+ desc 'Import the latest USDA nutrition data'
3
+ task import: :environment do
4
+ UsdaNutrientDatabase::Importer.new
5
+ end
6
+ end
@@ -0,0 +1,35 @@
1
+ require 'faraday'
2
+ require 'usda_nutrient_database/configuration'
3
+ require 'usda_nutrient_database/food_group'
4
+ require 'usda_nutrient_database/food'
5
+ require 'usda_nutrient_database/nutrient'
6
+ require 'usda_nutrient_database/foods_nutrient'
7
+ require 'usda_nutrient_database/importer'
8
+ require 'usda_nutrient_database/import/base'
9
+ require 'usda_nutrient_database/import/downloader'
10
+ require 'usda_nutrient_database/import/food_groups'
11
+ require 'usda_nutrient_database/import/foods'
12
+ require 'usda_nutrient_database/import/foods_nutrients'
13
+ require 'usda_nutrient_database/import/nutrients'
14
+ require 'usda_nutrient_database/railtie' if defined?(Rails)
15
+ require 'usda_nutrient_database/version'
16
+
17
+ module UsdaNutrientDatabase
18
+ class << self
19
+ attr_writer :configuration
20
+
21
+ def log(message, level = :debug)
22
+ configuration.logger.send(level, message)
23
+ end
24
+
25
+ def configuration
26
+ @configuration ||= UsdaNutrientDatabase::Configuration.new
27
+ end
28
+ end
29
+
30
+ def self.configure
31
+ self.configuration = UsdaNutrientDatabase::Configuration.new
32
+ yield(self.configuration)
33
+ self.configuration
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ module UsdaNutrientDatabase
2
+ class Configuration
3
+ attr_accessor :logger
4
+
5
+ def logger
6
+ @logger ||= Logger.new(STDOUT)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module UsdaNutrientDatabase
2
+ class Food < ActiveRecord::Base
3
+ self.table_name = 'usda_foods'
4
+ self.primary_key = 'nutrient_databank_number'
5
+
6
+ validates :nutrient_databank_number, presence: true,
7
+ uniqueness: { allow_blank: true }
8
+ validates :food_group_code, presence: true
9
+ validates :long_description, presence: true
10
+ validates :short_description, presence: true
11
+
12
+ belongs_to :food_group, class_name: 'UsdaNutrientDatabase::FoodGroup',
13
+ foreign_key: :food_group_code
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ module UsdaNutrientDatabase
2
+ class FoodGroup < ActiveRecord::Base
3
+ self.table_name = 'usda_food_groups'
4
+ self.primary_key = 'code'
5
+
6
+ validates :code, presence: true, uniqueness: { allow_blank: true }
7
+ validates :description, presence: true
8
+
9
+ has_many :foods, class_name: 'UsdaNutrientDatabase::Food',
10
+ dependent: :destroy
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ module UsdaNutrientDatabase
2
+ class FoodsNutrient < ActiveRecord::Base
3
+ self.table_name = 'usda_foods_nutrients'
4
+
5
+ validates :nutrient_databank_number, presence: true,
6
+ uniqueness: { scope: :nutrient_number, allow_blank: true }
7
+ validates :nutrient_number, presence: true,
8
+ uniqueness: { scope: :nutrient_databank_number, allow_blank: true }
9
+ validates :nutrient_value, presence: true
10
+ validates :num_data_points, presence: true
11
+ validates :src_code, presence: true
12
+
13
+ belongs_to :food, class_name: 'UsdaNutrientDatabase::Food',
14
+ foreign_key: :nutrient_databank_number
15
+ belongs_to :nutrient, class_name: 'UsdaNutrientDatabase::Nutrient',
16
+ foreign_key: :nutrient_number
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module UsdaNutrientDatabase
2
+ module Import
3
+ class Base
4
+ attr_reader :directory
5
+
6
+ def initialize(directory)
7
+ @directory = directory
8
+ end
9
+
10
+ private
11
+
12
+ def csv_options
13
+ { col_sep: '^', quote_char: '~' }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,59 @@
1
+ require 'zip'
2
+
3
+ module UsdaNutrientDatabase
4
+ module Import
5
+ class Downloader
6
+
7
+ attr_reader :directory, :version
8
+
9
+ def initialize(directory = 'tmp/usda', version = 'sr25')
10
+ @directory = directory
11
+ @version = version
12
+ end
13
+
14
+ def cleanup
15
+ UsdaNutrientDatabase.log 'Cleaning up data'
16
+ FileUtils.rm_rf(directory)
17
+ end
18
+
19
+ def download_and_unzip
20
+ download and unzip
21
+ end
22
+
23
+ def path
24
+ [
25
+ 'SP2UserFiles', 'Place', '12354500', 'Data', version.upcase,
26
+ 'dnload', "#{version}.zip"
27
+ ].join('/')
28
+ end
29
+
30
+ def download
31
+ UsdaNutrientDatabase.log "Downloading USDA data version: #{version}"
32
+ unless File.directory?(directory)
33
+ FileUtils.mkdir_p("#{directory}/#{version}")
34
+ end
35
+ File.open("#{directory}/#{version}.zip", 'w+b') do |file|
36
+ file.write connection.get(path).body
37
+ end
38
+ end
39
+
40
+ def unzip
41
+ UsdaNutrientDatabase.log 'Unzipping data'
42
+ Zip::File.open("#{directory}/#{version}.zip") do |zipfile|
43
+ zipfile.each do |file|
44
+ unless File.exist?("#{directory}/#{version}/#{file.name}")
45
+ zipfile.extract(file, "#{directory}/#{version}/#{file.name}")
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def connection
52
+ @connection ||= Faraday.new(url: 'http://www.ars.usda.gov') do |faraday|
53
+ faraday.response :logger
54
+ faraday.adapter Faraday.default_adapter
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,19 @@
1
+ require 'csv'
2
+
3
+ module UsdaNutrientDatabase
4
+ module Import
5
+ class FoodGroups < UsdaNutrientDatabase::Import::Base
6
+ def import
7
+ UsdaNutrientDatabase.log 'Importing food groups'
8
+ CSV.open(
9
+ "#{directory}/FD_GROUP.txt", 'r:iso-8859-1:utf-8', csv_options
10
+ ) do |csv|
11
+ csv.each do |row|
12
+ UsdaNutrientDatabase::FoodGroup.create! code: row[0],
13
+ description: row[1]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+
3
+ module UsdaNutrientDatabase
4
+ module Import
5
+ class Foods < Base
6
+ def import
7
+ UsdaNutrientDatabase.log 'Importing foods'
8
+ CSV.open("#{directory}/FOOD_DES.txt", 'r:iso-8859-1:utf-8', csv_options) do |csv|
9
+ csv.each do |row|
10
+ UsdaNutrientDatabase::Food.create!(
11
+ nutrient_databank_number: row[0],
12
+ food_group_code: row[1],
13
+ long_description: row[2],
14
+ short_description: row[3],
15
+ common_names: row[4],
16
+ manufacturer_name: row[5],
17
+ survey: row[6],
18
+ refuse_description: row[7],
19
+ percentage_refuse: row[8],
20
+ nitrogen_factor: row[9],
21
+ protein_factor: row[10],
22
+ fat_factor: row[11],
23
+ carbohydrate_factor: row[12]
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ module UsdaNutrientDatabase
2
+ module Import
3
+ class FoodsNutrients < Base
4
+ def import
5
+ UsdaNutrientDatabase.log 'Importing foods_nutrients'
6
+ CSV.open(
7
+ "#{directory}/NUT_DATA.txt", 'r:iso-8859-1:utf-8', csv_options
8
+ ) do |csv|
9
+ csv.each { |row| extract_row(row) }
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def columns
16
+ [
17
+ :nutrient_databank_number, :nutrient_number, :nutrient_value,
18
+ :num_data_points, :standard_error, :src_code, :derivation_code,
19
+ :ref_nutrient_databank_number, :add_nutrient_mark, :num_studies, :min,
20
+ :max, :degrees_of_freedom, :lower_error_bound, :upper_error_bound,
21
+ :statistical_comments, :add_mod_date
22
+ ]
23
+ end
24
+
25
+ def extract_row(row)
26
+ attrs = {}
27
+ columns.each_with_index do |column, index|
28
+ attrs.merge!(column => row[index])
29
+ end
30
+ UsdaNutrientDatabase::FoodsNutrient.create!(attrs)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ module UsdaNutrientDatabase
2
+ module Import
3
+ class Nutrients < Base
4
+ def import
5
+ UsdaNutrientDatabase.log 'Importing nutrients'
6
+ CSV.open(
7
+ "#{directory}/NUTR_DEF.txt", 'r:iso-8859-1:utf-8', csv_options
8
+ ) do |csv|
9
+ csv.each do |row|
10
+ UsdaNutrientDatabase::Nutrient.create!(
11
+ nutrient_number: row[0], units: row[1], tagname: row[2],
12
+ nutrient_description: row[3], number_decimal_places: row[4],
13
+ sort_record_order: row[5]
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ module UsdaNutrientDatabase
2
+ class Importer
3
+ attr_reader :directory, :version
4
+
5
+ def initialize(directory = 'tmp/usda', version = 'sr25')
6
+ @directory = directory
7
+ @version = version
8
+ end
9
+
10
+ def import
11
+ downloader.download_and_unzip
12
+ food_group_importer.import
13
+ food_importer.import
14
+ nutrient_importer.import
15
+ foods_nutrient_importer.import
16
+ ensure
17
+ downloader.cleanup
18
+ end
19
+
20
+ private
21
+
22
+ def food_importer
23
+ UsdaNutrientDatabase::Import::Foods.new("#{directory}/#{version}")
24
+ end
25
+
26
+ def nutrient_importer
27
+ UsdaNutrientDatabase::Import::Nutrients.new("#{directory}/#{version}")
28
+ end
29
+
30
+ def foods_nutrient_importer
31
+ UsdaNutrientDatabase::Import::FoodsNutrients.
32
+ new("#{directory}/#{version}")
33
+ end
34
+
35
+ def food_group_importer
36
+ UsdaNutrientDatabase::Import::FoodGroups.new("#{directory}/#{version}")
37
+ end
38
+
39
+ def downloader
40
+ UsdaNutrientDatabase::Import::Downloader.new(directory, version)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,17 @@
1
+ module UsdaNutrientDatabase
2
+ class Nutrient < ActiveRecord::Base
3
+ self.table_name = 'usda_nutrients'
4
+
5
+ validates :nutrient_number, presence: true,
6
+ uniqueness: { allow_blank: true }
7
+ validates :units, presence: true
8
+ validates :nutrient_description, presence: true
9
+ validates :number_decimal_places, presence: true
10
+ validates :sort_record_order, presence: true,
11
+ numericality: { allow_blank: true }
12
+
13
+ has_many :foods_nutrients,
14
+ class_name: 'UsdaNutrientDatabase::FoodsNutrient',
15
+ dependent: :destroy
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module UsdaNutrientDatabase
2
+ class Railtie < Rails::Railtie
3
+ railtie_name :usda_nutrient_database
4
+
5
+ rake_tasks do
6
+ load 'tasks/usda_nutrient_database.rake'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module UsdaNutrientDatabase
2
+ VERSION = "0.1.0"
3
+ end
data/spec/database.yml ADDED
@@ -0,0 +1,19 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: usda_nutrient_database_test.sqlite3
4
+
5
+ mysql:
6
+ adapter: mysql2
7
+ hostname: localhost
8
+ username: root
9
+ password:
10
+ database: usda_nutrient_database_test
11
+ charset: utf8
12
+
13
+ postgresql:
14
+ adapter: postgresql
15
+ hostname: localhost
16
+ username: postgres
17
+ password:
18
+ database: usda_nutrient_database_test
19
+ encoding: utf8
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsdaNutrientDatabase::FoodGroup do
4
+ it { should validate_presence_of(:code) }
5
+ it { should validate_presence_of(:description) }
6
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsdaNutrientDatabase::Food do
4
+ it { should validate_presence_of(:nutrient_databank_number) }
5
+ it { should validate_presence_of(:food_group_code) }
6
+ it { should validate_presence_of(:long_description) }
7
+ it { should validate_presence_of(:short_description) }
8
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsdaNutrientDatabase::FoodsNutrient do
4
+ it { should validate_presence_of(:nutrient_databank_number) }
5
+ it { should validate_presence_of(:nutrient_number) }
6
+ it { should validate_presence_of(:nutrient_value) }
7
+ it { should validate_presence_of(:num_data_points) }
8
+ it { should validate_presence_of(:src_code) }
9
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsdaNutrientDatabase::Import::Downloader do
4
+ let(:downloader) { described_class.new('tmp/usda') }
5
+ let(:extraction_path) { "#{directory}/#{version}" }
6
+ let(:directory) { 'tmp/usda' }
7
+ let(:version) { 'sr25' }
8
+ let(:filenames) do
9
+ [
10
+ 'DATA_SRC.txt', 'DATSRCLN.txt', 'DERIV_CD.txt', 'FD_GROUP.txt',
11
+ 'FOOD_DES.txt', 'FOOTNOTE.txt', 'LANGDESC.txt', 'LANGUAL.txt',
12
+ 'NUTR_DEF.txt', 'NUT_DATA.txt', 'SRC_CD.txt', 'WEIGHT.txt', 'sr25_doc.pdf'
13
+ ]
14
+ end
15
+
16
+ describe '#download_and_unzip' do
17
+ before do
18
+ stub_request(:get, /.*/).
19
+ to_return(body: File.read('spec/support/sr25.zip'))
20
+ downloader.download_and_unzip
21
+ end
22
+
23
+ after { downloader.cleanup }
24
+
25
+ it 'should download and extract all files' do
26
+ filenames.each do |filename|
27
+ expect(File.exist?("#{extraction_path}/#{filename}")).to eql(true)
28
+ end
29
+ end
30
+ end
31
+
32
+ describe '#cleanup' do
33
+ before do
34
+ stub_request(:get, /.*/).
35
+ to_return(body: File.read('spec/support/sr25.zip'))
36
+ downloader.download_and_unzip
37
+ downloader.cleanup
38
+ end
39
+
40
+ it 'should remove all of the extracted files' do
41
+ filenames.each do |filename|
42
+ expect(File.exist?("#{extraction_path}/#{filename}")).to eql(false)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsdaNutrientDatabase::Import::FoodGroups do
4
+ let(:importer) { described_class.new('spec/support/sr25') }
5
+
6
+ describe '#import' do
7
+ before { importer.import }
8
+
9
+ it 'should import all food groups' do
10
+ expect(UsdaNutrientDatabase::FoodGroup.count).to eql(2)
11
+ end
12
+
13
+ it 'should import food group data correctly' do
14
+ [
15
+ { code: '0100', description: 'Dairy and Egg Products' },
16
+ { code: '0200', description: 'Spices and Herbs' }
17
+ ].each do |product|
18
+ expect(
19
+ UsdaNutrientDatabase::FoodGroup.
20
+ where(code: product[:code], description: product[:description]).count
21
+ ).to eql(1)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsdaNutrientDatabase::Import::FoodsNutrients do
4
+ let(:importer) { described_class.new('spec/support/sr25') }
5
+
6
+ describe '#import' do
7
+ before { importer.import }
8
+
9
+ it do
10
+ expect(UsdaNutrientDatabase::FoodsNutrient.count).to eql(5)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsdaNutrientDatabase::Import::Foods do
4
+ let(:importer) { described_class.new('spec/support/sr25') }
5
+
6
+ describe '#import' do
7
+ before { importer.import }
8
+
9
+ it { expect(UsdaNutrientDatabase::Food.count).to eql(4) }
10
+
11
+ it do
12
+ expect(UsdaNutrientDatabase::Food.where(food_group_code: '0100').count).
13
+ to eql(3)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsdaNutrientDatabase::Import::Nutrients do
4
+ let(:importer) { described_class.new('spec/support/sr25') }
5
+
6
+ describe '#import' do
7
+ before { importer.import }
8
+
9
+ it { expect(UsdaNutrientDatabase::Nutrient.count).to eql(3) }
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsdaNutrientDatabase::Importer do
4
+ let(:importer) { UsdaNutrientDatabase::Importer.new }
5
+
6
+ describe '#import' do
7
+ before do
8
+ stub_request(:get, /.*/).
9
+ to_return(body: File.read('spec/support/sr25.zip'))
10
+ importer.import
11
+ end
12
+
13
+ it { expect(UsdaNutrientDatabase::FoodGroup.count).to eql(25) }
14
+ it { expect(UsdaNutrientDatabase::Food.count).to eql(16) }
15
+ it { expect(UsdaNutrientDatabase::Nutrient.count).to eql(15) }
16
+ it { expect(UsdaNutrientDatabase::FoodsNutrient.count).to eql(12) }
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe UsdaNutrientDatabase::Nutrient do
4
+ it { should validate_presence_of(:nutrient_number) }
5
+ it { should validate_presence_of(:units) }
6
+ it { should validate_presence_of(:nutrient_description) }
7
+ it { should validate_presence_of(:number_decimal_places) }
8
+ it { should validate_presence_of(:sort_record_order) }
9
+ it { should validate_numericality_of(:sort_record_order) }
10
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,52 @@
1
+ ActiveRecord::Schema.define version: 0 do
2
+ create_table :usda_food_groups, force: true, primary_key: :code, id: false do |t|
3
+ t.string :code, null: false, index: true, unique: true
4
+ t.string :description, null: false
5
+ end
6
+
7
+ create_table :usda_foods, force: true, primary_key: :code, id: false do |t|
8
+ t.string :nutrient_databank_number, null: false, index: true
9
+ t.string :food_group_code, index: true
10
+ t.string :long_description, null: false
11
+ t.string :short_description, null: false
12
+ t.string :common_names
13
+ t.string :manufacturer_name
14
+ t.boolean :survey
15
+ t.string :refuse_description
16
+ t.integer :percentage_refuse
17
+ t.float :nitrogen_factor
18
+ t.float :protein_factor
19
+ t.float :fat_factor
20
+ t.float :carbohydrate_factor
21
+ end
22
+
23
+ create_table :usda_nutrients, force: true, primary_key: :nutrient_number, id: false do |t|
24
+ t.string :nutrient_number, null: false, index: true
25
+ t.string :units, null: false
26
+ t.string :tagname
27
+ t.string :nutrient_description, null: false
28
+ t.string :number_decimal_places, null: false
29
+ t.integer :sort_record_order, null: false
30
+ end
31
+
32
+ create_table :usda_foods_nutrients, force: true do |t|
33
+ t.string :nutrient_databank_number, null: false
34
+ t.string :nutrient_number, null: false
35
+ t.float :nutrient_value, null: false
36
+ t.integer :num_data_points, null: false
37
+ t.float :standard_error
38
+ t.string :src_code, null: false
39
+ t.string :derivation_code
40
+ t.string :ref_nutrient_databank_number
41
+ t.boolean :add_nutrient_mark
42
+ t.integer :num_studies
43
+ t.float :min
44
+ t.float :max
45
+ t.integer :degrees_of_freedom
46
+ t.float :lower_error_bound
47
+ t.float :upper_error_bound
48
+ t.string :statistical_comments
49
+ t.string :add_mod_date
50
+ t.string :confidence_code
51
+ end
52
+ end
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'logger'
4
+
5
+ Bundler.setup
6
+
7
+ require 'active_record'
8
+ require 'database_cleaner'
9
+ require 'usda-nutrient-database'
10
+ require 'shoulda-matchers'
11
+ require 'webmock/rspec'
12
+
13
+ # This file was generated by the `rspec --init` command. Conventionally, all
14
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
15
+ # Require this file using `require "spec_helper"` to ensure that it is only
16
+ # loaded once.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ config.run_all_when_everything_filtered = true
21
+ config.filter_run :focus
22
+
23
+ # Run specs in random order to surface order dependencies. If you find an
24
+ # order dependency and want to debug it, you can fix the order by providing
25
+ # the seed, which is printed after each run.
26
+ # --seed 1234
27
+ config.order = 'random'
28
+
29
+ config.fail_fast = true
30
+
31
+ db_name = ENV['DB'] || 'postgresql'
32
+ database_yml = File.expand_path('../database.yml', __FILE__)
33
+ ActiveRecord::Base.configurations = YAML.load_file(database_yml)
34
+ db_config = ActiveRecord::Base.configurations[db_name]
35
+
36
+ begin
37
+ ActiveRecord::Base.establish_connection(db_name)
38
+ ActiveRecord::Base.connection
39
+ rescue PG::ConnectionBad
40
+ ActiveRecord::Base.establish_connection db_config.merge('database' => nil)
41
+ ActiveRecord::Base.connection.create_database db_config['database']
42
+ ActiveRecord::Base.establish_connection db_config
43
+ end
44
+
45
+ ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), 'debug.log'))
46
+ ActiveRecord::Base.default_timezone = :utc
47
+
48
+ ActiveRecord::Migration.verbose = false
49
+ load(File.join(File.dirname(__FILE__), 'schema.rb'))
50
+
51
+ config.before :suite do
52
+ DatabaseCleaner.strategy = :transaction
53
+ DatabaseCleaner.clean_with(:truncation)
54
+ end
55
+
56
+ config.before do
57
+ DatabaseCleaner.start
58
+ end
59
+
60
+ config.after do
61
+ DatabaseCleaner.clean
62
+ end
63
+ end
Binary file
@@ -0,0 +1,2 @@
1
+ ~0100~^~Dairy and Egg Products~
2
+ ~0200~^~Spices and Herbs~
@@ -0,0 +1,4 @@
1
+ ~01001~^~0100~^~Butter, salted~^~BUTTER,WITH SALT~^~~^~~^~Y~^~~^0^~~^6.38^4.27^8.79^3.87
2
+ ~01002~^~0100~^~Butter, whipped, with salt~^~BUTTER,WHIPPED,WITH SALT~^~~^~~^~Y~^~~^0^~~^6.38^4.27^8.79^3.87
3
+ ~01003~^~0100~^~Butter oil, anhydrous~^~BUTTER OIL,ANHYDROUS~^~~^~~^~Y~^~~^0^~~^6.38^4.27^8.79^3.87
4
+ ~02001~^~0200~^~Spices, allspice, ground~^~ALLSPICE,GROUND~^~~^~~^~~^~~^0^~Pimenta dioica~^6.25^3.36^8.37^2.35
@@ -0,0 +1,3 @@
1
+ ~203~^~g~^~PROCNT~^~Protein~^~2~^~600~
2
+ ~204~^~g~^~FAT~^~Total lipid (fat)~^~2~^~800~
3
+ ~205~^~g~^~CHOCDF~^~Carbohydrate, by difference~^~2~^~1100~
@@ -0,0 +1,5 @@
1
+ ~01001~^~203~^0.85^16^0.074^~1~^~~^~~^~~^^^^^^^~~^11/1976^
2
+ ~01001~^~204~^81.11^580^0.065^~1~^~~^~~^~~^^^^^^^~~^11/1976^
3
+ ~01001~^~205~^0.06^0^^~4~^~NC~^~~^~~^^^^^^^~~^11/1976^
4
+ ~01001~^~207~^2.11^35^0.054^~1~^~~^~~^~~^^^^^^^~~^11/1976^
5
+ ~01001~^~208~^717^0^^~4~^~NC~^~~^~~^^^^^^^~~^08/2010^
@@ -1,23 +1,34 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'usda/nutrient/database/version'
4
+ require 'usda_nutrient_database/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "usda-nutrient-database"
8
- spec.version = Usda::Nutrient::Database::VERSION
9
- spec.authors = ["Matt Beedle"]
10
- spec.email = ["mattbeedle@googlemail.com"]
7
+ spec.name = 'usda-nutrient-database'
8
+ spec.version = UsdaNutrientDatabase::VERSION
9
+ spec.authors = ['Matt Beedle']
10
+ spec.email = ['mattbeedle@googlemail.com']
11
11
  spec.description = %q{A gem to include all the USDA nutrient data quickly into a ruby project}
12
12
  spec.summary = %q{A gem to include all the USDA nutrient data quickly into a ruby project}
13
- spec.homepage = ""
14
- spec.license = "MIT"
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
21
+ spec.add_runtime_dependency 'activerecord', '>= 3.0.0'
22
+ spec.add_runtime_dependency 'faraday'
23
+ spec.add_runtime_dependency 'rubyzip'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.3'
26
+ spec.add_development_dependency 'database_cleaner'
27
+ spec.add_development_dependency 'guard'
28
+ spec.add_development_dependency 'guard-rspec'
29
+ spec.add_development_dependency 'pg'
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'rspec'
32
+ spec.add_development_dependency 'shoulda-matchers'
33
+ spec.add_development_dependency 'webmock'
23
34
  end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usda-nutrient-database
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Beedle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-23 00:00:00.000000000 Z
11
+ date: 2013-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubyzip
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
13
55
  - !ruby/object:Gem::Dependency
14
56
  name: bundler
15
57
  requirement: !ruby/object:Gem::Requirement
@@ -24,6 +66,62 @@ dependencies:
24
66
  - - ~>
25
67
  - !ruby/object:Gem::Version
26
68
  version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: database_cleaner
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pg
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
27
125
  - !ruby/object:Gem::Dependency
28
126
  name: rake
29
127
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +136,48 @@ dependencies:
38
136
  - - '>='
39
137
  - !ruby/object:Gem::Version
40
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: shoulda-matchers
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: webmock
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - '>='
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
41
181
  description: A gem to include all the USDA nutrient data quickly into a ruby project
42
182
  email:
43
183
  - mattbeedle@googlemail.com
@@ -46,12 +186,50 @@ extensions: []
46
186
  extra_rdoc_files: []
47
187
  files:
48
188
  - .gitignore
189
+ - .rspec
49
190
  - Gemfile
191
+ - Guardfile
50
192
  - LICENSE.txt
51
193
  - README.md
52
194
  - Rakefile
53
- - lib/usda/nutrient/database.rb
54
- - lib/usda/nutrient/database/version.rb
195
+ - db/migrate/1_create_usda_food_groups.rb
196
+ - db/migrate/2_create_usda_foods.rb
197
+ - db/migrate/3_create_usda_foods_nutrients.rb
198
+ - db/migrate/4_create_usda_nutrients.rb
199
+ - lib/tasks/usda_nutrient_database.rake
200
+ - lib/usda-nutrient-database.rb
201
+ - lib/usda_nutrient_database/configuration.rb
202
+ - lib/usda_nutrient_database/food.rb
203
+ - lib/usda_nutrient_database/food_group.rb
204
+ - lib/usda_nutrient_database/foods_nutrient.rb
205
+ - lib/usda_nutrient_database/import/base.rb
206
+ - lib/usda_nutrient_database/import/downloader.rb
207
+ - lib/usda_nutrient_database/import/food_groups.rb
208
+ - lib/usda_nutrient_database/import/foods.rb
209
+ - lib/usda_nutrient_database/import/foods_nutrients.rb
210
+ - lib/usda_nutrient_database/import/nutrients.rb
211
+ - lib/usda_nutrient_database/importer.rb
212
+ - lib/usda_nutrient_database/nutrient.rb
213
+ - lib/usda_nutrient_database/railtie.rb
214
+ - lib/usda_nutrient_database/version.rb
215
+ - spec/database.yml
216
+ - spec/lib/usda_nutrient_database/food_group_spec.rb
217
+ - spec/lib/usda_nutrient_database/food_spec.rb
218
+ - spec/lib/usda_nutrient_database/foods_nutrient_spec.rb
219
+ - spec/lib/usda_nutrient_database/import/downloader_spec.rb
220
+ - spec/lib/usda_nutrient_database/import/food_groups_spec.rb
221
+ - spec/lib/usda_nutrient_database/import/foods_nutrients_spec.rb
222
+ - spec/lib/usda_nutrient_database/import/foods_spec.rb
223
+ - spec/lib/usda_nutrient_database/import/nutrients_spec.rb
224
+ - spec/lib/usda_nutrient_database/importer_spec.rb
225
+ - spec/lib/usda_nutrient_database/nutrient_spec.rb
226
+ - spec/schema.rb
227
+ - spec/spec_helper.rb
228
+ - spec/support/sr25.zip
229
+ - spec/support/sr25/FD_GROUP.txt
230
+ - spec/support/sr25/FOOD_DES.txt
231
+ - spec/support/sr25/NUTR_DEF.txt
232
+ - spec/support/sr25/NUT_DATA.txt
55
233
  - usda-nutrient-database.gemspec
56
234
  homepage: ''
57
235
  licenses:
@@ -77,4 +255,22 @@ rubygems_version: 2.1.5
77
255
  signing_key:
78
256
  specification_version: 4
79
257
  summary: A gem to include all the USDA nutrient data quickly into a ruby project
80
- test_files: []
258
+ test_files:
259
+ - spec/database.yml
260
+ - spec/lib/usda_nutrient_database/food_group_spec.rb
261
+ - spec/lib/usda_nutrient_database/food_spec.rb
262
+ - spec/lib/usda_nutrient_database/foods_nutrient_spec.rb
263
+ - spec/lib/usda_nutrient_database/import/downloader_spec.rb
264
+ - spec/lib/usda_nutrient_database/import/food_groups_spec.rb
265
+ - spec/lib/usda_nutrient_database/import/foods_nutrients_spec.rb
266
+ - spec/lib/usda_nutrient_database/import/foods_spec.rb
267
+ - spec/lib/usda_nutrient_database/import/nutrients_spec.rb
268
+ - spec/lib/usda_nutrient_database/importer_spec.rb
269
+ - spec/lib/usda_nutrient_database/nutrient_spec.rb
270
+ - spec/schema.rb
271
+ - spec/spec_helper.rb
272
+ - spec/support/sr25.zip
273
+ - spec/support/sr25/FD_GROUP.txt
274
+ - spec/support/sr25/FOOD_DES.txt
275
+ - spec/support/sr25/NUTR_DEF.txt
276
+ - spec/support/sr25/NUT_DATA.txt
@@ -1,9 +0,0 @@
1
- require "usda/nutrient/database/version"
2
-
3
- module Usda
4
- module Nutrient
5
- module Database
6
- # Your code goes here...
7
- end
8
- end
9
- end
@@ -1,7 +0,0 @@
1
- module Usda
2
- module Nutrient
3
- module Database
4
- VERSION = "0.0.1"
5
- end
6
- end
7
- end