Module: Support::SpreadsheetCreator

Defined in:
lib/support/spreadsheet_creator.rb

Class Method Summary collapse

Class Method Details

.absolute_path(path) ⇒ Object



44
45
46
47
# File 'lib/support/spreadsheet_creator.rb', line 44

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

.column_widthObject



26
27
28
29
30
31
32
33
34
# File 'lib/support/spreadsheet_creator.rb', line 26

def self.column_width
  {
    0 => 90,
    1 => 20,
    2 => 20,
    3 => 20,
    4 => 20
  }
end

.dirnamesObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/support/spreadsheet_creator.rb', line 49

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
    [
      "#{Dir.pwd}/data/remote_ok/*",
      "#{Dir.pwd}/data/we_work_remotely/*",
      "#{Dir.pwd}/data/jobs_rails42/*"
    ]
  end
end

.filepath(path, filename) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/support/spreadsheet_creator.rb', line 36

def self.filepath(path, filename)
  absolute_path(path).concat(
    Time.now.strftime("%Y%m%d%H%M%S")
      .concat('_')
      .concat(filename)
  )
end

.generate(base_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(base_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]
    column_width.each { |idx, width| sheet.column(idx).width = width }

    next if file.nil?

    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(base_path)
  book.write filepath(base_path, filename)
end