Module: ExportHelper

Defined in:
frontend/app/helpers/export_helper.rb

Defined Under Namespace

Classes: ContextConverter

Instance Method Summary collapse

Instance Method Details

#csv_export_with_context(request_uri, params = {}) ⇒ Object

This is meant to be used by the search controller to be able to fill in the derived “context” column, since JSONModel can’t do that as it isn’t stored/indexed.



30
31
32
33
34
35
36
37
38
39
# File 'frontend/app/helpers/export_helper.rb', line 30

def csv_export_with_context(request_uri, params = {})
  params["dt"] = "csv"
  JSONModel::HTTP::stream(request_uri, params) do |response|
    old_csv = CSV.parse(response.body)
    new_csv = ContextConverter.new.convert_ancestors_to_context old_csv

    # convert arrays back CSV string
    return new_csv.map(&:to_csv).join
  end
end

#csv_response(request_uri, params = {}, filename_prefix = '') ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'frontend/app/helpers/export_helper.rb', line 5

def csv_response(request_uri, params = {}, filename_prefix = '')
  self.response.headers["Content-Type"] = "text/csv"
  self.response.headers["Content-Disposition"] = "attachment; filename=#{filename_prefix}#{Time.now.to_i}.csv"
  self.response.headers['Last-Modified'] = Time.now.ctime.to_s
  params["dt"] = "csv"
  self.response_body = Enumerator.new do |y|
    xml_response(request_uri, params) do |chunk, percent|
      y << chunk if !chunk.blank?
    end
  end
end

#xml_response(request_uri, params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'frontend/app/helpers/export_helper.rb', line 17

def xml_response(request_uri, params = {})
  JSONModel::HTTP::stream(request_uri, params) do |res|
    size, total = 0, res.header['Content-Length'].to_i
    res.read_body do |chunk|
      size += chunk.size
      percent = total > 0 ? ((size * 100) / total) : 0
      yield chunk, percent
    end
  end
end