class Irc::Bot::WebDispatcher

works similar to a message mapper but for url paths

Public Class Methods

new(bot) click to toggle source
# File lib/rbot/core/webservice.rb, line 228
def initialize(bot)
  @bot = bot
  @templates = []
end

Public Instance Methods

handle(m) click to toggle source

Handle a web service request, find matching mapping and dispatch.

In case authentication fails, sends a 401 Not Authorized response.

# File lib/rbot/core/webservice.rb, line 254
def handle(m)
  if @templates.empty?
    m.send_plaintext('no routes!', 404)
    return false if @templates.empty?
  end
  failures = []
  @templates.each do |tmpl|
    # Skip this element if it was unmapped
    next unless tmpl
    botmodule = @bot.plugins[tmpl.botmodule]
    params = tmpl.recognize(m)
    if params
      action = tmpl.options[:action]
      unless botmodule.respond_to?(action)
        failures << NoActionFailure.new(tmpl, m)
        next
      end
      # check http method:
      unless not tmpl.options.has_key? :method or tmpl.options[:method] == m.method
        debug 'request method missmatch'
        next
      end
      auth = tmpl.options[:full_auth_path]
      debug "checking auth for #{auth.inspect}"
      # We check for private permission
      if m.bot.auth.permit?(m.source || Auth::defaultbotuser, auth, '?')
        debug "template match found and auth'd: #{action.inspect} #{params.inspect}"
        response = botmodule.send(action, m, params)
        if m.res.sent_size == 0 and m.res.body.empty?
          m.send_json(response.to_json)
        end
        return true
      end
      debug "auth failed for #{auth}"
      # if it's just an auth failure but otherwise the match is good,
      # don't try any more handlers
      m.send_plaintext('Authentication Required!', 401)
      return false
    end
  end
  failures.each {|r|
    debug "#{r.template.inspect} => #{r}"
  }
  debug "no handler found"
  m.send_plaintext('No Handler Found!', 404)
  return false
end
map(botmodule, pattern, options={}) click to toggle source
# File lib/rbot/core/webservice.rb, line 233
def map(botmodule, pattern, options={})
  @templates << WebTemplate.new(botmodule.to_s, pattern, options)
  debug 'template route: ' + @templates[-1].inspect
  return @templates.length - 1
end
unmap(botmodule, index) click to toggle source

The unmap method for the RemoteDispatcher nils the template at the given index, therefore effectively removing the mapping

# File lib/rbot/core/webservice.rb, line 242
def unmap(botmodule, index)
  tmpl = @templates[index]
  raise "Botmodule #{botmodule.name} tried to unmap #{tmpl.inspect} that was handled by #{tmpl.botmodule}" unless tmpl.botmodule == botmodule.name
  debug "Unmapping #{tmpl.inspect}"
  @templates[index] = nil
  @templates.clear unless @templates.compact.size > 0
end