Sha256: f6f4df7e1f20a8891f667f43e86ff11da4845d20153dd78dd2cddb353be0d6bd

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

# -*- coding: utf-8 -*-
require 'cinch'
require 'cinch/cooldown'
require 'cinch/toolbox'

module Cinch
  module Plugins
    # Cinch plugin for getting UD defs.
    class UrbanDict
      include Cinch::Plugin

      enforce_cooldown

      self.help = 'Use .ud <term> to see the Urban Dictionary definition.'

      match(/ud (.*)/)

      def execute(m, term)
        m.reply get_def(term), true
      end

      private

      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."
        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')

        result.gsub!(/[\n\r]/, ' - ')

        # 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

      def ud_url(term)
        "http://www.urbandictionary.com/define.php?term=#{term}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cinch-urbandict-1.0.4 lib/cinch/plugins/urbandict.rb