|
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. |
36 | 6 |
|
37 | 7 | raise 'SERPAPI_KEY environment variable must be set' if ENV['SERPAPI_KEY'].nil? |
| 8 | +require 'pp' |
| 9 | +require 'serpapi' |
38 | 10 |
|
39 | 11 | 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'] |
47 | 14 | } |
48 | 15 | client = SerpApi::Client.new(default_params) |
49 | 16 | params = { |
|
0 commit comments