Class: CommunityZero::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/community_zero/router.rb

Overview

The router for the Community Server.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, *routes) ⇒ Router

Returns a new instance of Router.



25
26
27
28
29
30
31
# 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

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



23
24
25
# File 'lib/community_zero/router.rb', line 23

def routes
  @routes
end

#serverObject (readonly)

Returns the value of attribute server.



23
24
25
# File 'lib/community_zero/router.rb', line 23

def server
  @server
end

Instance Method Details

#call(request) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# 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