lib/peddler/flat_file_parser.rb in peddler-2.0.0 vs lib/peddler/flat_file_parser.rb in peddler-2.0.3
- old
+ new
@@ -10,21 +10,22 @@
class FlatFileParser < SimpleDelegator
include Headers
# http://stackoverflow.com/questions/8073920/importing-csv-quoting-error-is-driving-me-nuts
OPTIONS = { col_sep: "\t", quote_char: "\x00", headers: true }.freeze
+ private_constant :OPTIONS
attr_reader :content, :summary
def initialize(res, encoding)
super(res)
scrub_body!(encoding)
extract_content_and_summary
end
def parse(&blk)
- CSV.parse(content, OPTIONS, &blk) if content
+ CSV.parse(content, OPTIONS, &blk) unless content.empty?
end
def records_count
summarize if summary
end
@@ -40,10 +41,10 @@
self.body = body.dup.force_encoding(encoding)
end
def extract_content_and_summary
@content = body.encode('UTF-8', invalid: :replace, undef: :replace)
- @summary, @content = @content.split("\n\n") if @content.include?("\n\n")
+ @summary, @content = @content.split("\n\n", 2) if @content =~ /\t\t.*\n\n/
end
def summarize
Hash[summary.split("\n\t")[1, 2].map { |line| line.split("\t\t") }]
end