Skip to content

Commit

Permalink
Merge pull request #14 from mattbeedle/sr28
Browse files Browse the repository at this point in the history
Sr28
  • Loading branch information
mattbeedle committed Apr 19, 2016
2 parents 0bb0eb3 + 04e543b commit b782069
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: ruby
rvm:
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.9.3
before_install:
Expand Down
4 changes: 4 additions & 0 deletions lib/usda-nutrient-database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def log(message, level = :debug)
def configuration
@configuration ||= UsdaNutrientDatabase::Configuration.new
end

def usda_version
@usda_version ||= configuration.usda_version
end
end

def self.configure
Expand Down
7 changes: 6 additions & 1 deletion lib/usda_nutrient_database/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module UsdaNutrientDatabase
class Configuration
attr_accessor :logger
attr_writer :perform_logging
attr_writer :perform_logging,
:usda_version

def logger
@logger ||= Logger.new(STDOUT)
Expand All @@ -10,5 +11,9 @@ def logger
def perform_logging?
@perform_logging ||= false
end

def usda_version
@usda_version || 'sr28'
end
end
end
2 changes: 1 addition & 1 deletion lib/usda_nutrient_database/import/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def import
attr_reader :directory

def extract_row(row)
build_object(apply_typecasts(row)).save
build_object(apply_typecasts(row)).save!
end

def build_object(row)
Expand Down
16 changes: 12 additions & 4 deletions lib/usda_nutrient_database/import/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Downloader

attr_reader :directory, :version

def initialize(directory = 'tmp/usda', version = 'sr27')
def initialize(directory = 'tmp/usda', version = UsdaNutrientDatabase.usda_version)
@directory = directory
@version = version
end
Expand All @@ -22,9 +22,15 @@ def download_and_unzip

def path
[
'SP2UserFiles', 'Place', '12354500', 'Data', version.upcase,
'dnload', "#{version_file}.zip"
].join('/')
'SP2UserFiles',
'Place',
'12354500',
'Data',
version == 'sr28' ? 'SR' : nil,
version.upcase,
'dnload',
"#{version_file}.zip"
].compact.join('/')
end

def download
Expand Down Expand Up @@ -66,6 +72,8 @@ def version_file
case @version
when 'sr27'
'sr27asc'
when 'sr28'
'sr28asc'
else
@version
end
Expand Down
2 changes: 1 addition & 1 deletion lib/usda_nutrient_database/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module UsdaNutrientDatabase
class Importer
attr_reader :directory, :version

def initialize(directory = 'tmp/usda', version = 'sr25')
def initialize(directory = 'tmp/usda', version = UsdaNutrientDatabase.usda_version)
@directory = directory
@version = version
end
Expand Down
13 changes: 9 additions & 4 deletions lib/usda_nutrient_database/tasks/usda_nutrient_database.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace :usda do
desc 'Import the latest USDA nutrition data'
task import: :environment do
UsdaNutrientDatabase::Importer.new('tmp/usda', 'sr27').import
UsdaNutrientDatabase::Importer.new(
'tmp/usda',
UsdaNutrientDatabase.usda_version
).import
end

[
Expand All @@ -15,11 +18,13 @@ namespace :usda do
end

def download_and_import(importer_name)
UsdaNutrientDatabase::Import::Downloader.new('tmp/usda', 'sr27').
tap do |downloader|
UsdaNutrientDatabase::Import::Downloader.new(
'tmp/usda',
UsdaNutrientDatabase.usda_version
).tap do |downloader|
downloader.download_and_unzip
"UsdaNutrientDatabase::Import::#{importer_name}".constantize.
new('tmp/usda/sr27').import
new('tmp/usda/sr28').import
downloader.cleanup
end
end
Expand Down

0 comments on commit b782069

Please sign in to comment.