Skip to content

Commit

Permalink
[FInishes 114505177] Match route's path to endoint
Browse files Browse the repository at this point in the history
  • Loading branch information
andela-ydaniju committed Feb 26, 2016
1 parent 9f87eaa commit 55f0115
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/algernon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
require "algernon/dependencies"
require "utility/utility"
require "algernon/application"
require "algernon/route/router"
require "algernon/routes/router"
30 changes: 30 additions & 0 deletions lib/algernon/routes/finder.rb
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
19 changes: 19 additions & 0 deletions lib/algernon/routes/route.rb
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
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)
Expand Down
14 changes: 7 additions & 7 deletions spec/unit/router_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "spec_helper"

module Algernon
module Route
module Routes
class Router
attr_reader :route_info

Expand All @@ -13,9 +13,9 @@ def draw(&block)
end
end

describe Algernon::Route::Router do
describe Algernon::Routes::Router do
def draw(&block)
router = Algernon::Route::Router.new
router = Algernon::Routes::Router.new
router.draw(&block).route_info
end

Expand Down Expand Up @@ -52,7 +52,7 @@ def endpoint
end

context "get '/photos/:id/edit', to: 'photos#edit'" do
subject do
def endpoint
draw { get "/photos/:id/edit", to: "photos#edit" }
end

Expand All @@ -62,11 +62,11 @@ def endpoint
class_and_method: ["PhotosController", :edit]
}

it { is_expected.to eq route_info }
it { expect(endpoint).to eq route_info }
end

context "get 'album/:album_id/photos/:photo_id', to: 'photos#album_photo'" do
subject do
def endpoint
draw { get "/album/:album_id/photos/:photo_id", to: "photos#album_photo" }
end

Expand All @@ -76,6 +76,6 @@ def endpoint
class_and_method: ["PhotosController", :album_photo]
}

it { is_expected.to eq route_info }
it { expect(endpoint).to eq route_info }
end
end

0 comments on commit 55f0115

Please sign in to comment.