Class: Export::Dwca::Data
- Inherits:
-
Object
- Object
- Export::Dwca::Data
- 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
-
#all_data ⇒ Object
Tempfile.
-
#biological_associations_extension ⇒ Scope?
Returning BiologicalAssociation.
-
#core_scope ⇒ Object
!params core_scope [String, ActiveRecord::Relation] String is fully formed SQL.
-
#data ⇒ Tempfile
The csv data as a tempfile.
-
#data_predicate_ids ⇒ Object
collection_object_predicate_id: [], collecting_event_predicate_id: [].
-
#eml ⇒ Tempfile
This is a stub, and only half-heartedly done.
-
#filename ⇒ String
readonly
the name of zipfile.
- #media_extension ⇒ Scope?
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#predicate_data ⇒ Object
Returns the value of attribute predicate_data.
-
#total ⇒ Object
TODO update.
-
#zipfile ⇒ Tempfile
The zipfile.
Instance Method Summary collapse
- #biological_associations_resource_relationship ⇒ Object
- #build_zip ⇒ Object
-
#cleanup ⇒ True
Close and delete all temporary files.
-
#csv ⇒ CSV
The data as a CSV object.
-
#initialize(core_scope: nil, extension_scopes: {}, predicate_extensions: {}) ⇒ Data
constructor
A new instance of Data.
-
#meta_fields ⇒ Array
id, and non-standard DwC colums are handled elsewhere.
-
#no_records? ⇒ Boolean
True if provided core_scope returns no records.
-
#package_download(download) ⇒ Download
A download instance.
- #predicate_options_present? ⇒ Boolean
Constructor Details
#initialize(core_scope: nil, extension_scopes: {}, predicate_extensions: {}) ⇒ Data
Returns a new instance of Data.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/export/dwca/data.rb', line 55 def initialize(core_scope: nil, extension_scopes: {}, predicate_extensions: {} ) raise ArgumentError, 'must pass a core_scope' if core_scope.nil? @core_scope = core_scope @biological_associations_extension = extension_scopes[:biological_associations] #! STring @media_extension = extension_scopes[:media] # = get_scope(core_scope) @data_predicate_ids = { collection_object_predicate_id: [], collecting_event_predicate_id: [] }.merge(predicate_extensions) end |
Instance Attribute Details
#all_data ⇒ Object
Returns Tempfile.
52 53 54 |
# File 'lib/export/dwca/data.rb', line 52 def all_data @all_data end |
#biological_associations_extension ⇒ Scope?
Returning BiologicalAssociation
35 36 37 |
# File 'lib/export/dwca/data.rb', line 35 def biological_associations_extension @biological_associations_extension end |
#core_scope ⇒ Object
!params core_scope [String, ActiveRecord::Relation]
String is fully formed SQL
31 32 33 |
# File 'lib/export/dwca/data.rb', line 31 def core_scope @core_scope end |
#data ⇒ Tempfile
Returns the csv data as a tempfile.
119 120 121 |
# File 'lib/export/dwca/data.rb', line 119 def data @data end |
#data_predicate_ids ⇒ Object
collection_object_predicate_id: [], collecting_event_predicate_id: []
49 50 51 |
# File 'lib/export/dwca/data.rb', line 49 def data_predicate_ids @data_predicate_ids end |
#eml ⇒ Tempfile
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/resourceMetadata
https://github.com/gbif/ipt/wiki/resourceMetadata#exemplar-datasets
TODO: reference biological_resource_extension.csv
250 251 252 |
# File 'lib/export/dwca/data.rb', line 250 def eml @eml end |
#filename ⇒ String (readonly)
the name of zipfile
422 423 424 |
# File 'lib/export/dwca/data.rb', line 422 def filename @filename end |
#media_extension ⇒ Scope?
Returns @return Image(?).
39 40 41 |
# File 'lib/export/dwca/data.rb', line 39 def media_extension @media_extension end |
#meta ⇒ Object
Returns the value of attribute meta.
25 26 27 |
# File 'lib/export/dwca/data.rb', line 25 def @meta end |
#predicate_data ⇒ Object
Returns the value of attribute predicate_data.
45 46 47 |
# File 'lib/export/dwca/data.rb', line 45 def predicate_data @predicate_data end |
#total ⇒ Object
TODO update
41 42 43 |
# File 'lib/export/dwca/data.rb', line 41 def total @total end |
#zipfile ⇒ Tempfile
Returns the zipfile.
413 414 415 |
# File 'lib/export/dwca/data.rb', line 413 def zipfile @zipfile end |
Instance Method Details
#biological_associations_resource_relationship ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/export/dwca/data.rb', line 337 def biological_associations_resource_relationship return nil if biological_associations_extension.nil? @biological_associations_resource_relationship = Tempfile.new('biological_resource_relationship.xml') content = nil if no_records? content = "\n" else content = Export::Csv::Dwc::Extension::BiologicalAssociations.csv(biological_associations_extension) end @biological_associations_resource_relationship.write(content) @biological_associations_resource_relationship.flush @biological_associations_resource_relationship.rewind @biological_associations_resource_relationship end |
#build_zip ⇒ Object
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/export/dwca/data.rb', line 394 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.tsv', all_data.path) zip.add('media.csv', media.path) if media_extension zip.add('resource_relationships.tsv', biological_associations_resource_relationship.path) if biological_associations_extension zip.add('meta.xml', .path) zip.add('eml.xml', eml.path) end t end |
#cleanup ⇒ True
Returns close and delete all temporary files.
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/export/dwca/data.rb', line 429 def cleanup zipfile.close zipfile.unlink .close .unlink eml.close eml.unlink data.close data.unlink if biological_associations_extension biological_associations_resource_relationship.close biological_associations_resource_relationship.unlink end if predicate_data.close predicate_data.unlink end all_data.close all_data.unlink true end |
#csv ⇒ CSV
Returns the data as a CSV object.
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/export/dwca/data.rb', line 99 def csv ::Export::Csv.generate_csv( core_scope.computed_columns, # TODO: check to see if we nee dthis exclude_columns: ::DwcOccurrence.excluded_columns, column_order: ::CollectionObject::DWC_OCCURRENCE_MAP.keys, # TODO: add other maps here trim_columns: true, # going to have to be optional trim_rows: false, header_converters: [:dwc_headers] ) end |
#meta_fields ⇒ Array
id, and non-standard DwC colums are handled elsewhere
359 360 361 362 363 364 |
# File 'lib/export/dwca/data.rb', line 359 def return [] if no_records? h = File.open(all_data, &:gets)&.strip&.split("\t") h&.shift h || [] end |
#no_records? ⇒ Boolean
Returns true if provided core_scope returns no records.
113 114 115 |
# File 'lib/export/dwca/data.rb', line 113 def no_records? total == 0 end |
#package_download(download) ⇒ Download
Returns a download instance.
455 456 457 458 |
# File 'lib/export/dwca/data.rb', line 455 def package_download(download) download.update!(source_file_path: zipfile.path) download end |
#predicate_options_present? ⇒ Boolean
89 90 91 |
# File 'lib/export/dwca/data.rb', line 89 def data_predicate_ids[:collection_object_predicate_id].present? || data_predicate_ids[:collecting_event_predicate_id].present? end |