Module: Support::SpreadsheetCreator

Defined in:
lib/support/spreadsheet_creator.rb

Class Method Summary collapse

Class Method Details

.absolute_path(path) ⇒ Object



26
27
28
29
# File 'lib/support/spreadsheet_creator.rb', line 26

def self.absolute_path(path)
  return path unless ENV['RAILS_ENV'] == 'test'
  "#{RemoteJobScraper.root}/spec/fixtures/#{path}"
end

.dirnamesObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/support/spreadsheet_creator.rb', line 31

def self.dirnames
  if ENV['RAILS_ENV'] == 'test'
    [
      "#{RemoteJobScraper.root}/spec/fixtures/data/remote_ok/*",
      "#{RemoteJobScraper.root}/spec/fixtures/data/we_work_remotely/*",
      "#{RemoteJobScraper.root}/spec/fixtures/data/jobs_rails42/*"
    ]
  else
    [
      "#{RemoteJobScraper.root}/data/remote_ok/*",
      "#{RemoteJobScraper.root}/data/we_work_remotely/*",
      "#{RemoteJobScraper.root}/data/jobs_rails42/*"
    ]
  end
end

.generate(path: 'data/summary/', filename: 'remote_job_summary_.xls') ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/support/spreadsheet_creator.rb', line 6

def self.generate(path: 'data/summary/', filename: 'remote_job_summary_.xls')
  Spreadsheet.client_encoding = 'UTF-8'
  book = Spreadsheet::Workbook.new

  dirnames.each_with_index do |dirname, index|
    file = Dir.glob(dirname).first
    sheet = book.create_worksheet name: dirname.split("/")[-2]

    CSV.foreach(file).with_index(0) do |row, index|
      sheet.row(index).push(*(Spreadsheet::Link.new row[0]), *row[1..-1])
    end
  end

  FileUtils.mkdir_p absolute_path(path)

  book.write absolute_path(path).concat(
    Time.now.strftime("%Y%m%d%H%M%S").concat('_remote_jobs_summary.xls')
  )
end