Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encoding on parsing CSV file #38

Merged
merged 1 commit into from
May 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/csv_importer/csv_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ class CSVReader
attribute :file # IO
attribute :path, String
attribute :quote_char, String, default: '"'
attribute :encoding, String, default: 'UTF-8:UTF-8'

def csv_rows
@csv_rows ||= begin
sane_content = sanitize_content(read_content)
separator = detect_separator(sane_content)
cells = CSV.parse(sane_content, col_sep: separator, quote_char: quote_char, skip_blanks: true)
cells = CSV.parse(sane_content, col_sep: separator, quote_char: quote_char, skip_blanks: true, encoding: encoding)
sanitize_cells(cells)
end
end
Expand Down Expand Up @@ -43,8 +44,9 @@ def read_content
end

def sanitize_content(csv_content)
internal_encoding = encoding.split(':').last
csv_content
.encode(Encoding.find('UTF-8'), {invalid: :replace, undef: :replace, replace: ''}) # Remove invalid byte sequences
.encode(Encoding.find(internal_encoding), {invalid: :replace, undef: :replace, replace: ''}) # Remove invalid byte sequences
.gsub(/\r\r?\n?/, "\n") # Replaces windows line separators with "\n"
end

Expand Down
5 changes: 5 additions & 0 deletions spec/csv_importer/csv_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@ module CSVImporter
reader = CSVReader.new(content: "first_name,last_name\n'bob','the builder'", quote_char: "'")
expect(reader.rows).to eq [["bob", "the builder"]]
end

it "supports custom encoding" do
reader = CSVReader.new(content: "メール,氏名".encode('SJIS'), encoding: 'SJIS:UTF-8')
expect(reader.header).to eq ["メール", "氏名"]
end
end
end