Skip to content

Commit 33f6e18

Browse files
Linting fixes based on feedback from Rubocop
1 parent 0cb6860 commit 33f6e18

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/openai/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def self.to_json(string)
101101
# data: {JSON}
102102
# ...
103103
# data: [DONE]
104-
104+
105105
# Only call the user_proc if the chunk contains a JSON object.
106106
chunk.scan(/data: (\{.*\})/i).flatten.each do |data|
107107
user_proc.call(JSON.parse(data), bytesize)

spec/openai/client/client_spec.rb

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,39 @@
6969
end
7070

7171
context "when called with a string containing that looks like a JSON object but is invalid" do
72-
let(:chunk) {
72+
let(:chunk) do
7373
<<-CHUNK
7474
data: { "foo": "bar" }
7575
data: { BAD ]:-> JSON }
7676
CHUNK
77-
}
77+
end
78+
79+
it "does not raise an error" do
80+
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'), nil)
81+
82+
expect do
83+
stream.call(chunk)
84+
end.to_not raise_error(JSON::ParserError)
85+
end
86+
end
87+
88+
context "when called with a string containing an error" do
89+
let(:chunk) do
90+
<<-CHUNK
91+
data: { "foo": "bar" }
92+
error: { "message": "A bad thing has happened!" }
93+
CHUNK
94+
end
7895

7996
it "does not raise an error" do
8097
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'), nil)
98+
expect(user_proc).to_not receive(:call).with(
99+
JSON.parse('{ "message": "A bad thing has happened!" }'), nil
100+
)
81101

82-
expect {
83-
stream.call(chunk)
84-
}.to_not raise_error(JSON::ParserError)
102+
expect do
103+
stream.call(chunk)
104+
end.to_not raise_error(JSON::ParserError)
85105
end
86106
end
87107
end

0 commit comments

Comments
 (0)