class StreamLines::Reading::CSV

Constants

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.

Attributes

url[R]

Public Class Methods

new(url, **csv_options) click to toggle source
# File lib/stream_lines/reading/csv.rb, line 23
def initialize(url, **csv_options)
  @url = url
  @csv_options = accepted_csv_options(csv_options)

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

Public Instance Methods

each(&block) click to toggle source
# File lib/stream_lines/reading/csv.rb, line 31
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

Private Instance Methods

accepted_csv_options(csv_options) click to toggle source
# File lib/stream_lines/reading/csv.rb, line 52
def accepted_csv_options(csv_options)
  csv_options.transform_keys(&:to_sym)
             .delete_if { |key, _value| IGNORED_CSV_OPTIONS.include?(key) }
end
assign_first_row_headers(first_line) click to toggle source
# File lib/stream_lines/reading/csv.rb, line 47
def assign_first_row_headers(first_line)
  header_row = ::CSV.parse_line(first_line)
  @csv_options[:headers] = header_row
end
first_row_headers?() click to toggle source
# File lib/stream_lines/reading/csv.rb, line 43
def first_row_headers?
  @csv_options[:headers] == true
end