Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 133 lines (114 sloc) 3.73 KB
#!/usr/bin/ruby1.9.3
# Copyright 2012 Jordon Bedwell.
# MIT License.
# This is the actual code for pry[doc]...
require 'json/ext'
require 'cinch'
require 'fileutils'
require 'faraday'
require 'pry'
require 'logger'
require 'stringio'
require 'shellwords'
log = Logger.new('/var/log/pry/bot.log')
# Changing soon since we plan to use EGRedis-Cluster to cache URI's of
# docs so we don't spam Github with a bunch of redundant informations.
class Gistme
def self.create_gist(file, desc, content)
conn = Faraday.new(:url => 'https://api.github.com') do |builder|
builder.use Faraday::Adapter::EMSynchrony
end
gist = conn.post do |req|
req.body = {
files: {
file => {
content: content
}
},
public: true,
description: desc
}.to_json
req.url '/gists'
req.headers['Content-Type'] = 'application/json'
end
gist = JSON.parse(gist.body)
if gist['html_url']
return gist['html_url']
end
gist['message']
end
end
Thread.abort_on_exception = true
Cinch::Bot.new do
configure do |configuration|
configuration.channels = ['#envygeeks']
configuration.nick = 'pry[bot]'
configuration.user = 'bot'
configuration.name = 'Server 40522'
configuration.server = 'irc.freenode.org'
end
on :message, /\Ashow-(doc|method)\s(.+)/ do |message, command, meth|
begin
Timeout.timeout 5 do
begin
if meth.split(/\s/).length > 1
message.reply 'One command at a time please.'
else
out = StringIO.new
IO.popen('pry --no-color --no-history --no-pager --simple-prompt','r+') do |io|
io.puts "show-#{command} #{meth}"
io.close_write
out.puts io.read
end
if out !~ /\A>>\s*Error/
out = out.string.split(/\n/)[2..-1]
file = out[0].split[1].split(/\//).last
desc = out[0].split(/:/)[1].split(/\//).last
if command == 'method'
comment = '#'
if file.split('.').last == 'c' || file.split('.').last == 'h'
comment = '//'
end
out = out.map.with_index do |value, iteration|
if iteration <= 4 && !value.empty? && value =~ /\A[a-zA-Z\s{1}]+:\s.*/
value = "#{comment} #{value}"
end
value
end
else
file = file.gsub /\.[a-z\-_0-9]+\Z/, '.rdoc'
out = out.map.with_index do |value, iteration|
if iteration <= 4 && !value.empty? && value =~ /\A[a-zA-Z\s{1}]+:\s.*/
value = " #{value}"
end
value
end
end
final_output = Gistme.create_gist(file, desc, out[1..-1].join("\n"))
if final_output =~ /\Ahttp/
message.reply final_output
if meth =~ /\APry/i && command == 'doc'
message.reply 'If this doc is ugly please submit a ticket for a format fix.'
end
else
raise
end
else
log.info "Unknown #{meth}"
message.reply 'Could not find the information you requested.'
end
end
rescue => error
log.fatal error
message.reply 'An error has occured, cannot continue.'
end
end
rescue Timeout::Error => error
log.fatal error
message.reply 'Execution has tried to extend beyond the timeout, killed.'
end
end
on :message, /\A(?:eval|!!)\s(.+)/ do |message, code|
message.reply 'This feature is not supported yet'
end
end.start