class Cinch::Plugins::Wikipedia

Plugin to allow users to search wikipedia.

Versioning information

Constants

VERSION

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/cinch/plugins/wikipedia.rb, line 17
def initialize(*args)
  super
  @max_length = config[:max_length] || 300
end

Public Instance Methods

execute(m, term) click to toggle source
# File lib/cinch/plugins/wikipedia.rb, line 22
def execute(m, term)
  m.reply wiki(term)
end

Private Instance Methods

get_def(term, url) click to toggle source
# File lib/cinch/plugins/wikipedia.rb, line 40
def get_def(term, url)
  cats = Cinch::Toolbox.get_html_element(url, '#mw-normal-catlinks')
  if cats && cats.include?('Disambiguation')
    return "'#{term} is too vague and lead to a disambiguation page."
  else
    wiki_text = Cinch::Toolbox.get_html_element(url, '#mw-content-text/p')
    if wiki_text.nil?
      return not_found(url)
    end
    return wiki_text
  end
end
not_found(url) click to toggle source
# File lib/cinch/plugins/wikipedia.rb, line 53
def not_found(url)
  msg = "I couldn't find anything for that search, "
  alt_term = Cinch::Toolbox.get_html_element(url, '.searchdidyoumean')
  if alt_term
    alt_term = alt_term[/\ADid you mean: (\w+)\z/, 1]
    msg << "did you mean '#{alt_term}'?"
  else
    msg << 'sorry!'
  end
  msg
end
wiki(term) click to toggle source
# File lib/cinch/plugins/wikipedia.rb, line 28
def wiki(term)
  # URI Encode
  term = URI.escape(term, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  url = "http://en.wikipedia.org/w/index.php?search=#{term}"

  # Truncate text and url if they are too long
  text = Cinch::Toolbox.truncate(get_def(term, url), @max_length)
  url  = Cinch::Toolbox.shorten(url)

  "Wikipedia ∴ #{text} [#{url}]"
end