Skip to content

Commit 37c17ae

Browse files
authored
Merge pull request #7 from serpapi/fix-documentation
Update documentation, lint configuration, Ruby syntax style compliance
2 parents c1ddbc4 + 30bfd5b commit 37c17ae

File tree

7 files changed

+143
-89
lines changed

7 files changed

+143
-89
lines changed

README.md

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
[![serpapi-ruby](https://github.com/serpapi/serpapi-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/serpapi/serpapi-ruby/actions/workflows/ci.yml) [![Gem Version](https://badge.fury.io/rb/serpapi.svg)](https://badge.fury.io/rb/serpapi)
44

5-
Integrate search data into your AI workflow, RAG / fine tuning or ruby application using this official wrapper for [SerpApi](https://serpapi.com).
5+
Integrate search data into your AI workflow, RAG / fine-tuning, or Ruby application using this official wrapper for [SerpApi](https://serpapi.com).
66

7-
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and [more.](https://serpapi.com).
7+
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and [more](https://serpapi.com).
88

9-
Fast query at scale a vast range of data, including web search results, flight schedule, stock market data, news headlines, and [more.](https://serpapi.com).
9+
Query a vast range of data at scale, including web search results, flight schedules, stock market data, news headlines, and [more](https://serpapi.com).
1010

1111
## Features
1212
* `persistent` → Keep socket connection open to save on SSL handshake / reconnection (2x faster). [Search at scale](#Search-At-Scale)
@@ -16,14 +16,13 @@ Fast query at scale a vast range of data, including web search results, flight s
1616

1717
## Installation
1818

19-
To achieve optimal performance, it is essential to latest Ruby version installed on your system (Ruby 2.7+ is supported by 3.4 is recommended for [performance reason](#Performance-Comparison)).
20-
21-
| Older versions such as Ruby 1.9, 2.x, and JRuby are compatible with [serpapi older library](https://github.com/serpapi/google-search-results-ruby), which continues to function effectively. see [migration guide](#Migration-quick-guide) if you are using the older library.
19+
Ruby 2.7 and later are supported. To achieve an optimal performance, the latest version is recommended. Check 2.7.8 vs 3.4.4 [performance comparison](#Performance-Comparison).
2220

21+
Other versions, such as Ruby 1.9, Ruby 2.x, and JRuby, are compatible with [legacy SerpApi library](https://github.com/serpapi/google-search-results-ruby), which is still supported. To upgrade to the latest library, check our [migration guide](#Migration-quick-guide).
2322

2423
### Bundler
2524
```ruby
26-
gem 'serpapi', '~> 1.0.0'
25+
gem 'serpapi', '~> 1.0', '>= 1.0.1'
2726
```
2827

2928
### Gem
@@ -55,7 +54,7 @@ Environment variables are a secure, safe, and easy way to manage secrets.
5554
## Search API advanced usage with Google search engine
5655

5756
This example dives into all the available parameters for the Google search engine.
58-
The set of parameters is extensive and depends on the search engine you choose.
57+
The list of parameters depends on the chosen search engine.
5958

6059
```ruby
6160
# load gem
@@ -66,15 +65,15 @@ client = SerpApi::Client.new(
6665
engine: 'google',
6766
api_key: ENV['SERPAPI_KEY'],
6867
# HTTP client configuration
69-
async: false, # non blocking HTTP request see: Search Asynchronous (default: false)
68+
async: false, # non-blocking HTTP request see: Search Asynchronous (default: false)
7069
persistent: true, # leave socket connection open for faster response time see: Search at scale (default: true)
7170
timeout: 5, # HTTP timeout in seconds on the client side only. (default: 120s)
72-
symbolize_names: true # turn on/off JSON keys to symbols (default: on more efficient)
71+
symbolize_names: true # turn on/off JSON keys to symbols (default: on, more efficient)
7372
)
7473

7574
# search query overview (more fields available depending on search engine)
7675
params = {
77-
# overview of parameter for Google search engine which one of many search engine supported.
76+
# overview of parameter for Google search engine which is one of many search engine supported.
7877
# select the search engine (full list: https://serpapi.com/)
7978
engine: "google",
8079
# actual search query
@@ -126,11 +125,11 @@ Here is an example of asynchronous searches using Ruby
126125
require 'serpapi'
127126

128127
company_list = %w[meta amazon apple netflix google]
129-
client = SerpApi::Client.new(engine: 'google', async: true, persistent: true, api_key: ENV.fetch('SERPAPI_KEY', nil))
128+
client = SerpApi::Client.new(engine: 'google', async: true, persistent: true, api_key: ENV['SERPAPI_KEY'])
130129
schedule_search = Queue.new
131130
result = nil
132131
company_list.each do |company|
133-
result = client.search({ q: company })
132+
result = client.search(q: company)
134133
puts "#{company}: search results found in cache for: #{company}" if result[:search_metadata][:status] =~ /Cached/
135134

136135
schedule_search.push(result[:search_metadata][:id])
@@ -163,8 +162,7 @@ puts 'done'
163162

164163
* source code: [demo/demo_async.rb](https://github.com/serpapi/serpapi-ruby/blob/master/demo/demo_async.rb)
165164

166-
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)).
167-
Each search takes a few seconds before completion by SerpApi service and the search engine. By the time the first element pops out of the queue. The search result might be already available in the archive. If not, the `search_archive` method blocks until the search results are available.
165+
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)). Each search may take up to few seconds to complete. By the time the first element pops out of the queue, the search results might already be available in the archive. If not, the `search_archive` method blocks until the search results are available.
168166

169167
### Search at scale
170168
The provided code snippet is a Ruby spec test case that demonstrates the use of thread pools to execute multiple HTTP requests concurrently.
@@ -210,19 +208,11 @@ require 'serpapi'
210208

211209
require 'pp'
212210

213-
client = SerpApi::Client.new({api_key: ENV['SERPAPI_KEY']})
211+
client = SerpApi::Client.new(api_key: ENV['SERPAPI_KEY'])
214212
params = {
215213
q: 'coffee'
216214
}
217215
results = client.search(params)
218-
print results[:search_parameters][:engine]
219-
if params[:engine] != results[:search_parameters][:engine]
220-
puts "Engine mismatch: expected #{params[:engine]}, got #{results[:search_parameters][:engine]}"
221-
exit 1
222-
end
223-
puts 'search engine match'
224-
exit 0
225-
226216
unless results[:organic_results]
227217
puts 'organic results found'
228218
exit 1
@@ -239,7 +229,7 @@ exit 0
239229

240230
```ruby
241231
require 'serpapi'
242-
client = SerpApi::Client.new()
232+
client = SerpApi::Client.new
243233
location_list = client.location(q: "Austin", limit: 3)
244234
puts "number of location: #{location_list.size}"
245235
pp location_list
@@ -270,7 +260,7 @@ NOTE: api_key is not required for this endpoint.
270260
This API allows retrieving previous search results.
271261
To fetch earlier results from the search_id.
272262

273-
First, you need to run a search and save the search id.
263+
First, you need to run a search and save the search ID.
274264

275265
```ruby
276266
require 'serpapi'
@@ -279,7 +269,7 @@ results = client.search(q: "Coffee", location: "Portland")
279269
search_id = results[:search_metadata][:id]
280270
```
281271

282-
Now we can retrieve the previous search results from the archive using the search id (free of charge).
272+
Now we can retrieve the previous search results from the archive using the search ID (free of charge).
283273

284274
```ruby
285275
require 'serpapi'
@@ -300,22 +290,22 @@ pp client.account
300290
It prints your account information as:
301291
```ruby
302292
{
303-
account_id: "1234567890",
304-
api_key: "your_secret_key",
305-
account_email: "[email protected]",
306-
account_status: "Active",
307-
plan_id: "free",
308-
plan_name: "Free Plan",
309-
plan_monthly_price: 0.0,
310-
searches_per_month: 100,
311-
plan_searches_left: 0,
312-
extra_credits: 100,
313-
total_searches_left: 10,
314-
this_month_usage: 100,
315-
this_hour_searches: 0,
316-
last_hour_searches: 0,
317-
account_rate_limit_per_hour: 100
318-
}
293+
account_id: "1234567890",
294+
api_key: "your_secret_key",
295+
account_email: "[email protected]",
296+
account_status: "Active",
297+
plan_id: "free",
298+
plan_name: "Free Plan",
299+
plan_monthly_price: 0.0,
300+
searches_per_month: 250,
301+
plan_searches_left: 250,
302+
extra_credits: 0,
303+
total_searches_left: 250,
304+
this_month_usage: 0,
305+
this_hour_searches: 0,
306+
last_hour_searches: 0,
307+
account_rate_limit_per_hour: 250
308+
}
319309
```
320310

321311
## Basic example per search engine

README.md.erb

Lines changed: 90 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ end
2222

2323
[![serpapi-ruby](https://github.com/serpapi/serpapi-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/serpapi/serpapi-ruby/actions/workflows/ci.yml) [![Gem Version](https://badge.fury.io/rb/serpapi.svg)](https://badge.fury.io/rb/serpapi)
2424

25-
Integrate search data into your AI workflow, RAG / fine tuning or ruby application using this official wrapper for [SerpApi](https://serpapi.com).
25+
Integrate search data into your AI workflow, RAG / fine-tuning, or Ruby application using this official wrapper for [SerpApi](https://serpapi.com).
2626

27-
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and [more.](https://serpapi.com).
27+
SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBay, App Stores, and [more](https://serpapi.com).
2828

29-
Fast query at scale a vast range of data, including web search results, flight schedule, stock market data, news headlines, and [more.](https://serpapi.com).
29+
Query a vast range of data at scale, including web search results, flight schedules, stock market data, news headlines, and [more](https://serpapi.com).
3030

3131
## Features
3232
* `persistent` → Keep socket connection open to save on SSL handshake / reconnection (2x faster). [Search at scale](#Search-At-Scale)
@@ -36,14 +36,13 @@ Fast query at scale a vast range of data, including web search results, flight s
3636

3737
## Installation
3838

39-
To achieve optimal performance, it is essential to latest Ruby version installed on your system (Ruby 2.7+ is supported by 3.4 is recommended for [performance reason](#Performance-Comparison)).
40-
41-
| Older versions such as Ruby 1.9, 2.x, and JRuby are compatible with [serpapi older library](https://github.com/serpapi/google-search-results-ruby), which continues to function effectively. see [migration guide](#Migration-quick-guide) if you are using the older library.
39+
Ruby 2.7 and later are supported. To achieve an optimal performance, the latest version is recommended. Check 2.7.8 vs 3.4.4 [performance comparison](#Performance-Comparison).
4240

41+
Other versions, such as Ruby 1.9, Ruby 2.x, and JRuby, are compatible with [legacy SerpApi library](https://github.com/serpapi/google-search-results-ruby), which is still supported. To upgrade to the latest library, check our [migration guide](#Migration-quick-guide).
4342

4443
### Bundler
4544
```ruby
46-
gem 'serpapi', '~> 1.0.0'
45+
gem 'serpapi', '~> 1.0', '>= 1.0.1'
4746
```
4847

4948
### Gem
@@ -75,7 +74,7 @@ Environment variables are a secure, safe, and easy way to manage secrets.
7574
## Search API advanced usage with Google search engine
7675

7776
This example dives into all the available parameters for the Google search engine.
78-
The set of parameters is extensive and depends on the search engine you choose.
77+
The list of parameters depends on the chosen search engine.
7978

8079
```ruby
8180
# load gem
@@ -86,15 +85,15 @@ client = SerpApi::Client.new(
8685
engine: 'google',
8786
api_key: ENV['SERPAPI_KEY'],
8887
# HTTP client configuration
89-
async: false, # non blocking HTTP request see: Search Asynchronous (default: false)
88+
async: false, # non-blocking HTTP request see: Search Asynchronous (default: false)
9089
persistent: true, # leave socket connection open for faster response time see: Search at scale (default: true)
9190
timeout: 5, # HTTP timeout in seconds on the client side only. (default: 120s)
92-
symbolize_names: true # turn on/off JSON keys to symbols (default: on more efficient)
91+
symbolize_names: true # turn on/off JSON keys to symbols (default: on, more efficient)
9392
)
9493

9594
# search query overview (more fields available depending on search engine)
9695
params = {
97-
# overview of parameter for Google search engine which one of many search engine supported.
96+
# overview of parameter for Google search engine which is one of many search engine supported.
9897
# select the search engine (full list: https://serpapi.com/)
9998
engine: "google",
10099
# actual search query
@@ -142,10 +141,48 @@ Search API enables `async` search.
142141
- Blocking (`async=false`) : it is easy to write the code but more compute-intensive when the parent process needs to hold many connections.
143142

144143
Here is an example of asynchronous searches using Ruby
145-
<%= snippet('ruby', 'demo/demo_async.rb', true) %>
144+
```ruby
145+
require 'serpapi'
146+
147+
company_list = %w[meta amazon apple netflix google]
148+
client = SerpApi::Client.new(engine: 'google', async: true, persistent: true, api_key: ENV['SERPAPI_KEY'])
149+
schedule_search = Queue.new
150+
result = nil
151+
company_list.each do |company|
152+
result = client.search(q: company)
153+
puts "#{company}: search results found in cache for: #{company}" if result[:search_metadata][:status] =~ /Cached/
154+
155+
schedule_search.push(result[:search_metadata][:id])
156+
end
157+
158+
puts "Last search submited at: #{result[:search_metadata][:created_at]}"
159+
160+
puts 'wait 10s for all requests to be completed '
161+
sleep(10)
162+
163+
puts 'wait until all searches are cached or success'
164+
until schedule_search.empty?
165+
search_id = schedule_search.pop
166+
167+
search_archived = client.search_archive(search_id)
168+
169+
company = search_archived[:search_parameters][:q]
170+
171+
if search_archived[:search_metadata][:status] =~ /Cached|Success/
172+
puts "#{search_archived[:search_parameters][:q]}: search results found in archive for: #{company}"
173+
next
174+
end
175+
176+
schedule_search.push(result)
177+
end
146178

147-
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)).
148-
Each search takes a few seconds before completion by SerpApi service and the search engine. By the time the first element pops out of the queue. The search result might be already available in the archive. If not, the `search_archive` method blocks until the search results are available.
179+
schedule_search.close
180+
puts 'done'
181+
```
182+
183+
* source code: [demo/demo_async.rb](https://github.com/serpapi/serpapi-ruby/blob/master/demo/demo_async.rb)
184+
185+
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)). Each search may take up to few seconds to complete. By the time the first element pops out of the queue, the search results might already be available in the archive. If not, the `search_archive` method blocks until the search results are available.
149186

150187
### Search at scale
151188
The provided code snippet is a Ruby spec test case that demonstrates the use of thread pools to execute multiple HTTP requests concurrently.
@@ -186,14 +223,33 @@ benchmark: (demo/demo_thread_pool.rb)
186223

187224
### Real world search without persistency
188225

189-
<%= snippet('ruby', 'demo/demo.rb', true) %>
226+
```ruby
227+
require 'serpapi'
228+
229+
require 'pp'
230+
231+
client = SerpApi::Client.new(api_key: ENV['SERPAPI_KEY'])
232+
params = {
233+
q: 'coffee'
234+
}
235+
results = client.search(params)
236+
unless results[:organic_results]
237+
puts 'organic results found'
238+
exit 1
239+
end
240+
pp results[:organic_results]
241+
puts 'done'
242+
exit 0
243+
```
244+
245+
* source code: [demo/demo.rb](https://github.com/serpapi/serpapi-ruby/blob/master/demo/demo.rb)
190246

191247
## APIs supported
192248
### Location API
193249

194250
```ruby
195251
require 'serpapi'
196-
client = SerpApi::Client.new()
252+
client = SerpApi::Client.new
197253
location_list = client.location(q: "Austin", limit: 3)
198254
puts "number of location: #{location_list.size}"
199255
pp location_list
@@ -224,7 +280,7 @@ NOTE: api_key is not required for this endpoint.
224280
This API allows retrieving previous search results.
225281
To fetch earlier results from the search_id.
226282

227-
First, you need to run a search and save the search id.
283+
First, you need to run a search and save the search ID.
228284

229285
```ruby
230286
require 'serpapi'
@@ -233,7 +289,7 @@ results = client.search(q: "Coffee", location: "Portland")
233289
search_id = results[:search_metadata][:id]
234290
```
235291

236-
Now we can retrieve the previous search results from the archive using the search id (free of charge).
292+
Now we can retrieve the previous search results from the archive using the search ID (free of charge).
237293

238294
```ruby
239295
require 'serpapi'
@@ -254,22 +310,22 @@ pp client.account
254310
It prints your account information as:
255311
```ruby
256312
{
257-
account_id: "1234567890",
258-
api_key: "your_secret_key",
259-
account_email: "[email protected]",
260-
account_status: "Active",
261-
plan_id: "free",
262-
plan_name: "Free Plan",
263-
plan_monthly_price: 0.0,
264-
searches_per_month: 100,
265-
plan_searches_left: 0,
266-
extra_credits: 100,
267-
total_searches_left: 10,
268-
this_month_usage: 100,
269-
this_hour_searches: 0,
270-
last_hour_searches: 0,
271-
account_rate_limit_per_hour: 100
272-
}
313+
account_id: "1234567890",
314+
api_key: "your_secret_key",
315+
account_email: "[email protected]",
316+
account_status: "Active",
317+
plan_id: "free",
318+
plan_name: "Free Plan",
319+
plan_monthly_price: 0.0,
320+
searches_per_month: 250,
321+
plan_searches_left: 250,
322+
extra_credits: 0,
323+
total_searches_left: 250,
324+
this_month_usage: 0,
325+
this_hour_searches: 0,
326+
last_hour_searches: 0,
327+
account_rate_limit_per_hour: 250
328+
}
273329
```
274330

275331
## Basic example per search engine

Rakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ RSpec::Core::RakeTask.new(:test) do |t|
3737
t.rspec_opts = '--format documentation'
3838
end
3939

40-
desc 'valiate all the examples (comprehensive of tests)'
40+
desc 'validate all the examples (comprehensive set of tests)'
4141
RSpec::Core::RakeTask.new(:regression) do |t|
4242
t.pattern = Dir.glob('spec/serpapi/client/example/*_spec.rb')
4343
t.rspec_opts = '--format documentation'
@@ -68,7 +68,7 @@ task :build do
6868
sh 'gem build serpapi'
6969
end
7070

71-
desc 'install serpapi library from the .gem'
71+
desc 'install serpapi library locally from the .gem'
7272
task :install do
7373
sh "gem install ./serpapi-#{SerpApi::VERSION}.gem"
7474
end

0 commit comments

Comments
 (0)