class CommunityZero::Router
The router for the Community Server
.
@author Seth Vargo <sethvargo@gmail.com>
Attributes
routes[R]
server[R]
Public Class Methods
new(server, *routes)
click to toggle source
# File lib/community_zero/router.rb, line 25 def initialize(server, *routes) @server = server @routes = routes.map do |route, endpoint| pattern = Regexp.new("^#{route.gsub(/:[A-Za-z_]+/, '[^/]*')}$") [pattern, endpoint] end end
Public Instance Methods
call(request)
click to toggle source
# File lib/community_zero/router.rb, line 33 def call(request) begin path = '/' + request.path.join('/') find_endpoint(path).new(server).call(request) rescue [ 500, { 'Content-Type' => 'text/plain' }, "Exception raised! #{$!.inspect}\n#{$!.backtrace.join("\n")}" ] end end
Private Instance Methods
find_endpoint(path)
click to toggle source
# File lib/community_zero/router.rb, line 47 def find_endpoint(path) _, endpoint = routes.find { |route, endpoint| route.match(path) } endpoint || NotFoundEndpoint end