Class: Csvtool::Application::UseCases::RunCsvStats

Inherits:
Object
  • Object
show all
Defined in:
lib/csvtool/application/use_cases/run_csv_stats.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(scanner: Infrastructure::CSV::CsvStatsScanner.new, csv_stats_file_writer: Infrastructure::Output::CsvStatsFileWriter.new) ⇒ RunCsvStats

Returns a new instance of RunCsvStats.



17
18
19
20
21
22
23
# File 'lib/csvtool/application/use_cases/run_csv_stats.rb', line 17

def initialize(
  scanner: Infrastructure::CSV::CsvStatsScanner.new,
  csv_stats_file_writer: Infrastructure::Output::CsvStatsFileWriter.new
)
  @scanner = scanner
  @csv_stats_file_writer = csv_stats_file_writer
end

Instance Method Details

#call(session:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/csvtool/application/use_cases/run_csv_stats.rb', line 25

def call(session:)
  path = session.source.path
  return failure(:file_not_found, path: path) unless File.file?(path)

  stats = @scanner.call(
    file_path: path,
    col_sep: session.source.separator,
    headers_present: session.source.headers_present
  )
  if session.output_destination&.file?
    @csv_stats_file_writer.call(path: session.output_destination.path, data: stats)
    return success(stats.merge(output_path: session.output_destination.path))
  end
  success(stats)
rescue CSV::MalformedCSVError
  failure(:could_not_parse_csv)
rescue Errno::EACCES => e
  if session.output_destination&.file?
    return failure(:cannot_write_output_file, path: session.output_destination.path, error_class: e.class)
  end
  failure(:cannot_read_file, path: path)
rescue Errno::ENOENT => e
  return failure(:cannot_write_output_file, path: session.output_destination.path, error_class: e.class) if session.output_destination&.file?

  failure(:cannot_read_file, path: path)
end