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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1cc2d851a28100a977e5f1c243bb70ba4c7dc691
4
- data.tar.gz: 62f598910c7b1e57188d08fd93c1a81c8abe2efd
3
+ metadata.gz: 7ce03922c74860e2745e15d03f6c272def82a4c5
4
+ data.tar.gz: 4fb9cf73acb5c2800f954c6a2c65ff9e431d5e5e
5
5
  SHA512:
6
- metadata.gz: 14aeaa16b9758200bc3fd3f6f245c4b24ff1a05ae1a8cf55bfaa43cc0b6e04157a85997b22d2c9f79d9273c5973615afa8c630c0dba3363a7936b686aa53cba9
7
- data.tar.gz: edcf5e47cc2a01dc93b66c9913e9510aab336d7647b3b95f494222f815a6537baf10565560f83f15cafc101c33d30367ba7b1c4490ded6e673d022a3a2802574
6
+ metadata.gz: 5e03b8800a02759e631d5222cf2cdf5316c18bc0969f05763915f91624aa7ae53b959635a15f4ec633edaf0307cf03455642649875198bca3d810900a346a141
7
+ data.tar.gz: 14a2a5e859edd9af69f602ede9706108a5f723ee5b76f44bcc7f0880633853d486c1a5b4b3cfe991d006c0b5963d21e938b9cf361e9ecfeac31c2271a31acccf
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *.sqlite3
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
data/.travis.yml CHANGED
@@ -2,10 +2,9 @@ language: ruby
2
2
  rvm:
3
3
  - 2.0.0
4
4
  - 1.9.3
5
- - jruby-19mode
6
5
  before_install:
7
6
  - gem update --system
8
7
  - gem install bundler
9
8
  - gem install rake
10
9
  script:
11
- - bundle exec rspec spec/
10
+ - DB=sqlite3 bundle exec rspec spec/
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
  [![Gem
11
11
  Version](https://badge.fury.io/rb/usda-nutrient-database.png)](http://badge.fury.io/rb/usda-nutrient-database)
12
+ [![Gitter
13
+ chat](https://badges.gitter.im/mattbeedle/usda-nutrient-database.png)](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)
@@ -1,4 +1,5 @@
1
1
  require 'faraday'
2
+ require 'active_record'
2
3
  require 'usda_nutrient_database/configuration'
3
4
  require 'usda_nutrient_database/engine' if defined?(Rails)
4
5
  require 'usda_nutrient_database/food_group'
@@ -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', "#{version}.zip"
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(path).body
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: 'http://www.ars.usda.gov')
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
@@ -6,6 +6,11 @@ module UsdaNutrientDatabase
6
6
 
7
7
  private
8
8
 
9
+ def apply_typecasts(row)
10
+ row[6] = row[6].present?
11
+ row
12
+ end
13
+
9
14
  def find_or_initialize(row)
10
15
  UsdaNutrientDatabase::Food.
11
16
  find_or_initialize_by(nutrient_databank_number: row[0])
@@ -1,3 +1,3 @@
1
1
  module UsdaNutrientDatabase
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  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('tmp/usda') }
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: File.read('spec/support/sr25.zip'))
13
+ to_return(body: sr_file)
10
14
  importer.import
11
15
  end
12
16
 
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
- it { expect(UsdaNutrientDatabase::Weight.count).to eql(11) }
18
- it { expect(UsdaNutrientDatabase::Footnote.count).to eql(7) }
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 { expect(UsdaNutrientDatabase::FoodGroup.count).to eql(25) }
24
- it { expect(UsdaNutrientDatabase::Food.count).to eql(16) }
25
- it { expect(UsdaNutrientDatabase::Nutrient.count).to eql(15) }
26
- it { expect(UsdaNutrientDatabase::FoodsNutrient.count).to eql(12) }
27
- it { expect(UsdaNutrientDatabase::Weight.count).to eql(11) }
28
- it { expect(UsdaNutrientDatabase::Footnote.count).to eql(7) }
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.1.0
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: 2014-05-11 00:00:00.000000000 Z
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.2.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