Skip to content

Commit b13a278

Browse files
committed
Fix lint issues
1 parent 86e3d1f commit b13a278

File tree

2 files changed

+57
-56
lines changed

2 files changed

+57
-56
lines changed

lib/optimizely/cmab/cmab_client.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ def fetch_decision(rule_id, user_id, attributes, cmab_uuid, timeout: MAX_WAIT_TI
7272
}]
7373
}
7474

75-
variation_id = @retry_config ?
76-
_do_fetch_with_retry(url, request_body, @retry_config, timeout) :
75+
if @retry_config
76+
_do_fetch_with_retry(url, request_body, @retry_config, timeout)
77+
else
7778
_do_fetch(url, request_body, timeout)
78-
variation_id
79+
end
7980
end
8081

8182
def _do_fetch(url, request_body, timeout)

spec/cmab_client_spec.rb

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,61 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717
#
18-
require "spec_helper"
19-
require "optimizely/cmab/cmab_client"
18+
require 'spec_helper'
19+
require 'optimizely/cmab/cmab_client'
2020

2121
describe Optimizely::CmabClient do
22-
let(:mock_http_client) { double("http_client") }
23-
let(:mock_logger) { double("logger") }
22+
let(:mock_http_client) { double('http_client') }
23+
let(:mock_logger) { double('logger') }
2424
let(:retry_config) { Optimizely::CmabRetryConfig.new(max_retries: 3, initial_backoff: 0.01, max_backoff: 1, backoff_multiplier: 2) }
2525
let(:client) { described_class.new(http_client: mock_http_client, logger: mock_logger, retry_config: nil) }
26-
let(:rule_id) { "test_rule" }
27-
let(:user_id) { "user123" }
28-
let(:attributes) { { 'attr1': "value1", 'attr2': "value2" } }
29-
let(:cmab_uuid) { "uuid-1234" }
26+
let(:rule_id) { 'test_rule' }
27+
let(:user_id) { 'user123' }
28+
let(:attributes) { {'attr1': 'value1', 'attr2': 'value2'} }
29+
let(:cmab_uuid) { 'uuid-1234' }
3030
let(:expected_url) { "https://prediction.cmab.optimizely.com/predict/#{rule_id}" }
3131
let(:expected_body) do
3232
{
3333
instances: [{
3434
visitorId: user_id,
3535
experimentId: rule_id,
3636
attributes: [
37-
{ id: "attr1", value: "value1", type: "custom_attribute" },
38-
{ id: "attr2", value: "value2", type: "custom_attribute" },
37+
{id: 'attr1', value: 'value1', type: 'custom_attribute'},
38+
{id: 'attr2', value: 'value2', type: 'custom_attribute'},
3939
],
40-
cmabUUID: cmab_uuid,
41-
}],
40+
cmabUUID: cmab_uuid
41+
}]
4242
}
4343
end
44-
let(:expected_headers) { { "Content-Type" => "application/json" } }
44+
let(:expected_headers) { {'Content-Type' => 'application/json'} }
4545

46-
it "should return the variation id on success without retrying" do
47-
mock_response = double("response", status_code: 200, json: { "predictions" => [{ 'variationId': "abc123" }] })
46+
it 'should return the variation id on success without retrying' do
47+
mock_response = double('response', status_code: 200, json: {'predictions' => [{'variationId': 'abc123'}]})
4848
allow(mock_http_client).to receive(:post).and_return(mock_response)
4949
result = client.fetch_decision(rule_id, user_id, attributes, cmab_uuid)
50-
expect(result).to eq("abc123")
50+
expect(result).to eq('abc123')
5151
expect(mock_http_client).to have_received(:post).with(
5252
expected_url,
5353
hash_including(
5454
json: expected_body,
5555
headers: expected_headers,
56-
timeout: 10,
56+
timeout: 10
5757
)
5858
)
5959
end
6060

61-
it "should return HTTP exception without retrying" do
62-
allow(mock_http_client).to receive(:post).and_raise(StandardError.new("Connection error"))
61+
it 'should return HTTP exception without retrying' do
62+
allow(mock_http_client).to receive(:post).and_raise(StandardError.new('Connection error'))
6363
allow(mock_logger).to receive(:error)
6464
expect do
6565
client.fetch_decision(rule_id, user_id, attributes, cmab_uuid)
6666
end.to raise_error(Optimizely::CmabFetchError, /Connection error/)
6767
expect(mock_http_client).to have_received(:post).once
68-
expect(mock_logger).to have_received(:error).with(a_string_including("Connection error"))
68+
expect(mock_logger).to have_received(:error).with(a_string_including('Connection error'))
6969
end
7070

71-
it "should not return 200 status without retrying" do
72-
mock_response = double("response", status_code: 500, json: nil)
71+
it 'should not return 200 status without retrying' do
72+
mock_response = double('response', status_code: 500, json: nil)
7373
allow(mock_http_client).to receive(:post).and_return(mock_response)
7474

7575
expect do
@@ -81,15 +81,15 @@
8181
hash_including(
8282
json: expected_body,
8383
headers: expected_headers,
84-
timeout: 10,
84+
timeout: 10
8585
)
8686
)
87-
expect(mock_logger).to have_received(:error).with(a_string_including("500"))
87+
expect(mock_logger).to have_received(:error).with(a_string_including('500'))
8888
end
8989

90-
it "should return invalid json without retrying" do
91-
mock_response = double("response", status_code: 200)
92-
allow(mock_response).to receive(:json).and_raise(JSON::ParserError.new("Expecting value"))
90+
it 'should return invalid json without retrying' do
91+
mock_response = double('response', status_code: 200)
92+
allow(mock_response).to receive(:json).and_raise(JSON::ParserError.new('Expecting value'))
9393
allow(mock_http_client).to receive(:post).and_return(mock_response)
9494

9595
expect do
@@ -101,14 +101,14 @@
101101
hash_including(
102102
json: expected_body,
103103
headers: expected_headers,
104-
timeout: 10,
104+
timeout: 10
105105
)
106106
)
107-
expect(mock_logger).to have_received(:error).with(a_string_including("Invalid CMAB fetch response"))
107+
expect(mock_logger).to have_received(:error).with(a_string_including('Invalid CMAB fetch response'))
108108
end
109109

110-
it "should return invalid response structure without retrying" do
111-
mock_response = double("response", status_code: 200, json: { "no_predictions" => [] })
110+
it 'should return invalid response structure without retrying' do
111+
mock_response = double('response', status_code: 200, json: {'no_predictions' => []})
112112
allow(mock_http_client).to receive(:post).and_return(mock_response)
113113

114114
expect do
@@ -120,48 +120,48 @@
120120
hash_including(
121121
json: expected_body,
122122
headers: expected_headers,
123-
timeout: 10,
123+
timeout: 10
124124
)
125125
)
126-
expect(mock_logger).to have_received(:error).with(a_string_including("Invalid CMAB fetch response"))
126+
expect(mock_logger).to have_received(:error).with(a_string_including('Invalid CMAB fetch response'))
127127
end
128128

129-
it "should return the variation id on first try with retry config but no retry needed" do
129+
it 'should return the variation id on first try with retry config but no retry needed' do
130130
client_with_retry = described_class.new(
131131
http_client: mock_http_client,
132132
logger: mock_logger,
133-
retry_config: retry_config,
133+
retry_config: retry_config
134134
)
135135

136136
# Mock successful response
137-
mock_response = double("response", status_code: 200, json: { "predictions" => [{ 'variationId': "abc123" }] })
137+
mock_response = double('response', status_code: 200, json: {'predictions' => [{'variationId': 'abc123'}]})
138138
allow(mock_http_client).to receive(:post).and_return(mock_response)
139139
allow_any_instance_of(Object).to receive(:sleep)
140140

141141
result = client_with_retry.fetch_decision(rule_id, user_id, attributes, cmab_uuid)
142142

143-
expect(result).to eq("abc123")
143+
expect(result).to eq('abc123')
144144
expect(mock_http_client).to have_received(:post).with(
145145
expected_url,
146146
hash_including(
147147
json: expected_body,
148148
headers: expected_headers,
149-
timeout: 10,
149+
timeout: 10
150150
)
151151
).once
152152
expect_any_instance_of(Object).not_to have_received(:sleep)
153153
end
154154

155-
it "should return the variation id on third try with retry config" do
155+
it 'should return the variation id on third try with retry config' do
156156
client_with_retry = described_class.new(
157157
http_client: mock_http_client,
158158
logger: mock_logger,
159-
retry_config: retry_config,
159+
retry_config: retry_config
160160
)
161161

162162
# Create failure and success responses
163-
failure_response = double("response", status_code: 500)
164-
success_response = double("response", status_code: 200, json: { "predictions" => [{ 'variationId': "xyz456" }] })
163+
failure_response = double('response', status_code: 500)
164+
success_response = double('response', status_code: 200, json: {'predictions' => [{'variationId': 'xyz456'}]})
165165

166166
# First two calls fail, third succeeds
167167
call_sequence = [failure_response, failure_response, success_response]
@@ -172,7 +172,7 @@
172172

173173
result = client_with_retry.fetch_decision(rule_id, user_id, attributes, cmab_uuid)
174174

175-
expect(result).to eq("xyz456")
175+
expect(result).to eq('xyz456')
176176
expect(mock_http_client).to have_received(:post).exactly(3).times
177177

178178
# Verify all HTTP calls used correct parameters
@@ -181,28 +181,28 @@
181181
hash_including(
182182
json: expected_body,
183183
headers: expected_headers,
184-
timeout: 10,
184+
timeout: 10
185185
)
186186
)
187187

188188
# Verify retry logging
189-
expect(mock_logger).to have_received(:info).with("Retrying CMAB request (attempt 1) after 0.01 seconds...")
190-
expect(mock_logger).to have_received(:info).with("Retrying CMAB request (attempt 2) after 0.02 seconds...")
189+
expect(mock_logger).to have_received(:info).with('Retrying CMAB request (attempt 1) after 0.01 seconds...')
190+
expect(mock_logger).to have_received(:info).with('Retrying CMAB request (attempt 2) after 0.02 seconds...')
191191

192192
# Verify sleep was called with correct backoff times
193193
expect_any_instance_of(Object).to have_received(:sleep).with(0.01)
194194
expect_any_instance_of(Object).to have_received(:sleep).with(0.02)
195195
end
196196

197-
it "should exhausts all retry attempts" do
197+
it 'should exhausts all retry attempts' do
198198
client_with_retry = described_class.new(
199199
http_client: mock_http_client,
200200
logger: mock_logger,
201-
retry_config: retry_config,
201+
retry_config: retry_config
202202
)
203203

204204
# Create failure response
205-
failure_response = double("response", status_code: 500)
205+
failure_response = double('response', status_code: 500)
206206

207207
# All attempts fail
208208
allow(mock_http_client).to receive(:post).and_return(failure_response)
@@ -218,16 +218,16 @@
218218
expect(mock_http_client).to have_received(:post).exactly(4).times
219219

220220
# Verify retry logging
221-
expect(mock_logger).to have_received(:info).with("Retrying CMAB request (attempt 1) after 0.01 seconds...")
222-
expect(mock_logger).to have_received(:info).with("Retrying CMAB request (attempt 2) after 0.02 seconds...")
223-
expect(mock_logger).to have_received(:info).with("Retrying CMAB request (attempt 3) after 0.08 seconds...")
221+
expect(mock_logger).to have_received(:info).with('Retrying CMAB request (attempt 1) after 0.01 seconds...')
222+
expect(mock_logger).to have_received(:info).with('Retrying CMAB request (attempt 2) after 0.02 seconds...')
223+
expect(mock_logger).to have_received(:info).with('Retrying CMAB request (attempt 3) after 0.08 seconds...')
224224

225225
# Verify sleep was called for each retry
226226
expect_any_instance_of(Object).to have_received(:sleep).with(0.01)
227227
expect_any_instance_of(Object).to have_received(:sleep).with(0.02)
228228
expect_any_instance_of(Object).to have_received(:sleep).with(0.08)
229229

230230
# Verify final error logging
231-
expect(mock_logger).to have_received(:error).with(a_string_including("Max retries exceeded for CMAB request"))
231+
expect(mock_logger).to have_received(:error).with(a_string_including('Max retries exceeded for CMAB request'))
232232
end
233233
end

0 commit comments

Comments
 (0)