Class: Hanami::API

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/api.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,
lib/hanami/api/middleware/app.rb,
lib/hanami/api/middleware/node.rb,
lib/hanami/api/middleware/trie.rb

Overview

Hanami::API

Since:

  • 0.1.0

Defined Under Namespace

Modules: Block, Middleware Classes: Error, Router

Constant Summary collapse

VERSION =

Since:

  • 0.1.0

"0.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router: self.class.router) ⇒ API

Returns a new instance of API.

Since:

  • 0.1.0



296
297
298
299
300
# File 'lib/hanami/api.rb', line 296

def initialize(router: self.class.router)
  @router = router

  freeze
end

Class Attribute Details

.routerObject (readonly)

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.1



26
27
28
# File 'lib/hanami/api.rb', line 26

def router
  @router
end

Class Method Details

.delete(*args, **kwargs, &blk) ⇒ Object

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



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

def self.delete(*args, **kwargs, &blk)
  @router.delete(*args, **kwargs, &blk)
end

.get(*args, **kwargs, &blk) ⇒ Object

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



90
91
92
# File 'lib/hanami/api.rb', line 90

def self.get(*args, **kwargs, &blk)
  @router.get(*args, **kwargs, &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



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

def self.inherited(app)
  super

  app.class_eval do
    @router = Router.new
  end
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



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

def self.link(*args, **kwargs, &blk)
  @router.link(*args, **kwargs, &blk)
end

.mount(*args, **kwargs, &blk) ⇒ Object

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



273
274
275
# File 'lib/hanami/api.rb', line 273

def self.mount(*args, **kwargs, &blk)
  @router.mount(*args, **kwargs, &blk)
end

.options(*args, **kwargs, &blk) ⇒ Object

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



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

def self.options(*args, **kwargs, &blk)
  @router.options(*args, **kwargs, &blk)
end

.patch(*args, **kwargs, &blk) ⇒ Object

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



120
121
122
# File 'lib/hanami/api.rb', line 120

def self.patch(*args, **kwargs, &blk)
  @router.patch(*args, **kwargs, &blk)
end

.post(*args, **kwargs, &blk) ⇒ Object

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



105
106
107
# File 'lib/hanami/api.rb', line 105

def self.post(*args, **kwargs, &blk)
  @router.post(*args, **kwargs, &blk)
end

.put(*args, **kwargs, &blk) ⇒ Object

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



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

def self.put(*args, **kwargs, &blk)
  @router.put(*args, **kwargs, &blk)
end

.redirect(*args, **kwargs, &blk) ⇒ Object

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



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

def self.redirect(*args, **kwargs, &blk)
  @router.redirect(*args, **kwargs, &blk)
end

.root(*args, **kwargs, &blk) ⇒ Object

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



53
54
55
# File 'lib/hanami/api.rb', line 53

def self.root(*args, **kwargs, &blk)
  @router.root(*args, **kwargs, &blk)
end

.scope(*args, **kwargs, &blk) ⇒ Object

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



248
249
250
# File 'lib/hanami/api.rb', line 248

def self.scope(*args, **kwargs, &blk)
  @router.scope(*args, **kwargs, &blk)
end

.trace(*args, **kwargs, &blk) ⇒ Object

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



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

def self.trace(*args, **kwargs, &blk)
  @router.trace(*args, **kwargs, &blk)
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



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

def self.unlink(*args, **kwargs, &blk)
  @router.unlink(*args, **kwargs, &blk)
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



291
292
293
# File 'lib/hanami/api.rb', line 291

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

Instance Method Details

#call(env) ⇒ Object

Since:

  • 0.1.0



314
315
316
# File 'lib/hanami/api.rb', line 314

def call(env)
  @app.call(env)
end

#freezeObject

Since:

  • 0.1.0



303
304
305
306
307
308
309
310
311
# File 'lib/hanami/api.rb', line 303

def freeze
  @app = @router.to_rack_app
  @url_helpers = @router.url_helpers
  @router.remove_instance_variable(:@url_helpers)
  remove_instance_variable(:@router)
  @url_helpers.freeze
  @app.freeze
  super
end

#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



322
323
324
# File 'lib/hanami/api.rb', line 322

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



330
331
332
# File 'lib/hanami/api.rb', line 330

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