Class: Lita::Handlers::Animate

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_config(handler_config) ⇒ Object



10
11
12
# File 'lib/lita/handlers/animate.rb', line 10

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

Instance Method Details

#fetch(response) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lita/handlers/animate.rb', line 14

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

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

  data = MultiJson.load(http_response.body)

  if data["responseStatus"] == 200
    choice = data["responseData"]["results"].sample
    response.reply "#{choice["unescapedUrl"]}#.gif"
  else
    reason = data["responseDetails"] || "unknown error"
    Lita.logger.warn(
      "Couldn't get image from Google: #{reason}"
    )
  end
end