Class: Lita::Handlers::Google

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/google.rb

Constant Summary collapse

URL =
"https://ajax.googleapis.com/ajax/services/search/web"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_config(handler_config) ⇒ Object



15
16
17
# File 'lib/lita/handlers/google.rb', line 15

def self.default_config(handler_config)
  handler_config.safe_search = :active
end

Instance Method Details

#search(response) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lita/handlers/google.rb', line 19

def search(response)
  query = response.matches[0][0]

  http_response = http.get(
    URL,
    safe: safe_value,
    q: query,
    v: "1.0"
  )

  if http_response.status == 200
    data = MultiJson.load(http_response.body)
    result = data["responseData"]["results"].first

    if result
      response.reply(
        "#{CGI.unescapeHTML(result["titleNoFormatting"])} - #{result["unescapedUrl"]}"
      )
    else
      response.reply("No search results for query: #{query}")
    end
  else
    Lita.logger.warn(
      "Non-200 response from Google for search query: #{query}"
    )
  end
end