hanami / router

Ruby/Rack HTTP router
http://hanamirb.org
MIT License
362 stars 92 forks source link

Router inspector #208

Closed jodosha closed 3 years ago

jodosha commented 3 years ago

Feature

Introduce Hanami::Router#to_inspect, which returns a string blob with all the routes formatted for human readability.

Example

Given the following routes, it will return the following inspect:

router = Hanami::Router.new do
  # ROOT
  root to: "home#index"

  # HTTP METHODS
  get "/foo", to: "controller#action"
  post "/foo", to: "controller#action"
  patch "/foo", to: "controller#action"
  put "/foo", to: "controller#action"
  delete "/foo", to: "controller#action"
  trace "/foo", to: "controller#action"
  options "/foo", to: "controller#action"
  link "/foo", to: "controller#action"
  unlink "/foo", to: "controller#action"

  # NAMED ROUTES
  get "/login", to: "sessions#new", as: :login

  # CONSTRAINTS
  get "/constraints/:id/:keyword", to: "constraints#show", id: /\d+/, keyword: /\w+/

  # BLOCK
  get "/block" do
  end

  # ENDPOINT DUCK-TYPING
  get "/proc", to: ->(*) { [200, {}, ["OK"]] }
  get "/class", to: Endpoint
  get "/object", to: Endpoint.new
  get "/anonymous", to: Class.new {}
  get "/anonymous-object", to: Class.new {}.new

  # REDIRECT
  redirect "/redirect", to: "/redirect_destination"
  redirect "/redirect-temporary", to: "/redirect_destination", code: 302

  # SCOPE
  scope "/v1" do
    get "/users", to: ->(*) {}
  end

  # MOUNT
  mount App.new, at: "/app"
end

puts router.to_inspect
GET     /                             home#index                    as :root
GET     /foo                          controller#action
POST    /foo                          controller#action
PATCH   /foo                          controller#action
PUT     /foo                          controller#action
DELETE  /foo                          controller#action
TRACE   /foo                          controller#action
OPTIONS /foo                          controller#action
LINK    /foo                          controller#action
UNLINK  /foo                          controller#action
GET     /login                        sessions#new                  as :login
GET     /constraints/:id/:keyword     constraints#show              (id: /\d+/, keyword: /\w+/)
GET     /block                        (block)
GET     /proc                         (proc)
GET     /class                        Endpoint
GET     /object                       Endpoint
GET     /anonymous                    (class)
GET     /anonymous-object             (class)
GET     /redirect                     /redirect_destination (HTTP 301)
GET     /redirect-temporary           /redirect_destination (HTTP 302)
GET     /v1/users                     (proc)
*       /app                          App

Closes https://github.com/hanami/router/issues/165