Module: Export::BasicNomenclature

Defined in:
lib/export/basic_nomenclature.rb

Overview

Exports to a simple format.

Class Method Summary collapse

Class Method Details

.download(taxon_name, request = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/export/basic_nomenclature.rb', line 59

def self.download(taxon_name, request = nil)
  file_path = ::Export::BasicNomenclature.export(taxon_name.id)
  name = "basic_nomenclature_taxon_name_id_#{taxon_name.id}_#{DateTime.now}.zip"

  ::Download::BasicNomenclature.create!(
    name: "Basic nomenclature for #{taxon_name.cached} on #{Time.now}.",
    description: 'A zip file containing a simple CSV export of nomenclature.',
    filename: name,
    source_file_path: file_path,
    request: request,
    expires: 2.days.from_now
  )
end

.download_async(taxon_name, request = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/export/basic_nomenclature.rb', line 73

def self.download_async(taxon_name, request = nil)
  name = "basic_nomenclature_taxon_name_id_#{taxon_name.id}_#{DateTime.now}.zip"

  download = ::Download::BasicNomenclature.create!(
    name: "Basic nomenclature for #{taxon_name.cached} on #{Time.now}.",
    description: 'A zip file containing a simple CSV export of nomenclature.',
    filename: name,
    request: request,
    expires: 2.days.from_now
 )

  BasicNomenclatureCreateDownloadJob.perform_later(taxon_name, download)

  download
end

.export(taxon_name_id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/export/basic_nomenclature.rb', line 8

def self.export(taxon_name_id)
  t = TaxonName.find(taxon_name_id)

  zip_file_path = "/tmp/_#{SecureRandom.hex(8)}_basic_nomenclature.zip"

  Zip::File.open(zip_file_path, Zip::File::CREATE) do |zipfile|
    zipfile.get_output_stream('names.csv') { |f| f.write generate(t) }
  end

  zip_file_path
end

.generate(taxon_name) ⇒ Object



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
# File 'lib/export/basic_nomenclature.rb', line 20

def self.generate(taxon_name)
  ::CSV.generate(col_sep: "\t") do |csv|

    csv << %w{
      global_id
      order
      infraorder
      superfamily
      family
      subfamily
      genus
      species
      subspecies
      author
      year
      synonyms
    }

    taxon_name.self_and_descendants.that_is_valid.each do |t|
      a = t.ancestor_hash
      syn = t.synonyms.where('taxon_names.id != ?', t.id).collect{|s| s.cached_name_and_author_year }.join('; ')
      csv << [
        t.to_global_id.to_s,
        a['order'],
        a['infraorder'],
        a['superfamily'],
        a['family'],
        a['subfamily'],
        a['genus'],
        a['species'],
        a['subspecies'],
        t.cached_author,
        t.cached_nomenclature_date&.year,
        syn.blank? ? nil : syn,
      ]
    end
  end
end