Class: Hanami::API

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/api.rb,
lib/hanami/api/dsl.rb,
lib/hanami/api/error.rb,
lib/hanami/api/router.rb,
lib/hanami/api/version.rb,
lib/hanami/api/middleware.rb,
lib/hanami/api/block/context.rb

Overview

Hanami::API

Since:

  • 0.1.0

Defined Under Namespace

Modules: Block, DSL, Middleware Classes: Error, Router

Constant Summary collapse

VERSION =

Since:

  • 0.1.0

"0.3.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deleteObject

Defines a route that accepts DELETE requests for the given path.

Parameters:

  • path (String)

    the relative URL to be matched

  • to (#call)

    the Rack endpoint

  • as (Symbol)

    a unique name for the route

  • constraints (Hash)

    a set of constraints for path variables

  • blk (Proc)

    the anonymous proc to be used as endpoint for the route

See Also:

Since:

  • 0.1.0



195
196
197
# File 'lib/hanami/api.rb', line 195

def self.delete(...)
  @router.delete(...)
end

.getObject

Defines a route that accepts GET requests for the given path. It also defines a route to accept HEAD requests.

Examples:

Proc endpoint

require "hanami/api"

class MyAPI < Hanami::API
  get "/", to: ->(*) { [200, {}, ["OK"]] }
end

Block endpoint

require "hanami/api"

class MyAPI < Hanami::API
  get "/" do
    "OK"
  end
end

Constraints

require "hanami/api"

class MyAPI < Hanami::API
  get "/users/:id", to: ->(*) { [200, {}, ["OK"]] }, id: /\d+/
end

Parameters:

  • path (String)

    the relative URL to be matched

  • to (#call)

    the Rack endpoint

  • as (Symbol)

    a unique name for the route

  • constraints (Hash)

    a set of constraints for path variables

  • blk (Proc)

    the anonymous proc to be used as endpoint for the route

Since:

  • 0.1.0



135
136
137
# File 'lib/hanami/api.rb', line 135

def self.get(...)
  @router.get(...)
end

.helpers(mod = nil, &blk) ⇒ Object

Defines helper methods available within the block context. Helper methods have access to default utilities available in block context (e.g. ‘#halt`).

Examples:

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

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

Parameters:

  • mod (Module) (defaults to: nil)

    optional module to include in block context

  • blk (Proc)

    inline helper definitions

Since:

  • x.x.x



70
71
72
# File 'lib/hanami/api.rb', line 70

def self.helpers(mod = nil, &blk)
  const_get(:BlockContext).include(mod || Module.new(&blk))
end

.inherited(app) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



16
17
18
19
20
21
# File 'lib/hanami/api.rb', line 16

def self.inherited(app)
  super

  app.extend(DSL::ClassMethods)
  app.include(DSL::InstanceMethods)
end

Defines a route that accepts LINK requests for the given path.

Parameters:

  • path (String)

    the relative URL to be matched

  • to (#call)

    the Rack endpoint

  • as (Symbol)

    a unique name for the route

  • constraints (Hash)

    a set of constraints for path variables

  • blk (Proc)

    the anonymous proc to be used as endpoint for the route

See Also:

Since:

  • 0.1.0



240
241
242
# File 'lib/hanami/api.rb', line 240

def self.link(...)
  @router.link(...)
end

.mountObject

Mount a Rack application at the specified path. All the requests starting with the specified path, will be forwarded to the given application.

All the other methods (eg ‘#get`) support callable objects, but they restrict the range of the acceptable HTTP verb. Mounting an application with #mount doesn’t apply this kind of restriction at the router level, but let the application to decide.

Examples:

require "hanami/api"

class MyAPI < Hanami::API
  mount MyRackApp.new, at: "/foo"
end

Parameters:

  • app (#call)

    a class or an object that responds to #call

  • at (String)

    the relative path where to mount the app

  • constraints (Hash)

    a set of constraints for path variables

Since:

  • 0.1.0



318
319
320
# File 'lib/hanami/api.rb', line 318

def self.mount(...)
  @router.mount(...)
end

.optionsObject

Defines a route that accepts OPTIONS requests for the given path.

Parameters:

  • path (String)

    the relative URL to be matched

  • to (#call)

    the Rack endpoint

  • as (Symbol)

    a unique name for the route

  • constraints (Hash)

    a set of constraints for path variables

  • blk (Proc)

    the anonymous proc to be used as endpoint for the route

See Also:

Since:

  • 0.1.0



225
226
227
# File 'lib/hanami/api.rb', line 225

def self.options(...)
  @router.options(...)
end

.patchObject

Defines a route that accepts PATCH requests for the given path.

Parameters:

  • path (String)

    the relative URL to be matched

  • to (#call)

    the Rack endpoint

  • as (Symbol)

    a unique name for the route

  • constraints (Hash)

    a set of constraints for path variables

  • blk (Proc)

    the anonymous proc to be used as endpoint for the route

See Also:

Since:

  • 0.1.0



165
166
167
# File 'lib/hanami/api.rb', line 165

def self.patch(...)
  @router.patch(...)
end

.postObject

Defines a route that accepts POST requests for the given path.

Parameters:

  • path (String)

    the relative URL to be matched

  • to (#call)

    the Rack endpoint

  • as (Symbol)

    a unique name for the route

  • constraints (Hash)

    a set of constraints for path variables

  • blk (Proc)

    the anonymous proc to be used as endpoint for the route

See Also:

Since:

  • 0.1.0



150
151
152
# File 'lib/hanami/api.rb', line 150

def self.post(...)
  @router.post(...)
end

.putObject

Defines a route that accepts PUT requests for the given path.

Parameters:

  • path (String)

    the relative URL to be matched

  • to (#call)

    the Rack endpoint

  • as (Symbol)

    a unique name for the route

  • constraints (Hash)

    a set of constraints for path variables

  • blk (Proc)

    the anonymous proc to be used as endpoint for the route

See Also:

Since:

  • 0.1.0



180
181
182
# File 'lib/hanami/api.rb', line 180

def self.put(...)
  @router.put(...)
end

.redirectObject

Defines a route that redirects the incoming request to another path.

Parameters:

  • path (String)

    the relative URL to be matched

  • to (#call)

    the Rack endpoint

  • as (Symbol)

    a unique name for the route

  • code (Integer)

    a HTTP status code to use for the redirect

See Also:

Since:

  • 0.1.0



269
270
271
# File 'lib/hanami/api.rb', line 269

def self.redirect(...)
  @router.redirect(...)
end

.rootObject

Defines a named root route (a GET route for “/”)

Examples:

Proc endpoint

require "hanami/router"

router = Hanami::Router.new do
  root to: ->(env) { [200, {}, ["Hello from Hanami!"]] }
end

Block endpoint

require "hanami/router"

router = Hanami::Router.new do
  root do
    "Hello from Hanami!"
  end
end

Parameters:

  • to (#call)

    the Rack endpoint

  • blk (Proc)

    the anonymous proc to be used as endpoint for the route

See Also:

Since:

  • 0.1.0



98
99
100
# File 'lib/hanami/api.rb', line 98

def self.root(...)
  @router.root(...)
end

.scopeObject

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.

Examples:

require "hanami/api"

class MyAPI < Hanami::API
  scope "v1" do
    get "/users", to: ->(*) { ... }, as: :users
  end
end

# It generates a route with a path `/v1/users`

Parameters:

  • path (String)

    the scope path to be used as a path prefix

  • blk (Proc)

    the routes definitions withing the scope

See Also:

Since:

  • 0.1.0



293
294
295
# File 'lib/hanami/api.rb', line 293

def self.scope(...)
  @router.scope(...)
end

.traceObject

Defines a route that accepts TRACE requests for the given path.

Parameters:

  • path (String)

    the relative URL to be matched

  • to (#call)

    the Rack endpoint

  • as (Symbol)

    a unique name for the route

  • constraints (Hash)

    a set of constraints for path variables

  • blk (Proc)

    the anonymous proc to be used as endpoint for the route

See Also:

Since:

  • 0.1.0



210
211
212
# File 'lib/hanami/api.rb', line 210

def self.trace(...)
  @router.trace(...)
end

Defines a route that accepts UNLINK requests for the given path.

Parameters:

  • path (String)

    the relative URL to be matched

  • to (#call)

    the Rack endpoint

  • as (Symbol)

    a unique name for the route

  • constraints (Hash)

    a set of constraints for path variables

  • blk (Proc)

    the anonymous proc to be used as endpoint for the route

See Also:

Since:

  • 0.1.0



255
256
257
# File 'lib/hanami/api.rb', line 255

def self.unlink(...)
  @router.unlink(...)
end

.use(middleware, *args, &blk) ⇒ Object

Use a Rack middleware

Examples:

require "hanami/api"

class MyAPI < Hanami::API
  use MyRackMiddleware
end

Parameters:

  • middleware (Class, #call)

    a Rack middleware

  • args (Array<Object>)

    an optional array of arguments for Rack middleware

  • blk (Block)

    an optional block to pass to the Rack middleware

Since:

  • 0.1.0



336
337
338
# File 'lib/hanami/api.rb', line 336

def self.use(middleware, *args, &blk)
  @router.use(middleware, *args, &blk)
end

Instance Method Details

#path(name, variables = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: verify if needed here on in block context

Since:

  • 0.1.0



344
345
346
# File 'lib/hanami/api.rb', line 344

def path(name, variables = {})
  @url_helpers.path(name, variables)
end

#url(name, variables = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: verify if needed here on in block context

Since:

  • 0.1.0



352
353
354
# File 'lib/hanami/api.rb', line 352

def url(name, variables = {})
  @url_helpers.url(name, variables)
end