Sha256: e0f6a7fc05d84714eb25f389747485e07408bf0e59df1627a0d4e475fd19ba64

Contents?: true

Size: 961 Bytes

Versions: 14

Compression:

Stored size: 961 Bytes

Contents

require 'open-uri'
require 'json'

class Stackoverflow
  include Cinch::Plugin

  match /s (.+)/

  set :plugin_name, 'stackoverflow'
  set :help, <<EOT
Stackoverflow returns the first match on a search
.s  <keywords> : searches on those keywords
EOT

  def new(bot)
    @bot = bot
  end

  def search(query)
    url = "http://api.stackexchange.com/2.2/search?site=stackoverflow&pagesize=3&sort=activity&order=desc&intitle=#{CGI.escape(query)}"
    puts url

    resp = open(url, "Accept-Encoding" => "gzip,deflate")
    gz = Zlib::GzipReader.new(StringIO.new(resp.string))
    res = JSON.parse(gz.read)
    if res['items'].count == 0
      return "No results found."
    end
    back = ""
    res['items'].each do |i|
      title = i['title']
      link = i['link']
      desc = i['tags'].join(', ')
      back += "#{title} - #{desc} (#{link})\n"
    end
    back
  rescue
    "Query error."
  end

  def execute(m, query)
    m.reply(search(query))
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
cogbot-0.1.14 plugins/stackoverflow.rb
cogbot-0.1.13 plugins/stackoverflow.rb
cogbot-0.1.12 plugins/stackoverflow.rb
cogbot-0.1.11 plugins/stackoverflow.rb
cogbot-0.1.10 plugins/stackoverflow.rb
cogbot-0.1.9 plugins/stackoverflow.rb
cogbot-0.1.7 plugins/stackoverflow.rb
cogbot-0.1.6 plugins/stackoverflow.rb
cogbot-0.1.5 plugins/stackoverflow.rb
cogbot-0.1.4 plugins/stackoverflow.rb
cogbot-0.1.3 plugins/stackoverflow.rb
cogbot-0.1.2 plugins/stackoverflow.rb
cogbot-0.1.1 plugins/stackoverflow.rb
cogbot-0.1.0 plugins/stackoverflow.rb