Class: Moneypenny::Responders::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/moneypenny/responders/image.rb

Class Method Summary collapse

Class Method Details

.helpObject



8
9
10
# File 'lib/moneypenny/responders/image.rb', line 8

def self.help
  [ 'find a kitten image', 'returns a random kitten picture from Google Image search' ]
end

.respond(message) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/moneypenny/responders/image.rb', line 12

def self.respond(message)
  if (match = message.match(/\Afind\ (a|an)\ (.+)\ (image|picture|photo)\z/i))
    query = match[2]
    url = "https://ajax.googleapis.com/ajax/services/search/images?safe=off&rsz=8&v=1.0&q=#{CGI::escape query}"
    images = JSON.parse(open(url).read)['responseData']['results'] rescue []
    if images.any?
      images[rand(8)]['url']
    else
      "I couldn't find #{match[1]} #{match[2]} image."
    end
  else
    false
  end
end