Skip to content

Commit e0c020e

Browse files
authored
Merge pull request #13 from serpapi/override-client-inspect
Override client inspect
2 parents 3f8842d + eee8082 commit e0c020e

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Other versions, such as Ruby 1.9, Ruby 2.x, and JRuby, are compatible with [lega
2222

2323
### Bundler
2424
```ruby
25-
gem 'serpapi', '~> 1.0', '>= 1.0.1'
25+
gem 'serpapi', '~> 1.0', '>= 1.0.2'
2626
```
2727

2828
### Gem
@@ -1085,6 +1085,7 @@ Ruby versions validated by Github Actions:
10851085
* doc: [Github Actions.](https://github.com/serpapi/serpapi-ruby/actions/workflows/ci.yml)
10861086

10871087
## Change logs
1088+
* [2025-11-17] 1.0.2 Implement `inspect` functions for client
10881089
* [2025-07-18] 1.0.1 Add support for old Ruby versions (2.7, 3.0)
10891090
* [2025-07-01] 1.0.0 Full API support
10901091

README.md.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Other versions, such as Ruby 1.9, Ruby 2.x, and JRuby, are compatible with [lega
4242

4343
### Bundler
4444
```ruby
45-
gem 'serpapi', '~> 1.0', '>= 1.0.1'
45+
gem 'serpapi', '~> 1.0', '>= 1.0.2'
4646
```
4747

4848
### Gem
@@ -566,6 +566,7 @@ Ruby versions validated by Github Actions:
566566
* doc: [Github Actions.](https://github.com/serpapi/serpapi-ruby/actions/workflows/ci.yml)
567567

568568
## Change logs
569+
* [2025-11-17] 1.0.2 Implement `inspect` functions for client
569570
* [2025-07-18] 1.0.1 Add support for old Ruby versions (2.7, 3.0)
570571
* [2025-07-01] 1.0.0 Full API support
571572

lib/serpapi/client.rb

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

184+
def inspect
185+
masked_key = api_key && (api_key.length > 8 ? "#{api_key[..3]}****#{api_key[-4..]}" : '****')
186+
"#<#{self.class} @engine=#{engine} @timeout=#{timeout} @persistent=#{persistent} api_key=#{masked_key}>"
187+
end
188+
184189
private
185190

186191
# @param [Hash] params to merge with default parameters provided to the constructor.

lib/serpapi/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module SerpApi
22
# Current version of the gem
3-
VERSION = '1.0.1'.freeze
3+
VERSION = '1.0.2'.freeze
44
end

spec/serpapi/client/client_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@
9595
raise("wrong exception: #{e}")
9696
end
9797
end
98+
99+
it 'should not expose api_key via inspect' do
100+
inspect_str = client.inspect
101+
expect(inspect_str).to_not include(ENV['SERPAPI_KEY'])
102+
end
103+
104+
it 'should gracefully handle api_key values shorter than 8 characters' do
105+
short_key_client = SerpApi::Client.new(engine: 'google', api_key: 'abcdef', timeout: 10)
106+
inspect_str = short_key_client.inspect
107+
expect(inspect_str).to_not include('abcdef')
108+
end
98109
end
99110

100111
describe 'SerpApi client with persitency enable' do
@@ -136,4 +147,4 @@
136147
expect(client.socket).to be_nil
137148
expect(client.close).to be_nil
138149
end
139-
end
150+
end

0 commit comments

Comments
 (0)