Permalink
Browse files

Fix encoding problem

  • Loading branch information...
ngiger committed Jun 1, 2017
1 parent b837ce5 commit 6925031adce2e289bc46a760539007af8fea7c7f
Showing with 22 additions and 13 deletions.
  1. +3 −7 Gemfile
  2. +2 −2 Gemfile.lock
  3. +17 −4 lib/bbmb/util/csv_importer.rb
View
10 Gemfile
@@ -3,15 +3,11 @@ source 'https://rubygems.org'
gem 'odba'
gem 'rake'
gem 'oga'
gem 'bbmb', '>= 2.2.8'
gem 'bbmb', '>= 2.2.9'
group :debugger do
if RUBY_VERSION.match(/^1/)
gem 'pry-debugger'
else
gem 'pry-byebug'
gem 'pry-doc'
end
gem 'pry-byebug'
gem 'pry-doc'
end
group :test do
View
@@ -3,7 +3,7 @@ GEM
specs:
ansi (1.5.0)
ast (2.3.0)
bbmb (2.2.8)
bbmb (2.2.9)
deprecated (= 2.0.1)
hpricot
htmlgrid
@@ -134,7 +134,7 @@ PLATFORMS
ruby
DEPENDENCIES
bbmb (>= 2.2.8)
bbmb (>= 2.2.9)
flexmock
minitest
odba
@@ -14,9 +14,18 @@ def import(io, persistence=BBMB.persistence)
#CSV.parse(io, ";") { |record|
#FasterCSV.parse(io, :col_sep => ";", :row_sep => "\n") { |record|
#CSVParser.parse(io, false, ';').each { |record|
io = io.split("\n") if io.is_a?(String)
if io.is_a?(String)
begin
lines = io.split("\n")
rescue
lines = io.force_encoding('ISO-8859-1').split("\n")
end
else
lines = io
end
count = 0
io.each { |line|
lines.each { |line|
record = line.split(';')
if(object = import_record(record))
persistence.save(object)
@@ -30,8 +39,12 @@ def postprocess(persistence=BBMB.persistence)
end
def string(str)
return nil unless str
str = str.encode('utf-8')
str.gsub(/\s+/, ' ') unless str.empty?
begin
encoded = str.encode('utf-8')
rescue => error
encoded = str.force_encoding('ISO-8859-1').encode('utf-8')
end
encoded.gsub(/\s+/, ' ') unless encoded.empty?
end
end
class CustomerImporter < CsvImporter

0 comments on commit 6925031

Please sign in to comment.