usda-nutrient-database 1.1.0 → 1.2.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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +1 -2
- data/CHANGELOG.md +7 -0
- data/README.md +16 -0
- data/lib/usda-nutrient-database.rb +1 -0
- data/lib/usda_nutrient_database/import/base.rb +5 -1
- data/lib/usda_nutrient_database/import/downloader.rb +22 -3
- data/lib/usda_nutrient_database/import/foods.rb +5 -0
- data/lib/usda_nutrient_database/version.rb +1 -1
- data/spec/lib/usda_nutrient_database/import/downloader_spec.rb +16 -1
- data/spec/lib/usda_nutrient_database/importer_spec.rb +35 -15
- data/spec/spec_helper.rb +1 -2
- data/spec/support/sr27.zip +0 -0
- data/usda-nutrient-database.gemspec +1 -0
- metadata +19 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7ce03922c74860e2745e15d03f6c272def82a4c5
|
|
4
|
+
data.tar.gz: 4fb9cf73acb5c2800f954c6a2c65ff9e431d5e5e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5e03b8800a02759e631d5222cf2cdf5316c18bc0969f05763915f91624aa7ae53b959635a15f4ec633edaf0307cf03455642649875198bca3d810900a346a141
|
|
7
|
+
data.tar.gz: 14a2a5e859edd9af69f602ede9706108a5f723ee5b76f44bcc7f0880633853d486c1a5b4b3cfe991d006c0b5963d21e938b9cf361e9ecfeac31c2271a31acccf
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.2.0
|
|
4
|
+
|
|
5
|
+
- Upgrade to version SR27 USDA dataset.
|
|
6
|
+
[#5](https://github.com/mattbeedle/usda-nutrient-database/pull/5)
|
|
7
|
+
- Require ActiveRecord so the gem can work outside of Rails.
|
|
8
|
+
[#5](https://github.com/mattbeedle/usda-nutrient-database/pull/5)
|
|
9
|
+
|
|
3
10
|
## 1.0.0
|
|
4
11
|
|
|
5
12
|
- Update README
|
data/README.md
CHANGED
|
@@ -9,6 +9,8 @@ Status](https://coveralls.io/repos/mattbeedle/usda-nutrient-database/badge.png)]
|
|
|
9
9
|
Status](https://gemnasium.com/mattbeedle/usda-nutrient-database.png)](https://gemnasium.com/mattbeedle/usda-nutrient-database)
|
|
10
10
|
[](http://badge.fury.io/rb/usda-nutrient-database)
|
|
12
|
+
[](https://gitter.im/mattbeedle/usda-nutrient-database)
|
|
12
14
|
|
|
13
15
|
# USDA Nutrient Database
|
|
14
16
|
|
|
@@ -18,6 +20,14 @@ to be desired.
|
|
|
18
20
|
|
|
19
21
|
This is a simple gem to import the database into your ruby application.
|
|
20
22
|
|
|
23
|
+
## Using the USDA Nutrient Database REST API instead
|
|
24
|
+
|
|
25
|
+
Even after importing this data, keeping it up to date and adding/maintaining
|
|
26
|
+
additional logic such as full text search, weight conversion, etc is a pain. As
|
|
27
|
+
an alternative I have put together a small RESTful web service to do this for you.
|
|
28
|
+
You can find the docs at
|
|
29
|
+
[http://docs.usdanutrientservice.apiary.io/](http://docs.usdanutrientservice.apiary.io/)
|
|
30
|
+
|
|
21
31
|
## Installation
|
|
22
32
|
|
|
23
33
|
Add this line to your application's Gemfile:
|
|
@@ -76,6 +86,12 @@ Any feature requests/ideas can also go in the issues list.
|
|
|
76
86
|
4. Push to the branch (`git push origin my-new-feature`)
|
|
77
87
|
5. Create new Pull Request
|
|
78
88
|
|
|
89
|
+
## Contributors
|
|
90
|
+
|
|
91
|
+
Many thanks to the following contributers:
|
|
92
|
+
|
|
93
|
+
- [@benfalk](https://github.com/benfalk)
|
|
94
|
+
|
|
79
95
|
## Versioning
|
|
80
96
|
|
|
81
97
|
This gem follows [Semantic Versioning](http://semver.org)
|
|
@@ -19,7 +19,7 @@ module UsdaNutrientDatabase
|
|
|
19
19
|
attr_reader :directory
|
|
20
20
|
|
|
21
21
|
def extract_row(row)
|
|
22
|
-
build_object(row).save
|
|
22
|
+
build_object(apply_typecasts(row)).save
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
def build_object(row)
|
|
@@ -38,6 +38,10 @@ module UsdaNutrientDatabase
|
|
|
38
38
|
raise NotImplementedError
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
def apply_typecasts(row)
|
|
42
|
+
row
|
|
43
|
+
end
|
|
44
|
+
|
|
41
45
|
def file_location
|
|
42
46
|
"#{directory}/#{filename}"
|
|
43
47
|
end
|
|
@@ -23,7 +23,7 @@ module UsdaNutrientDatabase
|
|
|
23
23
|
def path
|
|
24
24
|
[
|
|
25
25
|
'SP2UserFiles', 'Place', '12354500', 'Data', version.upcase,
|
|
26
|
-
'dnload', "#{
|
|
26
|
+
'dnload', "#{version_file}.zip"
|
|
27
27
|
].join('/')
|
|
28
28
|
end
|
|
29
29
|
|
|
@@ -33,7 +33,7 @@ module UsdaNutrientDatabase
|
|
|
33
33
|
FileUtils.mkdir_p("#{directory}/#{version}")
|
|
34
34
|
end
|
|
35
35
|
File.open("#{directory}/#{version}.zip", 'w+b') do |file|
|
|
36
|
-
file.write connection.get(
|
|
36
|
+
file.write connection.get(final_path).body
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
@@ -49,7 +49,26 @@ module UsdaNutrientDatabase
|
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
def connection
|
|
52
|
-
@connection ||= Faraday.new(url: '
|
|
52
|
+
@connection ||= Faraday.new(url: 'https://www.ars.usda.gov')
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def final_path
|
|
56
|
+
look_ahead = connection.head(path)
|
|
57
|
+
if look_ahead.status == 302
|
|
58
|
+
raise 'No location' unless look_ahead.headers['location']
|
|
59
|
+
URI.parse(look_ahead.headers['location']).path
|
|
60
|
+
else
|
|
61
|
+
path
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def version_file
|
|
66
|
+
case @version
|
|
67
|
+
when 'sr27'
|
|
68
|
+
'sr27asc'
|
|
69
|
+
else
|
|
70
|
+
@version
|
|
71
|
+
end
|
|
53
72
|
end
|
|
54
73
|
end
|
|
55
74
|
end
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe UsdaNutrientDatabase::Import::Downloader do
|
|
4
|
-
let(:downloader) { described_class.new(
|
|
4
|
+
let(:downloader) { described_class.new(directory, version) }
|
|
5
5
|
let(:extraction_path) { "#{directory}/#{version}" }
|
|
6
6
|
let(:directory) { 'tmp/usda' }
|
|
7
7
|
let(:version) { 'sr25' }
|
|
@@ -13,10 +13,23 @@ describe UsdaNutrientDatabase::Import::Downloader do
|
|
|
13
13
|
]
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
describe '#version_file' do
|
|
17
|
+
subject { downloader.version_file }
|
|
18
|
+
context 'with version sr25' do
|
|
19
|
+
it { is_expected.to eq 'sr25' }
|
|
20
|
+
end
|
|
21
|
+
context 'with version sr27' do
|
|
22
|
+
let(:version) { 'sr27' }
|
|
23
|
+
it { is_expected.to eq 'sr27asc' }
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
16
27
|
describe '#download_and_unzip' do
|
|
17
28
|
before do
|
|
18
29
|
stub_request(:get, /.*/).
|
|
19
30
|
to_return(body: File.read('spec/support/sr25.zip'))
|
|
31
|
+
expect(downloader).to receive(:final_path)
|
|
32
|
+
.and_return('/sr25.zip')
|
|
20
33
|
downloader.download_and_unzip
|
|
21
34
|
end
|
|
22
35
|
|
|
@@ -33,6 +46,8 @@ describe UsdaNutrientDatabase::Import::Downloader do
|
|
|
33
46
|
before do
|
|
34
47
|
stub_request(:get, /.*/).
|
|
35
48
|
to_return(body: File.read('spec/support/sr25.zip'))
|
|
49
|
+
expect(downloader).to receive(:final_path)
|
|
50
|
+
.and_return('/sr25.zip')
|
|
36
51
|
downloader.download_and_unzip
|
|
37
52
|
downloader.cleanup
|
|
38
53
|
end
|
|
@@ -3,29 +3,49 @@ require 'spec_helper'
|
|
|
3
3
|
describe UsdaNutrientDatabase::Importer do
|
|
4
4
|
let(:importer) { UsdaNutrientDatabase::Importer.new }
|
|
5
5
|
|
|
6
|
-
describe '#import' do
|
|
6
|
+
describe '#import (sr25)' do
|
|
7
|
+
let(:sr_file) { File.read('spec/support/sr25.zip') }
|
|
7
8
|
before do
|
|
9
|
+
stub_request(:head, "https://www.ars.usda.gov/SP2UserFiles/Place/12354500/Data/SR25/dnload/sr25.zip").
|
|
10
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Faraday v0.9.1'}).
|
|
11
|
+
to_return(:status => 200, :body => "", :headers => {})
|
|
8
12
|
stub_request(:get, /.*/).
|
|
9
|
-
to_return(body:
|
|
13
|
+
to_return(body: sr_file)
|
|
10
14
|
importer.import
|
|
11
15
|
end
|
|
12
16
|
|
|
13
|
-
it
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
it 'should import the right number of records' do
|
|
18
|
+
expect(UsdaNutrientDatabase::FoodGroup.count).to eql(25)
|
|
19
|
+
expect(UsdaNutrientDatabase::Food.count).to eql(16)
|
|
20
|
+
expect(UsdaNutrientDatabase::Nutrient.count).to eql(15)
|
|
21
|
+
expect(UsdaNutrientDatabase::FoodsNutrient.count).to eql(12)
|
|
22
|
+
expect(UsdaNutrientDatabase::Weight.count).to eql(11)
|
|
23
|
+
expect(UsdaNutrientDatabase::Footnote.count).to eql(7)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context 'with sr27' do
|
|
27
|
+
let(:sr_file) { File.read('spec/support/sr27.zip') }
|
|
28
|
+
it 'should import the right number of records' do
|
|
29
|
+
expect(UsdaNutrientDatabase::FoodGroup.count).to eql(25)
|
|
30
|
+
expect(UsdaNutrientDatabase::Food.count).to eql(5)
|
|
31
|
+
expect(UsdaNutrientDatabase::Nutrient.count).to eql(150)
|
|
32
|
+
expect(UsdaNutrientDatabase::FoodsNutrient.count).to eql(476)
|
|
33
|
+
expect(UsdaNutrientDatabase::Weight.count).to eql(18)
|
|
34
|
+
expect(UsdaNutrientDatabase::Footnote.count).to eql(362)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
19
37
|
|
|
20
38
|
context 'importing twice' do
|
|
21
39
|
before { importer.import }
|
|
22
40
|
|
|
23
|
-
it
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
41
|
+
it 'should import the right number of records' do
|
|
42
|
+
expect(UsdaNutrientDatabase::FoodGroup.count).to eql(25)
|
|
43
|
+
expect(UsdaNutrientDatabase::Food.count).to eql(16)
|
|
44
|
+
expect(UsdaNutrientDatabase::Nutrient.count).to eql(15)
|
|
45
|
+
expect(UsdaNutrientDatabase::FoodsNutrient.count).to eql(12)
|
|
46
|
+
expect(UsdaNutrientDatabase::Weight.count).to eql(11)
|
|
47
|
+
expect(UsdaNutrientDatabase::Footnote.count).to eql(7)
|
|
48
|
+
end
|
|
29
49
|
end
|
|
30
50
|
end
|
|
31
|
-
end
|
|
51
|
+
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -25,7 +25,6 @@ end
|
|
|
25
25
|
#
|
|
26
26
|
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
|
27
27
|
RSpec.configure do |config|
|
|
28
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
|
29
28
|
config.run_all_when_everything_filtered = true
|
|
30
29
|
config.filter_run :focus
|
|
31
30
|
|
|
@@ -47,7 +46,7 @@ RSpec.configure do |config|
|
|
|
47
46
|
setup_database(db_name, db_config)
|
|
48
47
|
|
|
49
48
|
begin
|
|
50
|
-
ActiveRecord::Base.establish_connection(db_name)
|
|
49
|
+
ActiveRecord::Base.establish_connection(db_name.to_sym)
|
|
51
50
|
ActiveRecord::Base.connection
|
|
52
51
|
rescue PG::ConnectionBad
|
|
53
52
|
ActiveRecord::Base.establish_connection db_config.merge('database' => nil)
|
|
Binary file
|
|
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
|
|
|
28
28
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
|
29
29
|
spec.add_development_dependency 'coveralls'
|
|
30
30
|
spec.add_development_dependency 'database_cleaner'
|
|
31
|
+
spec.add_development_dependency 'sqlite3' if RUBY_PLATFORM != 'java'
|
|
31
32
|
spec.add_development_dependency 'guard'
|
|
32
33
|
spec.add_development_dependency 'guard-rspec'
|
|
33
34
|
spec.add_development_dependency 'pg' if RUBY_PLATFORM != 'java'
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: usda-nutrient-database
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.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:
|
|
11
|
+
date: 2015-06-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -94,6 +94,20 @@ dependencies:
|
|
|
94
94
|
- - ">="
|
|
95
95
|
- !ruby/object:Gem::Version
|
|
96
96
|
version: '0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: sqlite3
|
|
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'
|
|
97
111
|
- !ruby/object:Gem::Dependency
|
|
98
112
|
name: guard
|
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -267,6 +281,7 @@ files:
|
|
|
267
281
|
- spec/support/sr25/NUT_DATA.txt
|
|
268
282
|
- spec/support/sr25/SRC_CD.txt
|
|
269
283
|
- spec/support/sr25/WEIGHT.txt
|
|
284
|
+
- spec/support/sr27.zip
|
|
270
285
|
- usda-nutrient-database.gemspec
|
|
271
286
|
homepage: https://github.com/mattbeedle/usda-nutrient-database
|
|
272
287
|
licenses:
|
|
@@ -288,7 +303,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
288
303
|
version: '0'
|
|
289
304
|
requirements: []
|
|
290
305
|
rubyforge_project:
|
|
291
|
-
rubygems_version: 2.
|
|
306
|
+
rubygems_version: 2.4.5
|
|
292
307
|
signing_key:
|
|
293
308
|
specification_version: 4
|
|
294
309
|
summary: A gem to include all the USDA nutrient data quickly into a ruby project
|
|
@@ -321,3 +336,4 @@ test_files:
|
|
|
321
336
|
- spec/support/sr25/NUT_DATA.txt
|
|
322
337
|
- spec/support/sr25/SRC_CD.txt
|
|
323
338
|
- spec/support/sr25/WEIGHT.txt
|
|
339
|
+
- spec/support/sr27.zip
|