Sha256: ade541b2cfcbe71f1032398236ab59146ebcabb5ad993089f7ff1c60f7762132

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

module Blazer
  class ExcelParser

    def initialize(query, columns, rows)
      @query = query
      @columns = columns
      @rows = rows
      @excel = ::Axlsx::Package.new
    end

    def export
      load_data!
      save_file!
    end

    private

    attr_reader :query, :excel

    def load_data!
      excel.workbook.add_worksheet(name: "ID #{query.id}") do |sheet|
        sheet.add_row @columns
        @rows.each do |row|
          sheet.add_row row.each_with_index.map { |v, i| v.is_a?(Time) ? blazer_time_value(@columns[i], v) : v }
        end
      end
    end

    def save_file!
      tmp_file = Tempfile.new.path
      excel.serialize(tmp_file)
      return tmp_file
    end

    def blazer_time_value(key, value)
      value_with_time_zone = (value.in_time_zone(Blazer.time_zone) rescue value)
      if key.end_with?('_date')
        value_with_time_zone.strftime("%Y/%m/%d")
      elsif key.end_with?('_time')
        value_with_time_zone.strftime("%H:%M")
      elsif data_source.local_time_suffix.any? { |s| key.ends_with?(s) }
        value.to_s.sub(" UTC", "")
      else
        value_with_time_zone
      end
    rescue
      return value
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sql-jarvis-2.1.9 lib/blazer/excel_parser.rb
sql-jarvis-2.1.8 lib/blazer/excel_parser.rb
sql-jarvis-2.1.7 lib/blazer/excel_parser.rb
sql-jarvis-2.1.6 lib/blazer/excel_parser.rb
sql-jarvis-2.1.5 lib/blazer/excel_parser.rb