Sha256: 422259719b286561bb94a8bfdc3b149ec794721453ffce5cc20020168d099c3a

Contents?: true

Size: 1.5 KB

Versions: 39

Compression:

Stored size: 1.5 KB

Contents

require 'open-uri'

module Plugins
  class UrbanDictionary
    include Cinch::Plugin
    include Cinch::Helpers

    enable_acl(:nobody)

    set(
        plugin_name: 'UrbanDictionary',
        help: "Urban Dictionary -- Grabs a term from urbandictionary.com.\nUsage: `?urban <term>`; `?wotd`; `!?woty`"
    )

    match /urban (.*)/, method: :query
    match /ud (.*)/,    method: :query
    match /wotd/,       method: :wotd
    match "ud",         method: :wotd

    def query(m, query)
      m.reply "UD↦ #{search(query)}"
    end


    def wotd(m)
      url = URI.encode "http://www.urbandictionary.com/"
      doc = Nokogiri.HTML(
          # RestClient.get(url)
          open(url)
      )
      word = doc.at_css('.word').text.strip[0..40]
      meaning = doc.at_css('.meaning').text.strip[0..450] + "... \u263A"
      m.reply "UD↦ #{word} -- #{meaning}"
    end

    private
    def search(query)
      url = URI.encode "http://api.urbandictionary.com/v0/define?term=#{query}"
      # Nokogiri.HTML(open url).at_css('.meaning').text.strip[0..500]

      # Load API data
      data = JSON.parse(
          #RestClient.get(url)
          open(url).read
      )

      # Return if nothing is found
      return 'No Results found' if data['result_type'] == 'no_results'

      # Return first definition
      string = data['list'].first['definition'].gsub(/\r|\n|\n\r/, ' ')
      string[0..450] + "... \u263A"
    rescue => e
      e.message
    end
  end
end

# AutoLoad
Bot.config.plugins.plugins.push Plugins::UrbanDictionary

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
zetabot-2.0.8 lib/Zeta/plugins/urban.rb
zetabot-2.0.7 lib/Zeta/plugins/urban.rb
zetabot-2.0.6 lib/Zeta/plugins/urban.rb
zetabot-2.0.5 lib/Zeta/plugins/urban.rb
zetabot-2.0.4 lib/Zeta/plugins/urban.rb
zetabot-2.0.3 lib/Zeta/plugins/urban.rb
zetabot-2.0.2 lib/Zeta/plugins/urban.rb
zetabot-2.0.1 lib/Zeta/plugins/urban.rb
zetabot-2.0.0 lib/Zeta/plugins/urban.rb
zetabot-1.1.0 lib/Zeta/plugins/urban.rb
zetabot-1.0.7 lib/Zeta/plugins/urban.rb
zetabot-1.0.6 lib/Zeta/plugins/urban.rb
zetabot-1.0.5 lib/Zeta/plugins/urban.rb
zetabot-1.0.4 lib/Zeta/plugins/urban.rb
zetabot-1.0.3 lib/Zeta/plugins/urban.rb
zetabot-1.0.2 lib/Zeta/plugins/urban.rb
zetabot-1.0.1 lib/Zeta/plugins/urban.rb
zetabot-1.0.0 lib/Zeta/plugins/urban.rb
zetabot-0.0.22 lib/Zeta/plugins/urban.rb
zetabot-0.0.21 lib/Zeta/plugins/urban.rb