Module: Export::Coldp

Defined in:
lib/export/coldp.rb

Overview

TODO: Explore github.com/frictionlessdata/datapackage-rb to ingest frictionless data, then each module will provide a correspond method to each field write tests to check for coverage (missing methods)

api.col.plus/datapackage github.com/frictionlessdata/datapackage-rb github.com/frictionlessdata/tableschema-rb

  • Update all files formats to use tabs

Exports to the Catalog of Life in the new “coldp” format.

Constant Summary collapse

FILETYPES =
%w{Description Name Synonym Taxon VernacularName}.freeze

Class Method Summary collapse

Class Method Details

.current_taxon_name_id(taxon_name) ⇒ Object

TaxonWorks does not keep a seperate ID for ICZN names

that differ from their original combination.  Ultimately
if it moves to use a Combination::Original method then
we can use those IDs. The present rendering is a hack.


98
99
100
# File 'lib/export/coldp.rb', line 98

def self.current_taxon_name_id(taxon_name)
  taxon_name.id.to_s + '/current'
end

.download(otu, request = nil) ⇒ Object



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

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

  ::Download.create!(
    name: "ColDP Download for #{otu.otu_name} on #{Time.now}.",
    description: 'A zip file containing CoLDP formatted data.',
    filename: name,
    source_file_path: file_path,
    request: request,
    expires: 2.days.from_now
  )
end

.download_async(otu, request = nil) ⇒ Object



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

def self.download_async(otu, request = nil)
  download = ::Download.create!(
    name: "ColDP Download for #{otu.otu_name} on #{Time.now}.",
    description: 'A zip file containing CoLDP formatted data.',
    filename: "coldp_otu_id_#{otu.id}_#{DateTime.now}.zip",
    request: request,
    expires: 2.days.from_now
  )
  
  ColdpCreateDownloadJob.perform_later(otu, download)

  download
end

.export(otu_id) ⇒ Object



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

def self.export(otu_id)
  otus = otus(otu_id)
  ref_csv = CSV.new('temp_ref_csv', col_sep: "\t")

  ref_csv << %w{ID citation	author year source details doi}

  # TODO: This will likely have to change, it is renamed on serving the file.
  zip_file_path = "/tmp/_#{SecureRandom.hex(8)}_coldp.zip"

  Zip::File.open(zip_file_path, Zip::File::CREATE) do |zipfile|
    # Synonym doesn't have source
    # Name uses different params
    (FILETYPES - ['Name']).each do |ft| 
      m = "Export::Coldp::Files::#{ft}".safe_constantize
      zipfile.get_output_stream("#{ft}.csv") { |f| f.write m.generate(otus, ref_csv) }
    end 

    zipfile.get_output_stream('Name.csv') { |f| f.write Export::Coldp::Files::Name.generate( Otu.find(otu_id), ref_csv) }

    ref_csv.rewind
    zipfile.get_output_stream('References.csv') { |f| f.write ref_csv.string }
    ref_csv.close
  end
  
  zip_file_path
end

.original_field(taxon_name) ⇒ Object

TODO - perhaps a utilities file –



90
91
92
# File 'lib/export/coldp.rb', line 90

def self.original_field(taxon_name)
  (taxon_name.type == 'Protonym') && taxon_name.is_original_name?
end

.otus(otu_id) ⇒ Scope

TODO: include options for validity, sets of tags, etc. At present otus are a mix of valid and invalid

Returns:

  • (Scope)

    should return the full set of Otus (= Taxa in CoLDP) that are to be sent.



26
27
28
29
30
31
# File 'lib/export/coldp.rb', line 26

def self.otus(otu_id)
  o = ::Otu.find(otu_id)
  return ::Otu.none if o.taxon_name_id.nil?
  a = o.taxon_name.descendants
  ::Otu.joins(:taxon_name).where(taxon_name: a) 
end