class QbertBot::Router

Attributes

bind[R]
endpoint[R]
port[R]
routes[R]

Public Class Methods

new(conf) click to toggle source
# File lib/qbert_bot/router.rb, line 7
def initialize(conf)
  @routes = []
  @bind = conf['bind']
  @port = conf['port']
  @endpoint = conf['endpoint']
end

Public Instance Methods

exec_route(method, request, params, response) click to toggle source
# File lib/qbert_bot/router.rb, line 22
def exec_route(method, request, params, response)
  @routes.each do |route|
    next unless route.method == method
    next unless match = request.path_info.match(route.path)
    params[:route_matches] = match
    return route.proc.call(request, params, response)
  end
  return 'no route'
end
get(path, &block) click to toggle source
# File lib/qbert_bot/router.rb, line 14
def get(path, &block)
  add_route(:get, path, &block)
end
post(path, &block) click to toggle source
# File lib/qbert_bot/router.rb, line 18
def post(path, &block)
  add_route(:post, path, &block)
end

Private Instance Methods

add_route(method, path, &block) click to toggle source
# File lib/qbert_bot/router.rb, line 33
def add_route(method, path, &block)
  clean_path = nil

  if path.is_a?(Regexp)
    clean_path = path.source
  elsif path.is_a?(String)
    clean_path = Regexp.escape(path)
    clean_path = "/#{clean_path}" unless clean_path[0] == '/'
    clean_path = "#{clean_path}$" unless clean_path[-1, 1] == '$'
  end

  @routes << Route.new(method, Regexp.new(clean_path), block)
end