Class: DoiExtractor::DownloadCommand
- Defined in:
- lib/doi_extractor/download_command.rb
Instance Attribute Summary
Attributes inherited from Command
#log, #options, #start_time, #std_out, #user_input_callback
Instance Method Summary collapse
Methods inherited from Command
Constructor Details
This class inherits a constructor from DoiExtractor::Command
Instance Method Details
#_execute ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/doi_extractor/download_command.rb', line 4 def _execute say('retrieving DOI extract', true) doi_extract = ipums_client.get_doi_extract(.extract_group_id) unless doi_extract fail("no DOI extract with ID #{id} found") end unless doi_extract.all_data_available fail('The specified DOI extract group is not ready to download') end location = DownloadLocation.new(.download_base_path, doi_extract) if .force say('skipping user confirmation', true) else i = ask("download DOI extract files to #{location.path}?") if i.to_s.downcase[0...1] != 'y' fail('download cancelled') end end location.ensure! doi_extract.extract_requests.each_with_index do |er, idx| if location.complete_extract_request?(er) say("ER #{er.id} already downloaded") else say_nb("ER #{er.id} downloading (#{idx + 1} of #{doi_extract.extract_requests.count})...") location.clean_tmp!(er) er.files.each do |f| save_path = File.join(location.extract_request_tmp_path(er), f.filename) ipums_client.download_extract_file(save_path, er.id, f.ext) # Special handling of ddi files if f.ext.downcase == 'xml' FileUtils.cp(save_path, location.ddi_path) end end say(location.package_tmp_to_extract(er), true) say("Complete") end end location.clean_tmp! codebook_er = doi_extract.codebook_extract full_ddi = codebook_er ? codebook_er.files.detect { |f| f.ext == 'xml' } : nil if full_ddi.nil? fail('Missing full DDI codebook') end ipums_client.download_extract_file(location.complete_ddi_path, codebook_er.id, 'xml') # Write summary file to output location File.open(location.summary_file_path, 'w') do |f| f.puts "File generated on #{Time.now.strftime('%c')}" f.puts f.puts "Extract Request Group ID #{doi_extract.id}" f.puts "DOI Version v#{doi_extract.doi_version}" f.puts f.puts "Citation:" f.puts doi_extract.citation f.puts f.puts 'Included Extracts:' write_extract_summary(f, location, doi_extract) end location.remove_tmp end |
#write_extract_summary(file, location, doi_extract) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/doi_extractor/download_command.rb', line 82 def write_extract_summary(file, location, doi_extract) headers = ['ID', 'Sample', 'Archive Filename', 'Data Filename'] spacer = 4 extract_data = doi_extract.extract_requests.map do |er| [er.id.to_s, location.extract_request_name(er), location.extract_request_filename(er), er.extract_file_name] end max_lengths = (extract_data + [headers]).reduce([0, 0, 0, 0]) do |lengths, data| lengths.zip(data).map { |l, d| [l, d.length].max } end fmt = "%-#{max_lengths.first + spacer}s" max_lengths[1..-1].each do |l| fmt << "%#{l + spacer}s" end file.puts sprintf(fmt, *headers) extract_data.each do |d| file.puts sprintf(fmt, *d) end end |