Class: Dwca::Packer::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/dwca/packer.rb

Overview

Wrapper to build DWCA zipfiles for a specific project. See tasks/accesssions/report/dwc_controller.rb for use.

With help from thinkingeek.com/2013/11/15/create-temporary-zip-file-send-response-rails/

Usage:

begin
 data = Dwca::Packer::Data.new(DwcOccurrence.where(project_id: sessions_current_project_id)
ensure
 data.cleanup
end

Always use the ensure/data.cleanup pattern!

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Data) initialize(record_scope)

Returns a new instance of Data

Parameters:

  • args (Hash)

Raises:

  • (ArgumentError)


24
25
26
27
28
# File 'lib/dwca/packer.rb', line 24

def initialize(record_scope)
  raise ArgumentError, 'must pass a scope' if !record_scope.kind_of?( ActiveRecord::Relation )
  @scope = record_scope
  @total = scope.count('*')
end

Instance Attribute Details

- (Tempfile) data

Returns the csv data as a tempfile

Returns:

  • (Tempfile)

    the csv data as a tempfile



61
62
63
# File 'lib/dwca/packer.rb', line 61

def data
  @data
end

- (Tempfile) eml

This is a stub, and only half-heartedly done. You should be using IPT for the time being. See also

https://github.com/gbif/ipt/wiki/
https://github.com/gbif/ipt/wiki/#exemplar-datasets

Returns:

  • (Tempfile)

    metadata about this dataset



82
83
84
# File 'lib/dwca/packer.rb', line 82

def eml
  @eml
end

- (String) filename (readonly)

the name of zipfile

Returns:

  • (String)


200
201
202
# File 'lib/dwca/packer.rb', line 200

def filename
  @filename
end

- (Tempfile) meta

Returns the actual data file

Returns:

  • (Tempfile)

    the actual data file



153
154
155
# File 'lib/dwca/packer.rb', line 153

def meta
  @meta
end

- (Object) scope

Returns the value of attribute scope



20
21
22
# File 'lib/dwca/packer.rb', line 20

def scope
  @scope
end

- (Object) total

Returns the value of attribute total



20
21
22
# File 'lib/dwca/packer.rb', line 20

def total
  @total
end

- (Tempfile) zipfile

Returns the zipfile

Returns:

  • (Tempfile)

    the zipfile



193
194
195
# File 'lib/dwca/packer.rb', line 193

def zipfile
  @zipfile
end

Instance Method Details

- (True) cleanup

Returns close and delete all temporary files

Returns:

  • (True)

    close and delete all temporary files



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/dwca/packer.rb', line 207

def cleanup
  zipfile.close
  zipfile.unlink
  meta.close
  meta.unlink
  eml.close
  eml.unlink
  data.close
  data.unlink
  true
end

- (CSV) csv

Returns the data as a CSV object

Returns:

  • (CSV)

    the data as a CSV object



32
33
34
35
36
37
38
39
# File 'lib/dwca/packer.rb', line 32

def csv
  Download.generate_csv(
    scope.computed_columns,
    trim_columns: true,
    trim_rows: true,
    header_converters: [:dwc_headers]
  )
end

- (Array) csv_headers

Returns use the temporarily written, and refined, CSV file to read of the existing headers

Returns:

  • (Array)

    use the temporarily written, and refined, CSV file to read of the existing headers



43
44
45
46
47
48
49
50
51
# File 'lib/dwca/packer.rb', line 43

def csv_headers
  return [] if no_records?
  d = CSV.open(data, headers: true, col_sep: "\t")
  d.read
  h = d.headers
  d.rewind
  h.shift # get rid of id, it's special in meta
  h
end

- (File) getzip

Returns the stream to use in send_data, for example

Returns:

  • (File)

    the stream to use in send_data, for example



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/dwca/packer.rb', line 179

def getzip
  Zip::OutputStream.open(zipfile) { |zos| }

  Zip::File.open(zipfile.path, Zip::File::CREATE) do |zip|
    zip.add('data.csv', data.path)
    zip.add('meta.xml', meta.path)
    zip.add('eml.xml', eml.path)
  end

  File.read(zipfile.path)
end

- (Boolean) no_records?

Returns true if provided scope returns no records

Returns:

  • (Boolean)

    true if provided scope returns no records



55
56
57
# File 'lib/dwca/packer.rb', line 55

def no_records?
  total == 0
end