-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encode serialized output to avoid encoding errors. (#44)
- Loading branch information
Showing
4 changed files
with
102 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2023, by Samuel Williams. | ||
|
||
module Console | ||
module Output | ||
class Encoder | ||
def initialize(output, encoding = ::Encoding::UTF_8) | ||
@output = output | ||
@encoding = encoding | ||
end | ||
|
||
attr :output | ||
|
||
attr :encoding | ||
|
||
def call(subject = nil, *arguments, **options, &block) | ||
subject = encode(subject) | ||
arguments = encode(arguments) | ||
options = encode(options) | ||
|
||
@output.call(subject, *arguments, **options, &block) | ||
end | ||
|
||
def encode(value) | ||
case value | ||
when String | ||
value.encode(@encoding, invalid: :replace, undef: :replace) | ||
when Array | ||
value.map{|item| encode(item)} | ||
when Hash | ||
value.transform_values{|item| encode(item)} | ||
else | ||
value | ||
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
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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2023, by Samuel Williams. | ||
|
||
require 'console/output/encoder' | ||
require 'console/capture' | ||
|
||
describe Console::Output::Encoder do | ||
let(:output) {Console::Capture.new} | ||
let(:encoder) {subject.new(output)} | ||
|
||
let(:invalid_string) {"hello \xc3\x28 world"} | ||
it "is an invalid string" do | ||
expect(invalid_string).not.to be(:valid_encoding?) | ||
end | ||
|
||
it "can fix encoding" do | ||
valid_string = encoder.encode(invalid_string) | ||
expect(valid_string).to be(:valid_encoding?) | ||
end | ||
|
||
it "can encode hashes" do | ||
invalid = {key: invalid_string} | ||
valid = encoder.encode(invalid) | ||
|
||
expect(valid[:key]).to be(:valid_encoding?) | ||
end | ||
|
||
it "can encode arrays" do | ||
invalid = [invalid_string] | ||
valid = encoder.encode(invalid) | ||
|
||
expect(valid.first).to be(:valid_encoding?) | ||
end | ||
|
||
it "ignores non-strings" do | ||
expect(encoder.encode(1)).to be == 1 | ||
end | ||
end |