Class: UsdaNutrientDatabase::Import::Downloader
- Inherits:
-
Object
- Object
- UsdaNutrientDatabase::Import::Downloader
- Defined in:
- lib/usda_nutrient_database/import/downloader.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #cleanup ⇒ Object
- #connection ⇒ Object
- #download ⇒ Object
- #download_and_unzip ⇒ Object
- #final_path ⇒ Object
-
#initialize(directory = 'tmp/usda', version = 'sr27') ⇒ Downloader
constructor
A new instance of Downloader.
- #path ⇒ Object
- #unzip ⇒ Object
- #version_file ⇒ Object
Constructor Details
#initialize(directory = 'tmp/usda', version = 'sr27') ⇒ Downloader
Returns a new instance of Downloader.
9 10 11 12 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 9 def initialize(directory = 'tmp/usda', version = 'sr27') @directory = directory @version = version end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
7 8 9 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 7 def directory @directory end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
7 8 9 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 7 def version @version end |
Instance Method Details
#cleanup ⇒ Object
14 15 16 17 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 14 def cleanup UsdaNutrientDatabase.log 'Cleaning up data' FileUtils.rm_rf(directory) end |
#connection ⇒ Object
51 52 53 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 51 def connection @connection ||= Faraday.new(url: 'https://www.ars.usda.gov') end |
#download ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 30 def download UsdaNutrientDatabase.log "Downloading USDA data version: #{version}" unless File.directory?(directory) FileUtils.mkdir_p("#{directory}/#{version}") end File.open("#{directory}/#{version}.zip", 'w+b') do |file| file.write connection.get(final_path).body end end |
#download_and_unzip ⇒ Object
19 20 21 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 19 def download_and_unzip download and unzip end |
#final_path ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 55 def final_path look_ahead = connection.head(path) if look_ahead.status == 302 raise 'No location' unless look_ahead.headers['location'] URI.parse(look_ahead.headers['location']).path else path end end |
#path ⇒ Object
23 24 25 26 27 28 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 23 def path [ 'SP2UserFiles', 'Place', '12354500', 'Data', version.upcase, 'dnload', "#{version_file}.zip" ].join('/') end |
#unzip ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 40 def unzip UsdaNutrientDatabase.log 'Unzipping data' Zip::File.open("#{directory}/#{version}.zip") do |zipfile| zipfile.each do |file| unless File.exist?("#{directory}/#{version}/#{file.name}") zipfile.extract(file, "#{directory}/#{version}/#{file.name}") end end end end |
#version_file ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/usda_nutrient_database/import/downloader.rb', line 65 def version_file case @version when 'sr27' 'sr27asc' else @version end end |