Module: Chronicle::ETL::Extractors::Helpers::InputReader

Included in:
CSVExtractor, JSONExtractor
Defined in:
lib/chronicle/etl/extractors/helpers/input_reader.rb

Instance Method Summary collapse

Instance Method Details

#filenamesObject

Return an array of input filenames; converts a single string to an array if necessary



10
11
12
# File 'lib/chronicle/etl/extractors/helpers/input_reader.rb', line 10

def filenames
  [@config.input].flatten.map
end

#pathnamesObject

Filenames as an array of pathnames



15
16
17
# File 'lib/chronicle/etl/extractors/helpers/input_reader.rb', line 15

def pathnames
  filenames.map { |filename| Pathname.new(filename) }
end

#read_from_files?Boolean

Whether we're reading from files

Returns:

  • (Boolean)


20
21
22
# File 'lib/chronicle/etl/extractors/helpers/input_reader.rb', line 20

def read_from_files?
  filenames.any?
end

#read_from_stdin?Boolean

Whether we're reading input from stdin

Returns:

  • (Boolean)


25
26
27
# File 'lib/chronicle/etl/extractors/helpers/input_reader.rb', line 25

def read_from_stdin?
  !read_from_files? && $stdin.stat.pipe?
end

#read_inputObject

Read input sources and yield each content



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chronicle/etl/extractors/helpers/input_reader.rb', line 30

def read_input
  if read_from_files?
    pathnames.each do |pathname|
      File.open(pathname) do |file|
        yield file.read, pathname.to_path
      end
    end
  elsif read_from_stdin?
    yield $stdin.read, $stdin
  else
    raise ExtractionError, 'No input files or stdin provided'
  end
end

#read_input_as_lines(&block) ⇒ Object

Read input sources line by line



45
46
47
48
49
50
51
52
53
# File 'lib/chronicle/etl/extractors/helpers/input_reader.rb', line 45

def read_input_as_lines(&block)
  if read_from_files?
    lines_from_files(&block)
  elsif read_from_stdin?
    lines_from_stdin(&block)
  else
    raise ExtractionError, 'No input files or stdin provided'
  end
end