Class: Cinch::Plugins::JargonFile

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugins/jargonfile.rb

Instance Method Summary collapse

Instance Method Details

#execute(m, word) ⇒ Object


56
57
58
# File 'lib/cinch/plugins/jargonfile.rb', line 56

def execute(m, word)
  m.reply(lookup(word) || "No results found", false)
end

#lookup(word) ⇒ 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cinch/plugins/jargonfile.rb', line 14

def lookup(word)
  begin
    x = word[0].gsub(/([\(\/[email protected]])/,'0').upcase;

    term = word;
    #special cases for when the filename is different than the title
    case term
      when /^([\(])/
        term.gsub!(/^([\(])/,'');
        term.gsub!(/([\)]$)/,''); #hackety hack
      when /^([\/])/
        term.gsub!(/^([\/])/,'');
      when /([\s\W]+$)/
        term.gsub!(/([\s\W]+$)/,'');
      when /^([0])/
        term.gsub!(/^([0])/,'numeral-zero');
      when /^([1])/
        term.gsub!(/^([1])/,'one-');
      when /^([2])/
        term.gsub!(/^([2])/,'infix-two');
      when /^([4])/
        term.gsub!(/^([4])/,'code-4');
      when /^([@])/
        term.gsub!(/^([@])/,'at');
      when /([\*])/
        term.gsub!(/([\*])/,'-asterisk-');
    end

    term.gsub!(/([\s\W])/,'-');

    #url = "http://www.urbandictionary.com/define.php?term=#{CGI.escape(word)}"
    url = "http://www.catb.org/jargon/html/#{x}/#{term}.html"
    page = Nokogiri::HTML(open(url));
    key = CGI.unescape_html page.at("dt##{term}").text.gsub(/\s+/, ' ') rescue nil
    definition = CGI.unescape_html page.at("dd > p").text.gsub(/\s+/, ' ') rescue nil
    output = "#{key} -- #{definition.gsub(/^(.{100}[^\s]*)(.*)/m) {$2.empty? ? $1 : $1 + ''}} (#{url})";
    definition != nil ? output : nil;
  rescue OpenURI::HTTPError => err
    "I couldn't find that! Did you misspell it? Make sure that all words are capitalized properly."
  end
end