Class: Dwca::Packer::Data
- Inherits:
-
Object
- Object
- Dwca::Packer::Data
- 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
-
#data ⇒ Tempfile
The csv data as a tempfile.
-
#eml ⇒ Tempfile
This is a stub, and only half-heartedly done.
-
#filename ⇒ String
readonly
the name of zipfile.
-
#meta ⇒ Tempfile
The actual data file.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#total ⇒ Object
Returns the value of attribute total.
-
#zipfile ⇒ Tempfile
The zipfile.
Instance Method Summary collapse
-
#cleanup ⇒ True
Close and delete all temporary files.
-
#csv ⇒ CSV
The data as a CSV object.
-
#csv_headers ⇒ Array
Use the temporarily written, and refined, CSV file to read of the existing headers.
-
#getzip ⇒ File
The stream to use in send_data, for example.
-
#initialize(record_scope) ⇒ Data
constructor
A new instance of Data.
-
#no_records? ⇒ Boolean
True if provided scope returns no records.
Constructor Details
#initialize(record_scope) ⇒ Data
Returns a new instance of Data.
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
#data ⇒ Tempfile
Returns the csv data as a tempfile.
61 62 63 |
# File 'lib/dwca/packer.rb', line 61 def data @data 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
82 83 84 |
# File 'lib/dwca/packer.rb', line 82 def eml @eml end |
#filename ⇒ String (readonly)
the name of zipfile
200 201 202 |
# File 'lib/dwca/packer.rb', line 200 def filename @filename end |
#meta ⇒ Tempfile
Returns the actual data file.
153 154 155 |
# File 'lib/dwca/packer.rb', line 153 def @meta end |
#scope ⇒ Object
Returns the value of attribute scope.
20 21 22 |
# File 'lib/dwca/packer.rb', line 20 def scope @scope end |
#total ⇒ Object
Returns the value of attribute total.
20 21 22 |
# File 'lib/dwca/packer.rb', line 20 def total @total end |
#zipfile ⇒ Tempfile
Returns the zipfile.
193 194 195 |
# File 'lib/dwca/packer.rb', line 193 def zipfile @zipfile end |
Instance Method Details
#cleanup ⇒ True
Returns 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 .close .unlink eml.close eml.unlink data.close data.unlink true end |
#csv ⇒ CSV
Returns the data as a CSV object.
32 33 34 35 36 37 38 39 |
# File 'lib/dwca/packer.rb', line 32 def csv Export::Download.generate_csv( scope.computed_columns, trim_columns: true, trim_rows: true, header_converters: [:dwc_headers] ) end |
#csv_headers ⇒ Array
Returns 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 |
#getzip ⇒ File
Returns 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', .path) zip.add('eml.xml', eml.path) end File.read(zipfile.path) end |
#no_records? ⇒ Boolean
Returns true if provided scope returns no records.
55 56 57 |
# File 'lib/dwca/packer.rb', line 55 def no_records? total == 0 end |