lib/hanami/api.rb in hanami-api-0.2.0 vs lib/hanami/api.rb in hanami-api-0.3.0

- old
+ new

@@ -18,10 +18,61 @@ app.extend(DSL::ClassMethods) app.include(DSL::InstanceMethods) end + # Defines helper methods available within the block context. + # Helper methods have access to default utilities available in block + # context (e.g. `#halt`). + # + # @param mod [Module] optional module to include in block context + # @param blk [Proc] inline helper definitions + # + # @since x.x.x + # + # @example Inline helpers definition + # require "hanami/api" + # + # class MyAPI < Hanami::API + # helpers do + # def redirect_to_root + # # redirect method is provided by Hanami::API block context + # redirect "/" + # end + # end + # + # root { "Hello, World" } + # + # get "/legacy" do + # redirect_to_root + # end + # end + # + # @example Module helpers definition + # require "hanami/api" + # + # class MyAPI < Hanami::API + # module Authentication + # private + # + # def unauthorized + # halt(401) + # end + # end + # + # helpers(Authentication) + # + # root { "Hello, World" } + # + # get "/secrets" do + # unauthorized + # end + # end + def self.helpers(mod = nil, &blk) + const_get(:BlockContext).include(mod || Module.new(&blk)) + end + # Defines a named root route (a GET route for "/") # # @param to [#call] the Rack endpoint # @param blk [Proc] the anonymous proc to be used as endpoint for the route # @@ -42,12 +93,12 @@ # router = Hanami::Router.new do # root do # "Hello from Hanami!" # end # end - def self.root(*args, **kwargs, &blk) - @router.root(*args, **kwargs, &blk) + def self.root(...) + @router.root(...) end # Defines a route that accepts GET requests for the given path. # It also defines a route to accept HEAD requests. # @@ -79,12 +130,12 @@ # require "hanami/api" # # class MyAPI < Hanami::API # get "/users/:id", to: ->(*) { [200, {}, ["OK"]] }, id: /\d+/ # end - def self.get(*args, **kwargs, &blk) - @router.get(*args, **kwargs, &blk) + def self.get(...) + @router.get(...) end # Defines a route that accepts POST requests for the given path. # # @param path [String] the relative URL to be matched @@ -94,12 +145,12 @@ # @param blk [Proc] the anonymous proc to be used as endpoint for the route # # @since 0.1.0 # # @see .get - def self.post(*args, **kwargs, &blk) - @router.post(*args, **kwargs, &blk) + def self.post(...) + @router.post(...) end # Defines a route that accepts PATCH requests for the given path. # # @param path [String] the relative URL to be matched @@ -109,12 +160,12 @@ # @param blk [Proc] the anonymous proc to be used as endpoint for the route # # @since 0.1.0 # # @see .get - def self.patch(*args, **kwargs, &blk) - @router.patch(*args, **kwargs, &blk) + def self.patch(...) + @router.patch(...) end # Defines a route that accepts PUT requests for the given path. # # @param path [String] the relative URL to be matched @@ -124,12 +175,12 @@ # @param blk [Proc] the anonymous proc to be used as endpoint for the route # # @since 0.1.0 # # @see .get - def self.put(*args, **kwargs, &blk) - @router.put(*args, **kwargs, &blk) + def self.put(...) + @router.put(...) end # Defines a route that accepts DELETE requests for the given path. # # @param path [String] the relative URL to be matched @@ -139,12 +190,12 @@ # @param blk [Proc] the anonymous proc to be used as endpoint for the route # # @since 0.1.0 # # @see .get - def self.delete(*args, **kwargs, &blk) - @router.delete(*args, **kwargs, &blk) + def self.delete(...) + @router.delete(...) end # Defines a route that accepts TRACE requests for the given path. # # @param path [String] the relative URL to be matched @@ -154,12 +205,12 @@ # @param blk [Proc] the anonymous proc to be used as endpoint for the route # # @since 0.1.0 # # @see .get - def self.trace(*args, **kwargs, &blk) - @router.trace(*args, **kwargs, &blk) + def self.trace(...) + @router.trace(...) end # Defines a route that accepts OPTIONS requests for the given path. # # @param path [String] the relative URL to be matched @@ -169,12 +220,12 @@ # @param blk [Proc] the anonymous proc to be used as endpoint for the route # # @since 0.1.0 # # @see .get - def self.options(*args, **kwargs, &blk) - @router.options(*args, **kwargs, &blk) + def self.options(...) + @router.options(...) end # Defines a route that accepts LINK requests for the given path. # # @param path [String] the relative URL to be matched @@ -184,12 +235,12 @@ # @param blk [Proc] the anonymous proc to be used as endpoint for the route # # @since 0.1.0 # # @see .get - def self.link(*args, **kwargs, &blk) - @router.link(*args, **kwargs, &blk) + def self.link(...) + @router.link(...) end # Defines a route that accepts UNLINK requests for the given path. # # @param path [String] the relative URL to be matched @@ -199,12 +250,12 @@ # @param blk [Proc] the anonymous proc to be used as endpoint for the route # # @since 0.1.0 # # @see .get - def self.unlink(*args, **kwargs, &blk) - @router.unlink(*args, **kwargs, &blk) + def self.unlink(...) + @router.unlink(...) end # Defines a route that redirects the incoming request to another path. # # @param path [String] the relative URL to be matched @@ -213,12 +264,12 @@ # @param code [Integer] a HTTP status code to use for the redirect # # @since 0.1.0 # # @see .get - def self.redirect(*args, **kwargs, &blk) - @router.redirect(*args, **kwargs, &blk) + def self.redirect(...) + @router.redirect(...) end # Defines a routing scope. Routes defined in the context of a scope, # inherit the given path as path prefix and as a named routes prefix. # @@ -237,12 +288,12 @@ # get "/users", to: ->(*) { ... }, as: :users # end # end # # # It generates a route with a path `/v1/users` - def self.scope(*args, **kwargs, &blk) - @router.scope(*args, **kwargs, &blk) + def self.scope(...) + @router.scope(...) end # Mount a Rack application at the specified path. # All the requests starting with the specified path, will be forwarded to # the given application. @@ -262,11 +313,11 @@ # require "hanami/api" # # class MyAPI < Hanami::API # mount MyRackApp.new, at: "/foo" # end - def self.mount(*args, **kwargs, &blk) - @router.mount(*args, **kwargs, &blk) + def self.mount(...) + @router.mount(...) end # Use a Rack middleware # # @param middleware [Class,#call] a Rack middleware