class DwcaHunter::Resource

Attributes

abbr[R]
command[R]
download_path[R]
title[R]
url[R]
uuid[R]

Public Class Methods

gunzip(file, dir = nil) click to toggle source
# File lib/dwca_hunter/resource.rb, line 15
def self.gunzip(file, dir = nil)
  Dir.chdir(dir) if dir
  `gunzip #{file}`
end
new(opts) click to toggle source
# File lib/dwca_hunter/resource.rb, line 20
def initialize(opts)
  @needs_download = (opts[:download] != false)
  @needs_unpack = (opts[:unpack] != false)
  @download_dir, @download_file = File.split(@download_path)
  prepare_path if needs_download?
end
unzip(file, dir = nil) click to toggle source
# File lib/dwca_hunter/resource.rb, line 5
def self.unzip(file, dir = nil)
  Dir.chdir(dir) if dir
  Zip::File.open(file) do |zip_file|
    zip_file.each do |entry|
      puts "Extracting #{entry.name}"
      entry.extract
    end
  end
end

Public Instance Methods

download() click to toggle source
# File lib/dwca_hunter/resource.rb, line 35
def download
  DwcaHunter::logger_write(self.object_id,
                           "Starting download of '%s'" % @url)
  percentage = 0
  if url.match(/^\s*http:\/\//)
    dlr = DwcaHunter::Downloader.new(url, @download_path)
    downloaded_length = dlr.download_with_percentage do |r|
      if r[:percentage].to_i != percentage
        percentage = r[:percentage].to_i
        msg = "Downloaded %.0f%% in %.0f seconds ETA is %.0f seconds" %
                      [percentage, r[:elapsed_time], r[:eta]]
        DwcaHunter::logger_write(self.object_id, msg)
      end
    end
    DwcaHunter::logger_write(self.object_id,
                             "Download finished, Size: %s" %
                              downloaded_length)
  else
    `curl -s #{url} > #{download_path}`
  end
end
needs_download?() click to toggle source
# File lib/dwca_hunter/resource.rb, line 27
def needs_download?
  @needs_download
end
needs_unpack?() click to toggle source
# File lib/dwca_hunter/resource.rb, line 31
def needs_unpack?
  @needs_unpack
end

Private Instance Methods

cleanup(str) click to toggle source
# File lib/dwca_hunter/resource.rb, line 59
def cleanup(str)
  str.strip!
  str.to_i.to_s == str ? str.to_i : str
end
generate_dwca() click to toggle source
# File lib/dwca_hunter/resource.rb, line 95
def generate_dwca
  gen = DarwinCore::Generator.new(File.join(@download_dir, 'dwca.tar.gz'))
  gen.add_core(@core, 'taxa.txt')
  @extensions.each_with_index do |extension, i|
    gen.add_extension(extension[:data],
                      extension[:file_name],
                      true,
                      extension[:row_type])
  end
  gen.add_meta_xml
  gen.add_eml_xml(@eml)
  gen.pack
  DwcaHunter::logger_write(self.object_id,
                           'DarwinCore Archive file is created')
end
prepare_path() click to toggle source
# File lib/dwca_hunter/resource.rb, line 64
def prepare_path
  FileUtils.rm_rf(@download_dir)
  FileUtils.mkdir_p(@download_dir)
end
unpack_bz2() click to toggle source
# File lib/dwca_hunter/resource.rb, line 69
def unpack_bz2
  DwcaHunter::logger_write(self.object_id,
                           'Unpacking a bz2 file, it might take a while...')
  Dir.chdir(@download_dir)
  `bunzip2 #{@download_file}`
end
unpack_gzip() click to toggle source
# File lib/dwca_hunter/resource.rb, line 82
def unpack_gzip
  DwcaHunter::logger_write(self.object_id,
                           'Unpacking gzip file, it might take a while...')
  self.class.gunzip(@download_file, @download_dir)
end
unpack_tar() click to toggle source
# File lib/dwca_hunter/resource.rb, line 88
def unpack_tar
  DwcaHunter::logger_write(self.object_id,
                           'Unpacking a tar file, it might take a while...')
  Dir.chdir(@download_dir)
  `tar zxvf #{@download_file}`
end
unpack_zip() click to toggle source
# File lib/dwca_hunter/resource.rb, line 76
def unpack_zip
  DwcaHunter::logger_write(self.object_id,
                           'Unpacking a zip file, it might take a while...')
  self.class.unzip(@download_file, @download_dir)
end