Class: Export::Dwca::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/export/dwca/data.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::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

#initialize(core_scope: nil, extension_scopes: {}) ⇒ Data

Returns a new instance of Data.

Parameters:

  • args (Hash)


37
38
39
40
41
# File 'lib/export/dwca/data.rb', line 37

def initialize(core_scope: nil, extension_scopes: {} )
  # raise ArgumentError, 'must pass a core_scope' if !record_core_scope.kind_of?( ActiveRecord::Relation )
  @core_scope = get_scope(core_scope)
  @biological_extension_scope = extension_scopes[:biological_extension_scope] #  = get_scope(core_scope)
end

Instance Attribute Details

#biological_extension_scopeObject

Returns the value of attribute biological_extension_scope.



30
31
32
# File 'lib/export/dwca/data.rb', line 30

def biological_extension_scope
  @biological_extension_scope
end

#core_scopeObject

Returns the value of attribute core_scope.



28
29
30
# File 'lib/export/dwca/data.rb', line 28

def core_scope
  @core_scope
end

#dataTempfile

Returns the csv data as a tempfile.

Returns:

  • (Tempfile)

    the csv data as a tempfile



79
80
81
# File 'lib/export/dwca/data.rb', line 79

def data
  @data
end

#emlTempfile

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

TODO: reference biological_resource_extension.csv

Returns:

  • (Tempfile)

    metadata about this dataset



102
103
104
# File 'lib/export/dwca/data.rb', line 102

def eml
  @eml
end

#filenameString (readonly)

the name of zipfile

Returns:

  • (String)


244
245
246
# File 'lib/export/dwca/data.rb', line 244

def filename
  @filename
end

#metaObject

Returns the value of attribute meta.



24
25
26
# File 'lib/export/dwca/data.rb', line 24

def meta
  @meta
end

#totalObject

TODO update



32
33
34
# File 'lib/export/dwca/data.rb', line 32

def total
  @total
end

#zipfileTempfile

Returns the zipfile.

Returns:

  • (Tempfile)

    the zipfile



233
234
235
# File 'lib/export/dwca/data.rb', line 233

def zipfile
  @zipfile
end

Instance Method Details

#biological_resource_relationshipObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/export/dwca/data.rb', line 171

def biological_resource_relationship 
  return nil if biological_extension_scope.nil?
  @biological_resource_relationship = Tempfile.new('biological_resource_relationship.xml')

  content = nil
  if no_records?
    content = "\n"
  else
    content = ::Export::Download.generate_csv(
      biological_extension_scope.computed_columns,
      #          exclude_columns: []
      trim_columns: false,
      trim_rows: false,
      header_converters: []
    )
  end

  @biological_resource_relationship.write(content)
  @biological_resource_relationship.flush
  @biological_resource_relationship.rewind
  @biological_resource_relationship
end

#build_zipObject



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/export/dwca/data.rb', line 218

def build_zip
  t = Tempfile.new(filename)

  Zip::OutputStream.open(t) { |zos| }

  Zip::File.open(t.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
  t
end

#cleanupTrue

Returns close and delete all temporary files.

Returns:

  • (True)

    close and delete all temporary files



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/export/dwca/data.rb', line 251

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

#csvCSV

Returns the data as a CSV object.

Returns:

  • (CSV)

    the data as a CSV object



49
50
51
52
53
54
55
56
57
# File 'lib/export/dwca/data.rb', line 49

def csv
  ::Export::Download.generate_csv(
    core_scope.computed_columns,
    exclude_columns: ::DwcOccurrence.excluded_columns,
    trim_columns: true, # going to have to be optional
    trim_rows: false,
    header_converters: [:dwc_headers]
  )
end

#csv_headersArray

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

Returns:

  • (Array)

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



61
62
63
64
65
66
67
68
69
# File 'lib/export/dwca/data.rb', line 61

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

#get_scope(scope) ⇒ Object

params core_scope [String, ActiveRecord::Relation]

string is fully formed SQL


265
266
267
268
269
270
271
272
273
# File 'lib/export/dwca/data.rb', line 265

def get_scope(scope)
  if scope.kind_of?(String)
    DwcOccurrence.from('(' + scope + ') as dwc_occurrences')
  elsif scope.kind_of?(ActiveRecord::Relation)
    scope
  else
    raise ArgumentError, 'Scope is not a SQL string or ActiveRecord::Relation'
  end
end

#no_records?Boolean

Returns true if provided core_scope returns no records.

Returns:

  • (Boolean)

    true if provided core_scope returns no records



73
74
75
# File 'lib/export/dwca/data.rb', line 73

def no_records?
  total == 0
end

#package_download(download) ⇒ Download

Returns a download instance.

Parameters:

Returns:



277
278
279
280
# File 'lib/export/dwca/data.rb', line 277

def package_download(download)
  download.update!(source_file_path: zipfile.path)
  download
end