12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/moneypenny/responders/wikipedia.rb', line 12
def self.respond(message)
if (query = message.match(/\Awhat\ is\ (.+)(\?|)\z/i))
query = query[1]
agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.107 Safari/535.1'
response = open("http://en.wikipedia.org/w/api.php?action=opensearch&search=#{CGI::escape query}&format=xml&limit=1", 'User-Agent' => agent).read
description = response.match(/\<Description\ xml\:space\=\"preserve\"\>(.+)\<\/Description\>/m)[1] rescue nil
url = response.match(/\<Url\ xml\:space\=\"preserve\"\>(.+)\<\/Url\>/m)[1] rescue nil
if description
description + " (#{url})"
else
"I couldn't find any information on #{query}."
end
else
false
end
end
|