Class: CQHttp::Utils
- Inherits:
-
Object
- Object
- CQHttp::Utils
- Defined in:
- lib/Bot/Utils.rb
Overview
各种工具包
Example:
CQHttp::Utils.log str, Logger::INFO
Instance Attribute Summary collapse
-
#fileLogger ⇒ Logger
文件Logger.
-
#loggerFile ⇒ String
Logger文件地址.
-
#stdLogger ⇒ Logger
终端Logger.
Class Method Summary collapse
-
.cq_parse(cqmsg) ⇒ Hash
CQ码解析.
-
.httpPost(url, ret) ⇒ String
(also: post)
post发包.
-
.initLogger(loggerFile = nil) ⇒ Object
初始化日志.
-
.log(str, severity = Logger::INFO, app = "RUBY-CQHTTP") ⇒ Object
输出日志.
-
.msg_change(msg) ⇒ String
消息转义 & -> & [ -> [ ] -> ].
-
.msg_change!(msg) ⇒ String
消息反转义 & -> & [ -> [ ] -> ].
-
.setLoggerLevel(loggerLevel) ⇒ Object
设置日志等级.
Instance Attribute Details
#fileLogger ⇒ Logger
Returns 文件Logger.
13 |
# File 'lib/Bot/Utils.rb', line 13 attr_accessor :stdLogger, :fileLogger, :loggerFile |
#loggerFile ⇒ String
Returns Logger文件地址.
13 |
# File 'lib/Bot/Utils.rb', line 13 attr_accessor :stdLogger, :fileLogger, :loggerFile |
#stdLogger ⇒ Logger
Returns 终端Logger.
13 14 15 |
# File 'lib/Bot/Utils.rb', line 13 def stdLogger @stdLogger end |
Class Method Details
.cq_parse(cqmsg) ⇒ Hash
CQ码解析
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/Bot/Utils.rb', line 90 def cq_parse(cqmsg) cqary = [] cqmsg.scan(/\[CQ:(.*?),(.*?)\]/m).each do |matches| cqcode = { type: matches[0], data: {} } matches[1].split(',').each do |arg| args = arg.split('=') cqcode[:data][args[0].to_sym] = args[1] end cqary << cqcode end cqary end |
.httpPost(url, ret) ⇒ String Also known as: post
post发包
48 49 50 51 52 53 54 55 |
# File 'lib/Bot/Utils.rb', line 48 def httpPost(url, ret) req = Net::HTTP::Post.new(url.path, { 'Content-Type' => 'application/json' }) req.body = ret res = Net::HTTP.start(url.hostname, url.port) do |http| http.request(req) end res.body end |
.initLogger(loggerFile = nil) ⇒ Object
初始化日志
19 20 21 22 23 |
# File 'lib/Bot/Utils.rb', line 19 def initLogger(loggerFile=nil) @loggerFile = loggerFile @stdLogger = setLogger(Logger.new(STDOUT)) @fileLogger = setLogger(Logger.new(@loggerFile, 'daily')) if @loggerFile end |
.log(str, severity = Logger::INFO, app = "RUBY-CQHTTP") ⇒ Object
输出日志
38 39 40 41 |
# File 'lib/Bot/Utils.rb', line 38 def log(str, severity=Logger::INFO, app="RUBY-CQHTTP") @stdLogger.log(severity, str, app) @fileLogger.log(severity, str, app) if @loggerFile end |
.msg_change(msg) ⇒ String
消息转义
& -> &
[ -> [
] -> ]
65 66 67 68 69 70 |
# File 'lib/Bot/Utils.rb', line 65 def msg_change(msg) msg.gsub!('&','&') msg.gsub!('[','[') msg.gsub!(']',']') msg end |
.msg_change!(msg) ⇒ String
消息反转义
& -> &
[ -> [
] -> ]
79 80 81 82 83 84 |
# File 'lib/Bot/Utils.rb', line 79 def msg_change!(msg) msg.gsub!('&','&') msg.gsub!('[','[') msg.gsub!(']',']') msg end |
.setLoggerLevel(loggerLevel) ⇒ Object
设置日志等级
28 29 30 31 |
# File 'lib/Bot/Utils.rb', line 28 def setLoggerLevel(loggerLevel) @stdLogger.level = loggerLevel @fileLogger.level = loggerLevel if @loggerFile end |