Skip to content

Commit ed3071e

Browse files
authored
Merge pull request #23 from strzibny/new-examples
Rework example section
2 parents 0ea7f42 + 444e4fe commit ed3071e

45 files changed

Lines changed: 316 additions & 1038 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ rdoc
66
log
77
websites
88
.DS_Store
9+
.env
910

1011
## BUNDLER
1112
*.gem

‎README.md‎

Lines changed: 66 additions & 605 deletions
Large diffs are not rendered by default.

‎README.md.erb‎

Lines changed: 114 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
<%-
2-
def snippet(format, path, demo = false)
3-
lines = File.new(path).readlines
4-
if demo
5-
# skip comment
6-
buf = lines.reject { |line| line =~ /^\s*#|require 'serpapi'/ }[2..-1].join
7-
else
8-
# select test from line start to stop
9-
start = 9
10-
stop = lines.find_index { |line| line =~ /pp results/}
11-
slice = File.new(path).readlines[start..stop+1]
12-
slice.reject! { |l| l =~ /expect\(|^\s*describe|^\s+it|^\s*end\s*$/ }
13-
slice.map! { |l| l.gsub(/(^\s\s\s\s)/, '')}
14-
buf = slice.join
15-
buf.gsub!('# pp ', 'pp ')
16-
buf.gsub!('api_key)', "ENV['SERPAPI_KEY'])")
17-
end
18-
%Q(```#{format}\nrequire 'serpapi'\n#{buf}```\n\n * source code: [#{path}](https://github.com/serpapi/serpapi-ruby/blob/master/#{path}))
19-
end
20-
-%>
211
# SerpApi Ruby Library
222

233
[![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)
@@ -198,7 +178,7 @@ end
198178
# run user thread to search for your favorites coffee type
199179
threads = %w(latte espresso cappuccino americano mocha macchiato frappuccino cold_brew).map do |query|
200180
Thread.new do
201-
pool.with { |socket| socket.search({q: query }).to_s }
181+
pool.with { |socket| socket.search(q: query).to_s }
202182
end
203183
end
204184
responses = threads.map(&:value)
@@ -339,156 +319,156 @@ It prints your account information as:
339319
}
340320
```
341321

342-
## Basic example per search engine
322+
## Examples
323+
324+
Here are some examples for some of our most popular APIs. You can find the full list of supported engines and parameters in our [documentation](https://serpapi.com/search-engine-apis).
325+
326+
### Google Shopping
343327

344-
### Search google
328+
Scrape Google Shopping results with product names, prices, ratings, and merchant information.
345329

346-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_spec.rb') %>
347-
see: [https://serpapi.com/search-api](https://serpapi.com/search-api)
330+
```ruby
331+
require 'serpapi'
348332

349-
### Search google light
350-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_light_spec.rb') %>
351-
see: [https://serpapi.com/google-light-api](https://serpapi.com/google-light-api)
333+
client = SerpApi::Client.new(engine: 'google_shopping', api_key: ENV['SERPAPI_KEY'])
334+
results = client.search(q: 'Macbook M4')
335+
pp results[:shopping_results]
336+
```
352337

353-
### Search google scholar
354-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_scholar_spec.rb') %>
355-
see: [https://serpapi.com/google-scholar-api](https://serpapi.com/google-scholar-api)
338+
[See documentation](https://serpapi.com/google-shopping-api)
356339

357-
### Search google autocomplete
358-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_autocomplete_spec.rb') %>
359-
see: [https://serpapi.com/google-autocomplete-api](https://serpapi.com/google-autocomplete-api)
340+
**Google Shopping Light**
360341

361-
### Search google product
362-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_product_spec.rb') %>
363-
see: [https://serpapi.com/google-product-api](https://serpapi.com/google-product-api)
342+
A [light variant](https://serpapi.com/google-shopping-light-api) engine called `google_shopping_light` is also available for faster, lower-cost shopping searches.
364343

365-
### Search google reverse image
366-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_reverse_image_spec.rb') %>
367-
see: [https://serpapi.com/google-reverse-image](https://serpapi.com/google-reverse-image)
344+
### Google Images
368345

369-
### Search google events
370-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_events_spec.rb') %>
371-
see: [https://serpapi.com/google-events-api](https://serpapi.com/google-events-api)
346+
Scrape Google Images search results, including image URLs, thumbnails, titles, and source pages.
372347

373-
### Search google local services
374-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_local_services_spec.rb') %>
375-
see: [https://serpapi.com/google-local-services-api](https://serpapi.com/google-local-services-api)
348+
```ruby
349+
require 'serpapi'
376350

377-
### Search google maps
378-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_maps_spec.rb') %>
379-
see: [https://serpapi.com/google-maps-api](https://serpapi.com/google-maps-api)
351+
client = SerpApi::Client.new(engine: 'google_images', api_key: ENV['SERPAPI_KEY'])
352+
results = client.search(q: 'coffee')
353+
pp results[:images_results]
354+
```
380355

381-
### Search google jobs
382-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_jobs_spec.rb') %>
383-
see: [https://serpapi.com/google-jobs-api](https://serpapi.com/google-jobs-api)
356+
[See documentation](https://serpapi.com/images-results)
384357

385-
### Search google play
386-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_play_spec.rb') %>
387-
see: [https://serpapi.com/google-play-api](https://serpapi.com/google-play-api)
358+
**Google Images Light**
388359

389-
### Search google images
390-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_images_spec.rb') %>
391-
see: [https://serpapi.com/images-results](https://serpapi.com/images-results)
360+
A [light variant](https://serpapi.com/google-images-light-api) engine called `google_images_light` is also available for faster, lower-cost image searches.
392361

393-
### Search google lens
394-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_lens_spec.rb') %>
395-
see: [https://serpapi.com/google-lens-api](https://serpapi.com/google-lens-api)
362+
### Google Trends
396363

397-
### Search google images light
398-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_images_light_spec.rb') %>
399-
see: [https://serpapi.com/google-images-light-api](https://serpapi.com/google-images-light-api)
364+
Track search interest over time and compare the popularity of search terms.
400365

401-
### Search google hotels
402-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_hotels_spec.rb') %>
403-
see: [https://serpapi.com/google-hotels-api](https://serpapi.com/google-hotels-api)
366+
```ruby
367+
require 'serpapi'
404368

405-
### Search google flights
406-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_flights_spec.rb') %>
407-
see: [https://serpapi.com/google-flights-api](https://serpapi.com/google-flights-api)
369+
client = SerpApi::Client.new(engine: 'google_trends', api_key: ENV['SERPAPI_KEY'])
370+
results = client.search(q: 'coffee', data_type: 'TIMESERIES')
371+
pp results[:interest_over_time]
372+
```
408373

409-
### Search google finance
410-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_finance_spec.rb') %>
411-
see: [https://serpapi.com/google-finance-api](https://serpapi.com/google-finance-api)
374+
[See documentation](https://serpapi.com/google-trends-api)
412375

413-
### Search google ai overview
414-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_ai_overview_spec.rb') %>
415-
see: [https://serpapi.com/google-ai-overview-api](https://serpapi.com/google-ai-overview-api)
376+
### Google Flights
416377

417-
### Search google news
418-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_news_spec.rb') %>
419-
see: [https://serpapi.com/google-news-api](https://serpapi.com/google-news-api)
378+
Search flight routes, schedules, prices, and booking options.
420379

421-
### Search google news light
422-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_news_light_spec.rb') %>
423-
see: [https://serpapi.com/google-news-light-api](https://serpapi.com/google-news-light-api)
380+
> **Note:** The `google_flights` engine does not use `q`. Specify route and date parameters such as `departure_id`, `arrival_id`, `outbound_date`, and `return_date`.
424381

425-
### Search google patents
426-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_patents_spec.rb') %>
427-
see: [https://serpapi.com/google-patents-api](https://serpapi.com/google-patents-api)
382+
```ruby
383+
require 'date'
384+
require 'serpapi'
428385

429-
### Search google trends
430-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_trends_spec.rb') %>
431-
see: [https://serpapi.com/google-trends-api](https://serpapi.com/google-trends-api)
386+
outbound_date = (Date.today + 30).iso8601
387+
return_date = (Date.today + 37).iso8601
388+
client = SerpApi::Client.new(engine: 'google_flights', api_key: ENV['SERPAPI_KEY'])
389+
results = client.search(
390+
departure_id: 'LAX',
391+
arrival_id: 'AUS',
392+
outbound_date: outbound_date,
393+
return_date: return_date
394+
)
395+
flights = results[:best_flights] || results[:other_flights]
396+
pp flights
397+
```
432398

433-
### Search google shopping
434-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_shopping_spec.rb') %>
435-
see: [https://serpapi.com/google-shopping-api](https://serpapi.com/google-shopping-api)
399+
[See documentation](https://serpapi.com/google-flights-api)
436400

437-
### Search google immersive product
438-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_immersive_product_spec.rb') %>
439-
see: [https://serpapi.com/google-immersive-product-api](https://serpapi.com/google-immersive-product-api)
401+
### Google AI Mode API
440402

441-
### Search google videos
442-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_google_videos_spec.rb') %>
443-
see: [https://serpapi.com/google-videos-api](https://serpapi.com/google-videos-api)
403+
The Google AI Mode API returns AI-generated answers with structured text blocks, references, images, products, and more.
444404

445-
### Search amazon
446-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_amazon_spec.rb') %>
447-
see: [https://serpapi.com/amazon-search-api](https://serpapi.com/amazon-search-api)
405+
```ruby
406+
require 'serpapi'
448407

449-
### Search baidu
450-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_baidu_spec.rb') %>
451-
see: [https://serpapi.com/baidu-search-api](https://serpapi.com/baidu-search-api)
408+
client = SerpApi::Client.new(engine: 'google_ai_mode', api_key: ENV['SERPAPI_KEY'])
409+
results = client.search(q: 'best coffee maker')
410+
pp results[:reconstructed_markdown]
411+
```
452412

453-
### Search yahoo
454-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_yahoo_spec.rb') %>
455-
see: [https://serpapi.com/yahoo-search-api](https://serpapi.com/yahoo-search-api)
413+
[See documentation](https://serpapi.com/google-ai-mode-api)
456414

457-
### Search youtube
458-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_youtube_spec.rb') %>
459-
see: [https://serpapi.com/youtube-search-api](https://serpapi.com/youtube-search-api)
415+
### Bing Search
460416

461-
### Search walmart
462-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_walmart_spec.rb') %>
463-
see: [https://serpapi.com/walmart-search-api](https://serpapi.com/walmart-search-api)
417+
Scrape Bing web search results, including organic results, ads, related searches, and more.
464418

465-
### Search ebay
466-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_ebay_spec.rb') %>
467-
see: [https://serpapi.com/ebay-search-api](https://serpapi.com/ebay-search-api)
419+
```ruby
420+
require 'serpapi'
468421

469-
### Search naver
470-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_naver_spec.rb') %>
471-
see: [https://serpapi.com/naver-search-api](https://serpapi.com/naver-search-api)
422+
client = SerpApi::Client.new(engine: 'bing', api_key: ENV['SERPAPI_KEY'])
423+
results = client.search(q: 'coffee')
424+
pp results[:organic_results]
425+
```
472426

473-
### Search home depot
474-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_home_depot_spec.rb') %>
475-
see: [https://serpapi.com/home-depot-search-api](https://serpapi.com/home-depot-search-api)
427+
[See documentation](https://serpapi.com/bing-search-api)
476428

477-
### Search apple app store
478-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_apple_app_store_spec.rb') %>
479-
see: [https://serpapi.com/apple-app-store](https://serpapi.com/apple-app-store)
429+
### DuckDuckGo Search
480430

481-
### Search duckduckgo
482-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_duckduckgo_spec.rb') %>
483-
see: [https://serpapi.com/duckduckgo-search-api](https://serpapi.com/duckduckgo-search-api)
431+
Scrape DuckDuckGo search results, including organic results, ads, knowledge graphs, and related searches.
484432

485-
### Search yandex
486-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_yandex_spec.rb') %>
487-
see: [https://serpapi.com/yandex-search-api](https://serpapi.com/yandex-search-api)
433+
```ruby
434+
require 'serpapi'
435+
436+
client = SerpApi::Client.new(engine: 'duckduckgo', api_key: ENV['SERPAPI_KEY'])
437+
results = client.search(q: 'coffee')
438+
pp results[:organic_results]
439+
```
440+
441+
[See documentation](https://serpapi.com/duckduckgo-search-api)
442+
443+
### Baidu Search
444+
445+
Scrape Baidu search results, including organic results, answer boxes, and related searches.
446+
447+
```ruby
448+
require 'serpapi'
449+
450+
client = SerpApi::Client.new(engine: 'baidu', api_key: ENV['SERPAPI_KEY'])
451+
results = client.search(q: 'coffee')
452+
pp results[:organic_results]
453+
```
454+
455+
[See documentation](https://serpapi.com/baidu-search-api)
456+
457+
### Amazon Search
458+
459+
Scrape Amazon product search results, including product names, prices, ratings, reviews, and availability.
460+
461+
> **Note:** The `amazon` engine uses the `k` parameter for a keyword search, not `q`.
462+
463+
```ruby
464+
require 'serpapi'
465+
466+
client = SerpApi::Client.new(engine: 'amazon', api_key: ENV['SERPAPI_KEY'])
467+
results = client.search(k: 'coffee')
468+
pp results[:organic_results]
469+
```
488470

489-
### Search yelp
490-
<%= snippet('ruby', 'spec/serpapi/client/example/example_search_yelp_spec.rb') %>
491-
see: [https://serpapi.com/yelp-search-api](https://serpapi.com/yelp-search-api)
471+
[See documentation](https://serpapi.com/amazon-search-api)
492472

493473
## Performance Comparison
494474

@@ -539,7 +519,7 @@ client = SerpApi::Client.new(default_parameter)
539519
search.params[:location] = "Portland,Oregon,United States"
540520
# new way
541521
# just provided the search call with the parameters.
542-
results = client.search({location: "Portland,Oregon,United States", q: "Coffee"})
522+
results = client.search(location: "Portland,Oregon,United States", q: "Coffee")
543523

544524
# search format return as raw html
545525
# old way

‎spec/serpapi/client/client_spec.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
it 'missing query' do
3434
begin
35-
client.search({})
35+
client.search
3636
rescue SerpApi::SerpApiError => e
3737
expect(e.message).to include('Missing query')
3838
rescue => e

‎spec/serpapi/client/example/example_search_amazon_spec.rb‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
describe 'example: amazon search' do
44
it 'prints organic_results' do
5-
# Confirm that the environment variable for SERPAPI_KEY has been set properly.
6-
# Your SerpApi key can be obtained at this URL http://serpapi.com
7-
api_key = ENV['SERPAPI_KEY']
8-
skip('SERPAPI_KEY not set. Please set your SerpApi API key.') if api_key.nil?
9-
105
# initialize the serp api client
116
client = SerpApi::Client.new(engine: 'amazon', api_key: api_key)
127
# run a search using serpapi service
13-
results = client.search({
14-
q: 'coffee'
15-
})
8+
results = client.search(
9+
k: 'coffee'
10+
)
1611
expect(results[:organic_results]).not_to be_nil, "No organic results found! keys available: #{results.keys}"
1712

1813
# print the output of the response in formatted JSON

‎spec/serpapi/client/example/example_search_apple_app_store_spec.rb‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
describe 'example: apple_app_store search' do
44
it 'prints organic_results' do
5-
# Confirm that the environment variable for SERPAPI_KEY has been set properly.
6-
# Your SerpApi key can be obtained at this URL http://serpapi.com
7-
api_key = ENV['SERPAPI_KEY']
8-
skip('SERPAPI_KEY not set. Please set your SerpApi API key.') if api_key.nil?
9-
105
# initialize the serp api client
116
client = SerpApi::Client.new(engine: 'apple_app_store', api_key: api_key)
127
# run a search using serpapi service
13-
results = client.search({
8+
results = client.search(
149
term: 'coffee'
15-
})
10+
)
1611
expect(results[:organic_results]).not_to be_nil, "No organic results found! keys available: #{results.keys}"
1712

1813
# print the output of the response in formatted JSON

‎spec/serpapi/client/example/example_search_baidu_spec.rb‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
describe 'example: baidu search' do
44
it 'prints organic_results' do
5-
# Confirm that the environment variable for SERPAPI_KEY has been set properly.
6-
# Your SerpApi key can be obtained at this URL http://serpapi.com
7-
api_key = ENV['SERPAPI_KEY']
8-
skip('SERPAPI_KEY not set. Please set your SerpApi API key.') if api_key.nil?
9-
105
# initialize the serp api client
116
client = SerpApi::Client.new(engine: 'baidu', api_key: api_key)
127
# run a search using serpapi service
13-
results = client.search({
8+
results = client.search(
149
q: 'coffee'
15-
})
10+
)
1611
expect(results[:organic_results]).not_to be_nil, "No organic results found! keys available: #{results.keys}"
1712

1813
# print the output of the response in formatted JSON

0 commit comments

Comments
 (0)