Sha256: b3c8b630c0cae321d2ec62d10531710c7e8435462294d27ccda119029d855144

Contents?: true

Size: 710 Bytes

Versions: 2

Compression:

Stored size: 710 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.stackoverflow.com/docs/search#site=stackoverflow&sort=activity&intitle=#{CGI.escape(query)}"
p url
    res = JSON.parse(open(url))
p res
    res.items.each do |i|
      title = i.title
      link = i.link
      desc = i.tags.join(', ')
    end
    "#{title} - #{desc} (#{link})"
  rescue
    "No results found"
  end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cogbot-0.0.3 plugins/stackoverflow.rb
cogbot-0.0.2 plugins/stackoverflow.rb