class CommaSplice::ContentFinder
Given a file this will find the CSV content. Some files have some non-csv junk at the top
Attributes
content[R]
end_line[R]
start_line[R]
Public Class Methods
new(file_contents, start_line = nil, end_line = nil)
click to toggle source
# File lib/comma_splice/helpers/content_finder.rb, line 9 def initialize(file_contents, start_line = nil, end_line = nil) @file_contents = file_contents if start_line && end_line # the csvs this was built for have non-csv headers @start_line = start_line @end_line = end_line @content = @file_contents.lines[@start_line..@end_line] else find_content end end
Public Instance Methods
find_content()
click to toggle source
# File lib/comma_splice/helpers/content_finder.rb, line 22 def find_content @start_line = @file_contents.lines.find_index do |line| Line.new(line).values.size > 2 end relative_end_line = @file_contents.lines[@start_line..-1].find_index do |line| Line.new(line).values.size < 2 end @end_line = if relative_end_line @start_line + relative_end_line - 1 else -1 end @content = @file_contents.lines[@start_line..@end_line] end
parsed()
click to toggle source
# File lib/comma_splice/helpers/content_finder.rb, line 40 def parsed quote_chars = %w[" | ~ ^ & *] begin CSV.parse(@content.join("\n"), quote_char: quote_chars.shift, headers: :first_row, liberal_parsing: true) rescue CSV::MalformedCSVError quote_chars.empty? ? raise : retry end end