Skip to content

Commit e7da9cf

Browse files
author
Ron Dahlgren
committed
Follow inspect format conventions, do not override to_s
1 parent 17f344e commit e7da9cf

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

lib/serpapi/client.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,9 @@ def close
181181
@socket.close if @socket
182182
end
183183

184-
def to_s
185-
# If the api_key is set, mask it
186-
masked_api_key = if api_key && api_key.length > 4
187-
"#{api_key[0..3]}#{'*' * (api_key.length - 4)}"
188-
else
189-
api_key
190-
end
191-
"SerpApi::Client(engine: #{engine}, api_key: #{masked_api_key}, persistent: #{persistent?}, timeout: #{timeout}s)"
192-
end
193-
194184
def inspect
195-
to_s
185+
masked_key = api_key && (api_key.length > 4 ? "#{api_key[..1]}****#{api_key[-2..]}" : '****')
186+
"#<#{self.class} @engine=#{engine} @timeout=#{timeout} @persistent=#{persistent} api_key=#{masked_key}>"
196187
end
197188

198189
private

spec/serpapi/client/client_spec.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,18 @@
9696
end
9797
end
9898

99-
it 'should not expose api_key in to_s and inspect' do
100-
str = client.to_s
101-
expect(str).to include('SerpApi::Client(engine: google, api_key: ')
102-
expect(str).to_not include(ENV['SERPAPI_KEY'])
103-
99+
it 'should not expose api_key via inspect' do
104100
inspect_str = client.inspect
105-
expect(inspect_str).to include('SerpApi::Client(engine: google, api_key: ')
101+
expect(inspect_str).to include('#<SerpApi::Client @engine=google @timeout=')
106102
expect(inspect_str).to_not include(ENV['SERPAPI_KEY'])
107103
end
108104

109105
it 'should gracefully handle api_key values shorter than 5 characters' do
110106
short_key_client = SerpApi::Client.new(engine: 'google', api_key: 'abcd', timeout: 10)
111-
str = short_key_client.to_s
112-
expect(str).to include('SerpApi::Client(engine: google, api_key: abcd')
113-
114107
inspect_str = short_key_client.inspect
115-
expect(inspect_str).to include('SerpApi::Client(engine: google, api_key: abcd')
108+
expect(inspect_str).to include('#<SerpApi::Client @engine=google @timeout=')
109+
expect(inspect_str).to_not include('abcd')
110+
116111
end
117112
end
118113

0 commit comments

Comments
 (0)