Class: StreamLines::Reading::CSV

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/stream_lines/reading/csv.rb

Constant Summary collapse

IGNORED_CSV_OPTIONS =

NOTE: (jdlubrano) I suspect that these options are not used terribly frequently, and each would require additional logic in the #each method. Rather than attempting to implement sensible solutions for these options, I am choosing to explicitly ignore them until there is enough outcry to support them.

%i[
  return_headers
  header_converters
  skip_lines
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(url, **csv_options) ⇒ CSV

Returns a new instance of CSV.



23
24
25
26
27
28
29
30
# File 'lib/stream_lines/reading/csv.rb', line 23

def initialize(url, **csv_options)
  @url = url
  @csv_options = accepted_csv_options(csv_options)
  @first_row_headers = @csv_options[:headers] == true

  encoding = @csv_options[:encoding] || Encoding.default_external
  @stream = Stream.new(url, encoding: encoding)
end

Instance Method Details

#each(&block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/stream_lines/reading/csv.rb', line 32

def each(&block)
  @stream.each_with_index do |line, i|
    next assign_first_row_headers(line) if i.zero? && first_row_headers?

    block.call(::CSV.parse_line(line, **@csv_options))
  end
end