Class: Deepsearch::SearchAdapters::SerperAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/search_adapters/serper_adapter.rb

Overview

An adapter for the Serper Search API (google.serper.dev).

Constant Summary collapse

BASE_URL =
'https://google.serper.dev/search'

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ SerperAdapter

Returns a new instance of SerperAdapter.



13
14
15
16
# File 'lib/search_adapters/serper_adapter.rb', line 13

def initialize(api_key = nil)
  @api_key = api_key || Deepsearch.configuration.serper_api_key
  validate_api_key!
end

Instance Method Details

#search(query, options = {}) ⇒ Hash

Returns Parsed and transformed response from Serper API.

Parameters:

  • query (String)

    The search query

  • options (Hash) (defaults to: {})

    Additional search options

Options Hash (options):

  • :max_results (Integer)

    Maximum number of results to return. Serper calls this ‘num`.

Returns:

  • (Hash)

    Parsed and transformed response from Serper API

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/search_adapters/serper_adapter.rb', line 23

def search(query, options = {})
  raise ArgumentError, "Query cannot be empty" if query.nil? || query.strip.empty?

  payload = build_payload(query, options)
  response = make_request(payload)
  parsed_response = parse_response(response)
  transform_response(parsed_response)
rescue Net::HTTPError, Net::ReadTimeout, Net::OpenTimeout => e
  raise SerperError, "Network error: #{e.message}"
rescue JSON::ParserError => e
  raise SerperError, "Invalid JSON response: #{e.message}"
end