-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rake task to update records with invalid data, and remove non-b…
…rekaing spaces in CsvConverter
- Loading branch information
1 parent
8f15e8d
commit d2a3597
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
desc 'Iterate over bjson data structure and replace all non-breaking spaces with spaces and remove all  characters' | ||
task fix_invalid_data_from_imports: :environment do | ||
Report.find_each do |report| | ||
data = report.data | ||
hash_iterator(data, report.id) | ||
report.update(data: data) | ||
end | ||
end | ||
|
||
def hash_iterator(hash, report_id) | ||
keys = hash.keys | ||
keys.each do |key| | ||
value = hash[key] | ||
|
||
if value.is_a?(Hash) | ||
hash_iterator(value, report_id) | ||
elsif value.is_a?(Array) | ||
array_iterator(value, report_id) | ||
elsif value.is_a?(String) | ||
clean_string(value, report_id) | ||
end | ||
end | ||
end | ||
|
||
def array_iterator(value_array, report_id) | ||
value_array.each do |value| | ||
if value.is_a?(String) | ||
clean_string(value, report_id) | ||
elsif value.is_a?(Hash) | ||
hash_iterator(value, report_id) | ||
elsif value.is_a?(Array) | ||
array_iterator(value, report_id) | ||
end | ||
end | ||
end | ||
|
||
def clean_string(value, report_id) | ||
non_breaking_space = value =~ /\u00a0/ | ||
if non_breaking_space | ||
puts "removing non-breaking space from #{value} on Report ##{report_id}" | ||
value.gsub!(/\u00a0/," ") | ||
end | ||
|
||
invalid_character_index = value =~ /[Â]/ | ||
if invalid_character_index | ||
puts "removing  from #{value} on Report ##{report_id}" | ||
value.slice!(invalid_character_index) | ||
end | ||
end |