Class: Boxcars::GoogleSearch

Inherits:
Boxcar
  • Object
show all
Defined in:
lib/boxcars/boxcar/google_search.rb

Overview

A Boxcar that uses the Google SERP API to get answers to questions. It looks through SERP (search engine results page) results to find the answer.

Constant Summary collapse

SERPDESC =

the description of this boxcar

"useful for when you need to answer questions about current events." \
"You should ask targeted questions"

Instance Attribute Summary

Attributes inherited from Boxcar

#description, #name, #parameters, #return_direct

Instance Method Summary collapse

Methods inherited from Boxcar

#apply, assi, #call, #conduct, hist, #input_keys, #load, #output_keys, #save, #schema, syst, user, #validate_inputs, #validate_outputs

Constructor Details

#initialize(name: "Search", description: SERPDESC, serpapi_api_key: nil) ⇒ GoogleSearch

implements a boxcar that uses the Google SerpAPI to get answers to questions.

Parameters:

  • name (String) (defaults to: "Search")

    The name of the boxcar. Defaults to classname.

  • description (String) (defaults to: SERPDESC)

    A description of the boxcar. Defaults to SERPDESC.

  • serpapi_api_key (String) (defaults to: nil)

    The API key to use for the SerpAPI. Defaults to Boxcars.configuration.serpapi_api_key.



16
17
18
19
20
# File 'lib/boxcars/boxcar/google_search.rb', line 16

def initialize(name: "Search", description: SERPDESC, serpapi_api_key: nil)
  super(name: name, description: description)
  api_key = Boxcars.configuration.serpapi_api_key(serpapi_api_key: serpapi_api_key)
  ::GoogleSearch.api_key = api_key
end

Instance Method Details

#get_location(question) ⇒ String

Get the location of an answer from Google using the SerpAPI.

Parameters:

  • question (String)

    The question to ask Google.

Returns:

  • (String)

    The location found.



36
37
38
39
40
41
42
# File 'lib/boxcars/boxcar/google_search.rb', line 36

def get_location(question)
  Boxcars.debug "Question: #{question}", :yellow
  search = ::GoogleSearch.new(q: question, limit: 3)
  answer = search.get_location
  Boxcars.debug "Answer: #{answer}", :yellow, style: :bold
  answer
end

#run(question) ⇒ String

Get an answer from Google using the SerpAPI.

Parameters:

  • question (String)

    The question to ask Google.

Returns:

  • (String)

    The answer to the question.



25
26
27
28
29
30
31
# File 'lib/boxcars/boxcar/google_search.rb', line 25

def run(question)
  search = ::GoogleSearch.new(q: question)
  rv = find_answer(search.get_hash)
  Boxcars.info "Question: #{question}"
  Boxcars.info "Answer: #{rv}"
  rv
end