lib/cinch/plugins/urbandict.rb in cinch-urbandict-1.0.3 vs lib/cinch/plugins/urbandict.rb in cinch-urbandict-1.0.4
- old
+ new
@@ -1,54 +1,56 @@
# -*- coding: utf-8 -*-
require 'cinch'
require 'cinch/cooldown'
require 'cinch/toolbox'
-module Cinch::Plugins
- # Cinch plugin for getting UD defs.
- class UrbanDict
- include Cinch::Plugin
+module Cinch
+ module Plugins
+ # Cinch plugin for getting UD defs.
+ class UrbanDict
+ include Cinch::Plugin
- enforce_cooldown
+ enforce_cooldown
- self.help = 'Use .ud <term> to see the Urban Dictionary definition.'
+ self.help = 'Use .ud <term> to see the Urban Dictionary definition.'
- match /ud (.*)/
+ match(/ud (.*)/)
- def execute(m, term)
- m.reply get_def(term), true
- end
+ def execute(m, term)
+ m.reply get_def(term), true
+ end
- private
+ private
- def get_def(term)
- # URI Encode
- term = URI.escape(term, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
+ def get_def(term)
+ # URI Encode
+ term = URI.escape(term, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
- # Check if it's not defined.
- if Cinch::Toolbox.get_html_element(ud_url(term), '#not_defined_yet')
- return "Urban Dictionary ∴ #{term}: No definition available."
+ # Check if it's not defined.
+ if Cinch::Toolbox.get_html_element(ud_url(term), '#not_defined_yet')
+ return "Urban Dictionary ∴ #{term}: No definition available."
+ end
+
+ # Get the def
+ ud_def(term)
end
- # Get the def
- ud_def(term)
- end
+ def ud_def(term)
+ # Grab the element
+ result = Cinch::Toolbox.get_html_element(ud_url(term), '.meaning')
- def ud_def(term)
- # Grab the element
- result = Cinch::Toolbox.get_html_element(ud_url(term), '.meaning')
+ result.gsub!(/[\n\r]/, ' - ')
- result.gsub!(/[\n\r]/, ' - ')
+ # Make sure it's not terribly long
+ result = Cinch::Toolbox.truncate(result, 250)
+ url = Cinch::Toolbox.shorten(ud_url(term))
- # Make sure it's not terribly long
- result = Cinch::Toolbox.truncate(result, 250)
- url = Cinch::Toolbox.shorten(ud_url(term))
+ debug result
+ "Urban Dictionary ∴ #{term}: #{result} [#{url}]"
+ end
- debug result
- "Urban Dictionary ∴ #{term}: #{result} [#{url}]"
- end
-
- def ud_url(term)
- "http://www.urbandictionary.com/define.php?term=#{term}"
+ def ud_url(term)
+ "http://www.urbandictionary.com/define.php?term=#{term}"
+ end
end
end
end