Module: Marty::ContentHandler
- Defined in:
- lib/marty/content_handler.rb
Constant Summary collapse
- GEN_FORMATS =
{ "csv" => ['text/csv', 'download'], "zip" => ['application/zip', 'download'], "xlsx" => ['application/vnd.ms-excel', 'download'], "html" => ['text/html', 'download'], "txt" => ['text/plain', 'inline'], "json" => ['application/json', 'download'], # hacky: default format is JSON nil => ['application/json', 'download'], }
Class Method Summary collapse
Class Method Details
.export(data, format, name) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/marty/content_handler.rb', line 19 def self.export(data, format, name) begin case format when "csv" # Somewhat hacky, if data is string => pass through as CSV. # Should generalize to other data types, not just CSV. res = data.is_a?(String) ? data : Marty::DataExporter.to_csv(data) when "xlsx" res = Marty::Xl.spreadsheet(data).to_stream.read when "zip" res = to_zip(data) when nil, "json" res, format = data.to_json, "json" when "html" res = data.to_s else res, format = {error: "Unknown format: #{format}"}.to_json, "json" end rescue => exc res, format = {error: "Failed conversion #{format}: #{exc}"}.to_json, "json" end type, disposition = GEN_FORMATS[format] return [res, type, disposition, "#{name}.#{format}"] end |
.log_and_raise(err) ⇒ Object
14 15 16 17 |
# File 'lib/marty/content_handler.rb', line 14 def self.log_and_raise(err) Marty::Util.logger.error err raise err end |