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

Raises:

  • (ArgumentError)


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

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



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

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



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

def eml
  @eml
end

- (String) filename (readonly)

the name of zipfile

Returns:

  • (String)


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

def filename
  @filename
end

- (Tempfile) meta

Returns the actual data file

Returns:

  • (Tempfile)

    the actual data file



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

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



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

def zipfile
  @zipfile
end

Instance Method Details

- (True) cleanup

Returns close and delete all temporary files

Returns:

  • (True)

    close and delete all temporary files



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

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



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

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



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

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



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

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



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

def no_records?
  total == 0
end