lib/hanami/router.rb in hanami-router-0.6.2 vs lib/hanami/router.rb in hanami-router-0.7.0

- old
+ new

@@ -89,10 +89,18 @@ def initialize(env) super %(Cannot find routable endpoint for #{ env[REQUEST_METHOD] } "#{ env[PATH_INFO] }") end end + # Defines root path + # + # @since 0.7.0 + # @api private + # + # @see Hanami::Router#root + ROOT_PATH = '/'.freeze + # Returns the given block as it is. # # When Hanami::Router is used as a standalone gem and the routes are defined # into a configuration file, some systems could raise an exception. # @@ -487,9 +495,39 @@ # @see Hanami::Router#get # # @since 0.1.0 def trace(path, options = {}, &blk) @router.trace(path, options, &blk) + end + + # Defines a root route (a GET route for '/') + # + # @param options [Hash] the options to customize the route + # @option options [String,Proc,Class,Object#call] :to the endpoint + # + # @param blk [Proc] the anonymous proc to be used as endpoint for the route + # + # @return [Hanami::Routing::Route] this may vary according to the :route + # option passed to the constructor + # + # @since 0.7.0 + # + # @example Fixed matching string + # require 'hanami/router' + # + # router = Hanami::Router.new + # router.root to: ->(env) { [200, {}, ['Hello from Hanami!']] } + # + # @example Included names as `root` (for path and url helpers) + # require 'hanami/router' + # + # router = Hanami::Router.new(scheme: 'https', host: 'hanamirb.org') + # router.root to: ->(env) { [200, {}, ['Hello from Hanami!']] } + # + # router.path(:root) # => "/" + # router.url(:root) # => "https://hanamirb.org/" + def root(options = {}, &blk) + @router.get(ROOT_PATH, options.merge(as: :root), &blk) end # Defines a route that accepts a OPTIONS request for the given path. # # @param path [String] the relative URL to be matched