Class: RubyEventStore::Browser::Router

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

Defined Under Namespace

Classes: Route

Constant Summary collapse

NoMatch =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(urls = Urls.initial) ⇒ Router

Returns a new instance of Router.



38
39
40
41
# File 'lib/ruby_event_store/browser/router.rb', line 38

def initialize(urls = Urls.initial)
  @routes = Array.new
  @urls = urls
end

Instance Method Details

#add_route(request_method, pattern, &block) ⇒ Object



43
44
45
# File 'lib/ruby_event_store/browser/router.rb', line 43

def add_route(request_method, pattern, &block)
  routes << Route.new(request_method, pattern, &block)
end

#handle(request) ⇒ Object

Raises:



47
48
49
50
51
52
53
# File 'lib/ruby_event_store/browser/router.rb', line 47

def handle(request)
  routes.each do |route|
    route_params = route.match(request)
    return route.call(request.params.merge(route_params), urls.with_request(request)) if route_params
  end
  raise NoMatch
end