Skip to content

Commit 25af142

Browse files
committed
rewrite default demo to a basic google search
move code to demo_suggest
1 parent a671f30 commit 25af142

File tree

2 files changed

+51
-42
lines changed

2 files changed

+51
-42
lines changed

demo/demo.rb

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,16 @@
1-
# The code snippet you provided demonstrates a simple implementation of the SerpApi client using the Ruby Serpapi gem.
2-
# It performs a Google Autocomplete search for the query "coffee" and prints the suggestions returned.
3-
#
4-
# **Key Points:**
5-
#
6-
# * **API Key:** The code requires an API key to be set in the `SERPAPI_KEY` environment variable.
7-
# * **Serpapi Client:** A SerpApi client is created with default parameters, including the engine, client, language,
8-
# country, API key, persistence settings, and timeout.
9-
# * **Search Parameters:** A search request is made with the query "coffee".
10-
# * **Suggestions Retrieval:** The `suggestions` key in the response is checked for availability and non-emptiness.
11-
# * **Output:** The suggestions are printed using the `pp` gem.
12-
#
13-
# **Purpose:**
14-
#
15-
# The code is designed to demonstrate how to use the SerpApi client to retrieve autocomplete suggestions from Google. It
16-
# verifies that suggestions are returned and outputs them for demonstration purposes.
17-
#
18-
# **Usage:**
19-
#
20-
# To run the code, you need to set the `SERPAPI_KEY` environment variable to your actual SerpApi API key.
21-
# Obtain your free key from: serpapi.com
22-
#
23-
# **Output:**
24-
#
25-
# The script will output the autocomplete suggestions for the query "coffee" in a formatted manner.
26-
#
27-
# **Additional Notes:**
28-
#
29-
# * The `persistent: false` parameter ensures that each search request is treated as a new session. this is no the best pratice.
30-
# * The `timeout: 2` parameter sets a timeout of 2 seconds for each request.
31-
# * The code uses the `pp` gem for pretty-printing the suggestions.
32-
# * The `exit 1` and `exit 0` statements are used to indicate success or failure, respectively.
33-
34-
require 'serpapi'
35-
require 'pp'
1+
# This script initializes default parameters for interacting with the SerpApi service.
2+
# The `default_params` hash contains the following keys:
3+
# - `engine`: Specifies the search engine to be used, in this case, 'google'.
4+
# - `api_key`: Retrieves the API key from the environment variable `SERPAPI_KEY`.
5+
# Ensure that the `SERPAPI_KEY` environment variable is set before running this script.
366

377
raise 'SERPAPI_KEY environment variable must be set' if ENV['SERPAPI_KEY'].nil?
8+
require 'pp'
9+
require 'serpapi'
3810

3911
default_params = {
40-
engine: 'google_autocomplete',
41-
client: 'safari',
42-
hl: 'en',
43-
gl: 'us',
44-
api_key: ENV.fetch('SERPAPI_KEY', nil),
45-
persistent: false,
46-
timeout: 2
12+
engine: 'google',
13+
api_key: ENV['SERPAPI_KEY']
4714
}
4815
client = SerpApi::Client.new(default_params)
4916
params = {

demo/demo_suggest.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
# This script demonstrates how to use the SerpApi Ruby client to fetch Google Autocomplete suggestions.
3+
# It requires the `serpapi` gem and an environment variable `SERPAPI_KEY` containing your SerpApi API key.
4+
#
5+
# The script initializes the SerpApi client with default parameters, performs a search query for "coffee",
6+
# and prints the autocomplete suggestions if available. If no suggestions are found, the script exits with an error.
7+
#
8+
# Prerequisites:
9+
# - Install the `serpapi` gem (`gem install serpapi`).
10+
# - Set the `SERPAPI_KEY` environment variable with your SerpApi API key.
11+
# you can obtain an API key by signing up at https://serpapi.com.
12+
#
13+
# Usage:
14+
# Run the script in a terminal:
15+
# ruby demo_suggest.rb
16+
require 'serpapi'
17+
require 'pp'
18+
19+
raise 'SERPAPI_KEY environment variable must be set' if ENV['SERPAPI_KEY'].nil?
20+
21+
default_params = {
22+
engine: 'google_autocomplete',
23+
client: 'safari',
24+
hl: 'en',
25+
gl: 'us',
26+
api_key: ENV['SERPAPI_KEY'],
27+
persistent: false,
28+
timeout: 2
29+
}
30+
client = SerpApi::Client.new(default_params)
31+
params = {
32+
q: 'coffee'
33+
}
34+
results = client.search(params)
35+
puts 'print suggestions'
36+
if !results[:suggestions] || results[:suggestions].empty?
37+
puts 'no suggestions found'
38+
exit 1
39+
end
40+
pp results[:suggestions]
41+
puts 'done'
42+
exit 0

0 commit comments

Comments
 (0)