Class: CommaSplice::ContentFinder
- Inherits:
-
Object
- Object
- CommaSplice::ContentFinder
- Defined in:
- lib/comma_splice/helpers/content_finder.rb
Overview
Given a file this will find the CSV content. Some files have some non-csv junk at the top
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#end_line ⇒ Object
readonly
Returns the value of attribute end_line.
-
#separator ⇒ Object
readonly
Returns the value of attribute separator.
-
#start_line ⇒ Object
readonly
Returns the value of attribute start_line.
Instance Method Summary collapse
- #find_content ⇒ Object
-
#initialize(file_contents, start_line = nil, end_line = nil, separator = ',') ⇒ ContentFinder
constructor
A new instance of ContentFinder.
- #parsed ⇒ Object
Constructor Details
#initialize(file_contents, start_line = nil, end_line = nil, separator = ',') ⇒ ContentFinder
Returns a new instance of ContentFinder.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/comma_splice/helpers/content_finder.rb', line 9 def initialize(file_contents, start_line = nil, end_line = nil, separator = ',') @file_contents = file_contents @separator = separator 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 |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
7 8 9 |
# File 'lib/comma_splice/helpers/content_finder.rb', line 7 def content @content end |
#end_line ⇒ Object (readonly)
Returns the value of attribute end_line.
7 8 9 |
# File 'lib/comma_splice/helpers/content_finder.rb', line 7 def end_line @end_line end |
#separator ⇒ Object (readonly)
Returns the value of attribute separator.
7 8 9 |
# File 'lib/comma_splice/helpers/content_finder.rb', line 7 def separator @separator end |
#start_line ⇒ Object (readonly)
Returns the value of attribute start_line.
7 8 9 |
# File 'lib/comma_splice/helpers/content_finder.rb', line 7 def start_line @start_line end |
Instance Method Details
#find_content ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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, separator).values.size > 2 end relative_end_line = @file_contents.lines[@start_line..-1].find_index do |line| Line.new(line, separator).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 ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/comma_splice/helpers/content_finder.rb', line 40 def parsed quote_chars = %w[" | ~ ^ & *] begin CSV.parse(@content.join('\n'), col_sep: separator, quote_char: quote_chars.shift, headers: :first_row, liberal_parsing: true) rescue CSV::MalformedCSVError quote_chars.empty? ? raise : retry end end |