-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace non UTF-8 characters before JSON encoding
- Loading branch information
Showing
5 changed files
with
75 additions
and
1 deletion.
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module Castle | ||
class ReplaceInvalidCharacters | ||
def self.call(hash) | ||
return hash unless hash.is_a?(Hash) | ||
hash.each_with_object({}) do |(k, v), h| | ||
h[k] = if v.is_a?(Hash) | ||
call(v) | ||
elsif v.is_a?(String) | ||
v.encode('UTF-8', invalid: :replace, undef: :replace) | ||
else | ||
v | ||
end | ||
end | ||
end | ||
end | ||
end |
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 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe Castle::ReplaceInvalidCharacters do | ||
subject { described_class.call(input) } | ||
|
||
context 'when input is a string' do | ||
let(:input) { '1234' } | ||
|
||
it { is_expected.to eq input } | ||
end | ||
|
||
context 'when input is an array' do | ||
let(:input) { [1, 2, 3, '4'] } | ||
|
||
it { is_expected.to eq input } | ||
end | ||
|
||
context 'when input is a hash' do | ||
let(:input) { { user_id: 1 } } | ||
|
||
it { is_expected.to eq input } | ||
end | ||
|
||
context 'when input is nil' do | ||
let(:input) { nil } | ||
|
||
it { is_expected.to eq input } | ||
end | ||
|
||
context 'when input is a nested hash' do | ||
let(:input) { { user: { id: 1 } } } | ||
|
||
it { is_expected.to eq input } | ||
end | ||
|
||
context 'when input is a hash with invalid UTF-8 characters' do | ||
let(:input) { { user_id: "inv\xC4lid" } } | ||
|
||
it { is_expected.to eq(user_id: 'inv�lid') } | ||
end | ||
|
||
context 'when input is a nested hash with invalid UTF-8 characters' do | ||
let(:input) { { user: { id: "inv\xC4lid" } } } | ||
|
||
it { is_expected.to eq(user: { id: 'inv�lid' }) } | ||
end | ||
end |
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