-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FInishes 114505177] Match route's path to endoint
- Loading branch information
andela-ydaniju
committed
Feb 26, 2016
1 parent
9f87eaa
commit 55f0115
Showing
5 changed files
with
58 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Algernon | ||
module Routes | ||
class Finder | ||
def intialize(endpoints) | ||
@endpoints = endpoints | ||
end | ||
|
||
def locate_route(request) | ||
@request = request | ||
path = request.path_info | ||
method = request.request_method.downcase.to_sym | ||
result = @endpoints[method].detect do |endpoint| | ||
find_path_with_pattern path, endpoint | ||
end | ||
return Route.new(@request, result[:class_and_method]) if result | ||
end | ||
|
||
def find_path_with_pattern(path, endpoint) | ||
regex, placeholders = endpoint[:pattern] | ||
if path.match(regex) | ||
match_data = Regexp.last_match | ||
placeholders.each do |placeholder| | ||
@request.update_param(placeholder, match_data[placeholder]) | ||
end | ||
true | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Algernon | ||
module Routes | ||
class Route | ||
attr_reader :klass_name, :request, :method_name | ||
def initialize(request, klass_and_method) | ||
@klass_name, @method_name = klass_and_method | ||
@request = request | ||
end | ||
|
||
def klass | ||
klass_name.constantize | ||
end | ||
|
||
def dispatch | ||
klass.new(request).send(method_name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Algernon | ||
module Route | ||
module Routes | ||
class Router | ||
def draw(&block) | ||
instance_eval(&block) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters