Class: Hanami::Middleware Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/middleware.rb

Overview

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

Rack middleware stack for an application

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Hanami::Middleware

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.

Instantiate a middleware stack

Parameters:

See Also:

Since:

  • 0.1.0



17
18
19
20
# File 'lib/hanami/middleware.rb', line 17

def initialize(configuration)
  @stack = []
  @configuration = configuration
end

Instance Method Details

#_load_assets_middlewareObject

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.

Use static assets middleware

Since:

  • 0.6.0



134
135
136
137
138
139
140
141
# File 'lib/hanami/middleware.rb', line 134

def _load_assets_middleware
  env = Hanami.environment

  if !env.container? && env.serve_static_assets?
    require 'hanami/static'
    use Hanami::Static
  end
end

#_load_default_welcome_page_for(application) ⇒ 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.

Default welcome page

Since:

  • 0.2.0



113
114
115
116
117
118
# File 'lib/hanami/middleware.rb', line 113

def _load_default_welcome_page_for(application)
  unless Hanami.env?(:test) || application.routes.defined?
    require 'hanami/welcome'
    use Hanami::Welcome
  end
end

#_load_session_middlewareObject

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.

Add session middleware

Since:

  • 0.2.0



124
125
126
127
128
# File 'lib/hanami/middleware.rb', line 124

def _load_session_middleware
  if @configuration.sessions.enabled?
    prepend(*@configuration.sessions.middleware)
  end
end

#call(env) ⇒ Array

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.

Process a request. This method makes the middleware stack compatible with the Rack protocol.

Parameters:

  • env (Hash)

    a Rack env

Returns:

  • (Array)

    a serialized Rack response

Since:

  • 0.1.0



51
52
53
# File 'lib/hanami/middleware.rb', line 51

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

#load!(application, namespace) ⇒ Hanami::Middleware

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.

Load the middleware stack

Parameters:

Returns:

See Also:

Since:

  • 0.2.0



32
33
34
35
36
37
38
39
40
# File 'lib/hanami/middleware.rb', line 32

def load!(application, namespace)
  @namespace = namespace
  @builder = ::Rack::Builder.new
  load_default_stack(application)
  @stack.each { |m, args, block| @builder.use load_middleware(m), *args, &block }
  @builder.run application.routes

  self
end

#load_default_stack(application) ⇒ 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.2.0



98
99
100
101
102
103
104
105
106
107
# File 'lib/hanami/middleware.rb', line 98

def load_default_stack(application)
  @default_stack_loaded ||= begin
    _load_assets_middleware
    _load_session_middleware
    _load_default_welcome_page_for(application)
    use Rack::MethodOverride

    true
  end
end

#load_middleware(middleware) ⇒ 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.2.0



87
88
89
90
91
92
93
94
# File 'lib/hanami/middleware.rb', line 87

def load_middleware(middleware)
  case middleware
  when String
    @namespace.const_get(middleware)
  else
    middleware
  end
end

#prepend(middleware, *args, &blk) ⇒ Array

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.

Prepend a middleware to the stack.

Parameters:

  • middleware (Object)

    a Rack middleware

  • args (Array)

    optional arguments to pass to the Rack middleware

  • blk (Proc)

    an optional block to pass to the Rack middleware

Returns:

  • (Array)

    the middleware that was added

See Also:

Since:

  • 0.6.0



81
82
83
# File 'lib/hanami/middleware.rb', line 81

def prepend(middleware, *args, &blk)
  @stack.unshift [middleware, args, blk]
end

#use(middleware, *args, &blk) ⇒ Array

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.

Append a middleware to the stack.

Parameters:

  • middleware (Object)

    a Rack middleware

  • args (Array)

    optional arguments to pass to the Rack middleware

  • blk (Proc)

    an optional block to pass to the Rack middleware

Returns:

  • (Array)

    the middleware that was added

See Also:

Since:

  • 0.2.0



66
67
68
# File 'lib/hanami/middleware.rb', line 66

def use(middleware, *args, &blk)
  @stack.push [middleware, args, blk]
end