Class: Remi::DataSource::CsvFile

Inherits:
Object
  • Object
show all
Includes:
Remi::DataSource, DataStub
Defined in:
lib/remi/cucumber/data_source.rb,
lib/remi/data_source/csv_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Remi::DataSource

#feild_symbolizer

Methods included from Remi::DataSubject

#df=

Methods included from DataStub

#empty_stub_df, #stub_df, #stub_row_array, #stub_values

Constructor Details

#initialize(fields: {}, extractor:, csv_options: {}, logger: Remi::Settings.logger) ⇒ CsvFile

Returns a new instance of CsvFile.



17
18
19
20
21
22
# File 'lib/remi/data_source/csv_file.rb', line 17

def initialize(fields: {}, extractor:, csv_options: {}, logger: Remi::Settings.logger)
  @fields = fields
  self.extractor = extractor
  @csv_options = self.class.default_csv_options.merge(csv_options)
  @logger = logger
end

Instance Attribute Details

#csv_optionsObject (readonly)

Returns the value of attribute csv_options.



26
27
28
# File 'lib/remi/data_source/csv_file.rb', line 26

def csv_options
  @csv_options
end

#extractorObject

Returns the value of attribute extractor.



25
26
27
# File 'lib/remi/data_source/csv_file.rb', line 25

def extractor
  @extractor
end

#fieldsObject

Returns the value of attribute fields.



24
25
26
# File 'lib/remi/data_source/csv_file.rb', line 24

def fields
  @fields
end

Class Method Details

.default_csv_optionsObject



6
7
8
9
10
11
12
13
14
# File 'lib/remi/data_source/csv_file.rb', line 6

def self.default_csv_options
  CSV::DEFAULT_OPTIONS.merge({
    headers: true,
    header_converters: Remi::FieldSymbolizers[:standard],
    col_sep: ',',
    encoding: 'UTF-8',
    quote_char: '"'
  })
end

Instance Method Details

#dfObject



74
75
76
# File 'lib/remi/data_source/csv_file.rb', line 74

def df
  @dataframe ||= to_dataframe
end

#extractObject



32
33
34
# File 'lib/remi/data_source/csv_file.rb', line 32

def extract
  Array(@extractor.extract).tap { |x| raise "Multiple files not supported" if x.size > 1 }
end

#field_symbolizerObject



28
29
30
# File 'lib/remi/data_source/csv_file.rb', line 28

def field_symbolizer
  self.class.default_csv_options[:header_converters]
end

#first_lineObject



54
55
56
57
58
59
# File 'lib/remi/data_source/csv_file.rb', line 54

def first_line
  # Readline assumes \n line endings.  Strip out \r if it is a DOS file.
  @first_line ||= File.open(source_filename) do |f|
    f.readline.gsub(/\r/,'')
  end
end

#headersObject



61
62
63
# File 'lib/remi/data_source/csv_file.rb', line 61

def headers
  @headers ||= CSV.open(source_filename, 'r', source_csv_options) { |csv| csv.first }.headers
end

#source_filenameObject

Only going to support single file for now



50
51
52
# File 'lib/remi/data_source/csv_file.rb', line 50

def source_filename
  @source_filename ||= extract.first
end

#stub_headerObject



53
54
55
# File 'lib/remi/cucumber/data_source.rb', line 53

def stub_header
  @fields.keys.join(@csv_options[:col_sep])
end

#stub_row_csvObject



57
58
59
# File 'lib/remi/cucumber/data_source.rb', line 57

def stub_row_csv
  stub_row_array.join(@csv_options[:col_sep])
end

#stub_tmp_fileObject



40
41
42
# File 'lib/remi/cucumber/data_source.rb', line 40

def stub_tmp_file
  @stub_tmp_file ||= Tempfile.new('stub_tmp_file.csv').path
end

#to_dataframeObject



69
70
71
72
# File 'lib/remi/data_source/csv_file.rb', line 69

def to_dataframe
  @logger.info "Converting #{source_filename} to a dataframe"
  Daru::DataFrame.from_csv source_filename, @csv_options
end

#valid_headers?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/remi/data_source/csv_file.rb', line 65

def valid_headers?
  (fields.keys - headers).empty?
end

#write_stub_tmp_fileObject



44
45
46
47
48
49
50
51
# File 'lib/remi/cucumber/data_source.rb', line 44

def write_stub_tmp_file
  File.open(stub_tmp_file, "wb") do |file|
    file.puts stub_header
    file.puts stub_row_csv
  end

  stub_tmp_file
end