Skip to content

Commit

Permalink
Merge branch 'master' into ep/remove-example
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasjpr authored Jun 10, 2017
2 parents e9c0813 + 7a8cfd9 commit 35a5d35
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
20 changes: 20 additions & 0 deletions spec/amber/router/router_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ module Amber
end
end
end

describe "#all" do
it "gets all routes defined" do
router = Router.new
handler = ->(context : HTTP::Server::Context, action : Symbol) { "hey" }
routes = [Route.new("GET", "/", handler),
Route.new("GET", "/a", handler),
Route.new("DELETE", "/b", handler),
Route.new("PUT", "/b/c", handler),
Route.new("POST", "/b/c/d", handler),
Route.new("GET", "/e/f", handler) ]
routes.each { |r| router.add r }

router.draw :web do
resources "/comments", HelloController
end

router.all.size.should eq 14
end
end
end
end
end
8 changes: 8 additions & 0 deletions src/amber.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ module Amber
@@instance ||= new
end

def self.routes
instance.all_routes
end

def self.settings
instance
end
Expand Down Expand Up @@ -102,6 +106,10 @@ module Amber
@handler ||= Pipe::Pipeline.new
end

def all_routes
router.all
end

private def router
@router ||= Router::Router.instance
end
Expand Down
2 changes: 1 addition & 1 deletion src/amber/dsl/router.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Amber::DSL
content
}
%verb = {{verb.upcase.id.stringify}}
%route = Amber::Route.new(%verb, {{resource}}, %handler, {{action}}, valve, scope)
%route = Amber::Route.new(%verb, {{resource}}, %handler, {{action}}, valve, scope, "{{controller.id}}")

router.add(%route)
end
Expand Down
18 changes: 16 additions & 2 deletions src/amber/router/route.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
module Amber
class Route
property :handler, :action, :verb, :resource, :valve, :params, :scope
property :handler, :action, :verb, :resource, :valve, :params, :scope, :controller

def initialize(@verb : String,
@resource : String,
@handler : Proc(HTTP::Server::Context, Symbol, String),
@action : Symbol = :index,
@valve : Symbol = :web,
@scope : String = "")
@scope : String = "",
@controller : String = "")
end

def to_json
JSON.build do |json|
json.object do
json.field "verb", verb
json.field "controller", controller
json.field "action", action.to_s
json.field "valve", valve.to_s
json.field "scope", scope
json.field "resource", resource
end
end
end

def trail
Expand Down
17 changes: 17 additions & 0 deletions src/amber/router/router.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ module Amber
match(request.method, request.path)
end

def all
root_node = @routes.root
all_routes = {} of String => String
all_routes[root_node.payload.verb + root_node.payload.resource] = root_node.payload.to_json
add_children(root_node, all_routes)
all_routes
end

def add_children(node, accomulator = {} of String => String)
node.children.each do |c|
if c.payload?
accomulator[c.payload.verb + c.payload.resource] = c.payload.to_json
end
add_children(c, accomulator)
end
end

private def merge_params(params, context)
params.each { |k, v| context.params.add(k.to_s, v) }
end
Expand Down

0 comments on commit 35a5d35

Please sign in to comment.