class Irc::Bot::WebMessage
A WebMessage
is a web request and response object combined with helper methods.
Attributes
Parsed url parameters.
Bot
instance
Client
IP.
HTTP method (POST, GET, etc.)
URL Path.
Parsed post request parameters.
Request object, a instance of WEBrick::HTTPRequest ({www.ruby-doc.org/stdlib-2.0/libdoc/webrick/rdoc/WEBrick/HTTPRequest.html docs})
Response object, a instance of WEBrick::HTTPResponse ({www.ruby-doc.org/stdlib-2.0/libdoc/webrick/rdoc/WEBrick/HTTPResponse.html docs})
The bot user issuing the command.
Public Class Methods
# File lib/rbot/core/webservice.rb, line 55 def initialize(bot, req, res) @bot = bot @req = req @res = res @method = req.request_method @post = {} if req.body and not req.body.empty? @post = parse_query(req.body) end @args = {} if req.query_string and not req.query_string.empty? @args = parse_query(req.query_string) end @client = req.peeraddr[3] # login a botuser with http authentication WEBrick::HTTPAuth.basic_auth(req, res, 'RBotAuth') { |username, password| if username botuser = @bot.auth.get_botuser(Auth::BotUser.sanitize_username(username)) if botuser and botuser.password == password @source = botuser true else false end else true # no need to request auth at this point end } @path = req.path @load_path = [File.join(Config::datadir, 'web')] @load_path += @bot.plugins.core_module_dirs @load_path += @bot.plugins.plugin_dirs end
Public Instance Methods
Expands a relative filename to absolute using a list of load paths.
# File lib/rbot/core/webservice.rb, line 134 def get_load_path(filename) @load_path.each do |path| file = File.join(path, filename) return file if File.exists?(file) end end
# File lib/rbot/core/webservice.rb, line 93 def parse_query(query) params = CGI::parse(query) params.each_pair do |key, val| params[key] = val.last end params end
Remote messages are always ‘private’
# File lib/rbot/core/webservice.rb, line 107 def private? true end
Renders a erb template and responds it
# File lib/rbot/core/webservice.rb, line 142 def render(template, args={}) file = get_load_path template if not file raise 'template not found: ' + template end tmpl = ERB.new(IO.read(file)) ns = OpenStruct.new(args) body = tmpl.result(ns.instance_eval { binding }) send_html(body, 200) end
Sends a html response
# File lib/rbot/core/webservice.rb, line 129 def send_html(body, status=200) send_response(body, status, 'text/html') end
Sends a json response
# File lib/rbot/core/webservice.rb, line 124 def send_json(body, status=200) send_response(body, status, 'application/json') end
Sends a plaintext response
# File lib/rbot/core/webservice.rb, line 119 def send_plaintext(body, status=200) send_response(body, status, 'text/plain') end
Sends a response with the specified body, status and content type.
# File lib/rbot/core/webservice.rb, line 112 def send_response(body, status, type) @res.status = status @res['Content-Type'] = type @res.body = body end
The target of a RemoteMessage
# File lib/rbot/core/webservice.rb, line 102 def target @bot end