-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
100 lines (83 loc) · 2.9 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require "rapture"
require "sequel"
require "yaml"
require "yard"
config = YAML.load_file('config.yml')
TOKEN = config["token"]
OWNER_ID = config["owner_id"].to_i
GITHUB_PAGES_URL = config["docs_url"]
LIB_DIR = config["lib"]["dir"]
ROOT_NAMESPACE = config["lib"]["namespace"]
YARD::Registry.load_yardoc
DB = Sequel.connect("sqlite://doc-o-tron.db")
DB.create_table? :allowed_channels do
String :id
end
def format_path(object)
url_path = if object.type == :method
namespace, method_name = object.path.split('#')
"#{namespace.gsub("::", "/")}.html##{method_name}-instance_method"
elsif object.type == :constant
*namespace, const = object.path.split('::')
"#{namespace.join("/")}.html##{const}-constant"
else
object.path.gsub("::", "/") + ".html"
end
url_path.gsub("?", "%3F")
end
client = Rapture::Client.new(TOKEN)
# Allow/Deny/Update
client.on_message_create do |message|
next unless message.author.id == OWNER_ID
case message.content
when "doc-o-tron>allow"
DB[:allowed_channels].insert(message.channel_id.to_s)
client.create_message(message.channel_id, content: "Success")
when message.content == "doc-o-tron>deny"
DB[:allowed_channels].where(id: message.channel_id.to_s).delete
client.create_message(message.channel_id, content: "Success")
when "doc-o-tron>update"
client.create_message(message.channel_id, content: "Updating sources")
`git pull --recurse-submodules`
`bundle exec yard doc #{LIB_DIR}`
YARD::Registry.load_yardoc
end
end
client.on_message_create do |message|
next unless DB[:allowed_channels].map(:id).include? message.channel_id.to_s
args = message.content.split('>', 2)
if args[0] == 'doc' && args[1]
object = YARD::Registry.load_yardoc.resolve(P(ROOT_NAMESPACE), args.last.strip, true)
if object
path = format_path(object)
fields = []
fields << {
name: "Parameters",
value: object.tags("param").collect {|param| "`#{param.name} [#{param.types.join ", "}] #{param.text}`" }.join("\n")
} unless object.tags("param").empty?
fields << {
name: "Options",
value: object.tags("option").collect {|o| "`#{o.name}{#{o.pair.name}} [#{o.pair.types.join(", ")}] #{o.pair.text}`" }.join("\n")
} unless object.tags("option").empty?
fields << {
name: "Return",
value: object.tags("return").collect {|o| "`[#{o.types.join(", ")}] #{o.text}`" }.join("\n")
} unless object.tags("return").empty?
client.create_message(
message.channel_id,
embed: {
title: object.path,
url: "#{GITHUB_PAGES_URL}/#{path}",
description: object.docstring,
fields: fields
}
)
else
client.create_message(
message.channel_id,
content: "Unable to find docs"
)
end
end
end
client.run