class Irc::Bot::WebDispatcher::WebTemplate

Attributes

botmodule[R]
options[R]
pattern[R]

Public Class Methods

new(botmodule, pattern, options={}) click to toggle source
# File lib/rbot/core/webservice.rb, line 159
def initialize(botmodule, pattern, options={})
  @botmodule = botmodule
  @pattern = pattern
  @options = options
  set_auth_path(@options)
end

Public Instance Methods

recognize(m) click to toggle source
# File lib/rbot/core/webservice.rb, line 166
def recognize(m)
  message_route = m.path[1..-1].split('/')
  template_route = @pattern[1..-1].split('/')
  params = {}

  debug 'web mapping path %s <-> %s' % [message_route.inspect, template_route.inspect]

  message_route.each do |part|
    tmpl = template_route.shift
    return false if not tmpl

    if tmpl[0] == ':'
      # push part as url path parameter
      params[tmpl[1..-1].to_sym] = part
    elsif tmpl == part
      next
    else
      return false
    end
  end

  debug 'web mapping params is %s' % [params.inspect]

  params
end
set_auth_path(hash) click to toggle source
# File lib/rbot/core/webservice.rb, line 192
def set_auth_path(hash)
  if hash.has_key?(:full_auth_path)
    warning "Web route #{@pattern.inspect} in #{@botmodule} sets :full_auth_path, please don't do this"
  else
    pre = @botmodule
    words = @pattern[1..-1].split('/').reject{ |x|
      x == pre || x =~ /^:/ || x =~ /\[|\]/
    }
    if words.empty?
      post = nil
    else
      post = words.first
    end
    if hash.has_key?(:auth_path)
      extra = hash[:auth_path]
      if extra.sub!(/^:/, "")
        pre += "::" + post
        post = nil
      end
      if extra.sub!(/:$/, "")
        if words.length > 1
          post = [post,words[1]].compact.join("::")
        end
      end
      pre = nil if extra.sub!(/^!/, "")
      post = nil if extra.sub!(/!$/, "")
      extra = nil if extra.empty?
    else
      extra = nil
    end
    hash[:full_auth_path] = [pre,extra,post].compact.join("::")
    debug "Web route #{@pattern} in #{botmodule} will use authPath #{hash[:full_auth_path]}"
  end
end