Skip to content

Commit c470b80

Browse files
committed
add advanced parallel requests example and dependency to leverate Faraday implementation
1 parent e7b426e commit c470b80

File tree

6 files changed

+93
-9
lines changed

6 files changed

+93
-9
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ group :test, :development do
2020
# code coverage to monitor rspec tests
2121
gem 'simplecov'
2222

23+
# for client_async_spec.rb
24+
gem 'async-http-faraday'
25+
2326
# platforms :mri do
2427
# gem "byebug"
2528
# gem "pry"

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBa
1212

1313
## Installation
1414

15-
Ruby 3+ must be installed for best performance the latest version 3.4 is recommended.
16-
For older version of Ruby 1.9 and 2.x or JRuby, our older library is still functional.
15+
To achieve optimal performance, it is essential to have Ruby 3.x (preferably version 3.4) installed.
16+
17+
| 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.
1718

1819
### Bundler
1920
```ruby
@@ -176,6 +177,30 @@ data = client.search(q: 'Coffee', location: 'Austin, TX')
176177
pp data
177178
```
178179

180+
or a more [advanced parallel requests example](https://lostisland.github.io/faraday/#/advanced/parallel-requests)
181+
182+
```ruby
183+
# install and load
184+
require 'async/http/faraday'
185+
186+
# initialize the client
187+
client = SerpApi::Client.new(adapter: :async_http, api_key: ENV['API_KEY'], timeout: 10)
188+
189+
# run same query in parallel
190+
Async do
191+
Async do
192+
data = client.search(engine: 'google', q: 'Coffee', location: 'Austin, TX')
193+
expect(data.keys.size).to be > 5
194+
expect(data.dig(:search_metadata,:id)).not_to be_nil
195+
end
196+
Async do
197+
data = client.search(engine: 'youtube', search_query: 'Coffee', location: 'Austin, TX')
198+
expect(data.keys.size).to be > 5
199+
expect(data.dig(:search_metadata,:id)).not_to be_nil
200+
end
201+
end
202+
```
203+
179204
## Basic example per search engine
180205

181206
Here is how to calls the APIs.

README.md.erb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ SerpApi supports Google, Google Maps, Google Shopping, Baidu, Yandex, Yahoo, eBa
3131

3232
## Installation
3333

34-
Ruby 3+ must be installed for best performance the latest version 3.4 is recommended.
35-
For older version of Ruby 1.9 and 2.x or JRuby, our older library is still functional.
34+
To achieve optimal performance, it is essential to have Ruby 3.x (preferably version 3.4) installed.
35+
36+
| 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.
3637

3738
### Bundler
3839
```ruby
@@ -195,6 +196,30 @@ data = client.search(q: 'Coffee', location: 'Austin, TX')
195196
pp data
196197
```
197198

199+
or a more [advanced parallel requests example](https://lostisland.github.io/faraday/#/advanced/parallel-requests)
200+
201+
```ruby
202+
# install and load
203+
require 'async/http/faraday'
204+
205+
# initialize the client
206+
client = SerpApi::Client.new(adapter: :async_http, api_key: ENV['API_KEY'], timeout: 10)
207+
208+
# run same query in parallel
209+
Async do
210+
Async do
211+
data = client.search(engine: 'google', q: 'Coffee', location: 'Austin, TX')
212+
expect(data.keys.size).to be > 5
213+
expect(data.dig(:search_metadata,:id)).not_to be_nil
214+
end
215+
Async do
216+
data = client.search(engine: 'youtube', search_query: 'Coffee', location: 'Austin, TX')
217+
expect(data.keys.size).to be > 5
218+
expect(data.dig(:search_metadata,:id)).not_to be_nil
219+
end
220+
end
221+
```
222+
198223
## Basic example per search engine
199224

200225
Here is how to calls the APIs.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Run search in parallel using Async
2+
# and Gem async/http/faraday
3+
#
4+
# https://lostisland.github.io/faraday/#/advanced/parallel-requests
5+
#
6+
7+
require 'spec_helper'
8+
require 'async/http/faraday'
9+
10+
describe 'client adapter with async/http/faraday' do
11+
12+
it 'test httpclient (requires faraday/httpclient)' do
13+
adapter = :async_http
14+
client = SerpApi::Client.new(api_key: ENV['API_KEY'], timeout: 10, adapter: adapter)
15+
16+
# run same query in parallel
17+
Async do
18+
Async do
19+
data = client.search(engine: 'google', q: 'Coffee', location: 'Austin, TX')
20+
expect(data.keys.size).to be > 5
21+
expect(data.dig(:search_metadata,:id)).not_to be_nil
22+
end
23+
Async do
24+
data = client.search(engine: 'youtube', search_query: 'Coffee', location: 'Austin, TX')
25+
expect(data.keys.size).to be > 5
26+
expect(data.dig(:search_metadata,:id)).not_to be_nil
27+
end
28+
end
29+
end
30+
end

spec/serpapi/client/client_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22
require 'faraday/httpclient'
33

4-
describe 'client full coverage' do
4+
describe 'client full code coverage' do
55
before(:all) do
66
@client = SerpApi::Client.new(engine: 'google', api_key: ENV['API_KEY'], timeout: 30)
77
end
@@ -66,7 +66,7 @@
6666
end
6767
end
6868

69-
describe 'client adapter' do
69+
describe 'client adapter with httpclient' do
7070

7171
it 'test httpclient (requires faraday/httpclient)' do
7272
client = SerpApi::Client.new(engine: 'google', api_key: ENV['API_KEY'], timeout: 10, adapter: :httpclient)
@@ -76,4 +76,5 @@
7676
expect(data.keys.size).to be > 5
7777
expect(data[:search_metadata][:id]).not_to be_nil
7878
end
79+
7980
end

spec/spec_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# code coverage
2-
# require 'simplecov'
3-
# SimpleCov.start 'rails'
2+
require 'simplecov'
3+
SimpleCov.start
44

5-
# # load libary
5+
# load libary
66
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
77
require 'serpapi'
88

0 commit comments

Comments
 (0)