Class: Hanami::Action Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/action.rb,
lib/hanami/action/halt.rb,
lib/hanami/action/mime.rb,
lib/hanami/action/cache.rb,
lib/hanami/action/flash.rb,
lib/hanami/action/config.rb,
lib/hanami/action/errors.rb,
lib/hanami/action/params.rb,
lib/hanami/action/cookies.rb,
lib/hanami/action/request.rb,
lib/hanami/action/session.rb,
lib/hanami/action/response.rb,
lib/hanami/action/constants.rb,
lib/hanami/action/rack/file.rb,
lib/hanami/action/cookie_jar.rb,
lib/hanami/action/base_params.rb,
lib/hanami/action/validatable.rb,
lib/hanami/action/cache/expires.rb,
lib/hanami/action/config/formats.rb,
lib/hanami/action/csrf_protection.rb,
lib/hanami/action/cache/directives.rb,
lib/hanami/action/view_name_inferrer.rb,
lib/hanami/action/cache/cache_control.rb,
lib/hanami/action/cache/conditional_get.rb,
lib/hanami/action/mime/request_mime_weight.rb

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.

Defined Under Namespace

Modules: CSRFProtection, Cache, Cookies, Halt, Mime, Rack, Session, Validatable Classes: BaseParams, Config, CookieJar, Error, Flash, InvalidCSRFTokenError, MissingSessionError, Params, Request, Response, UnknownFormatError, UnknownHttpStatusError, ViewNameInferrer

Constant Summary collapse

RESPONSE_CODE =

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

Rack SPEC response code

Since:

  • 1.0.0

0
RESPONSE_HEADERS =

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

Rack SPEC response headers

Since:

  • 1.0.0

1
RESPONSE_BODY =

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

Rack SPEC response body

Since:

  • 1.0.0

2
DEFAULT_ERROR_CODE =

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

Since:

  • 1.0.0

500
HTTP_STATUSES_WITHOUT_BODY =

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

Status codes that by RFC must not include a message body

Since:

  • 0.3.2

Set.new((100..199).to_a << 204 << 205 << 304).freeze
NOT_FOUND =

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

Not Found

Since:

  • 1.0.0

404
ENTITY_HEADERS =

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

Entity headers allowed in blank body responses, according to RFC 2616 - Section 10 (HTTP 1.1).

“The response MAY include new or updated metainformation in the form

of entity-headers".
{
  "Allow" => true,
  "Content-Encoding" => true,
  "Content-Language" => true,
  "Content-Location" => true,
  "Content-MD5" => true,
  "Content-Range" => true,
  "Expires" => true,
  "Last-Modified" => true,
  "extension-header" => true
}.freeze
PATH_INFO =

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

The request relative path

Since:

  • 2.0.0

::Rack::PATH_INFO
REQUEST_METHOD =

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

The request method

Since:

  • 0.3.2

::Rack::REQUEST_METHOD
CONTENT_LENGTH =

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

The Content-Length HTTP header

Since:

  • 1.0.0

::Rack::CONTENT_LENGTH
X_CASCADE =

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

The non-standard HTTP header to pass the control over when a resource cannot be found by the current endpoint

Since:

  • 1.0.0

"X-Cascade"
HEAD =

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

HEAD request

Since:

  • 0.3.2

::Rack::HEAD
GET =

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

GET request

Since:

  • 2.0.0

::Rack::GET
TRACE =

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

TRACE request

Since:

  • 2.0.0

::Rack::TRACE
OPTIONS =

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

OPTIONS request

Since:

  • 2.0.0

::Rack::OPTIONS
HTTP_ACCEPT =

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

The key that returns accepted mime types from the Rack env

Since:

  • 0.1.0

"HTTP_ACCEPT"
DEFAULT_ACCEPT =

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

The default mime type for an incoming HTTP request

Since:

  • 0.1.0

"*/*"
DEFAULT_CONTENT_TYPE =

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

The default mime type that is returned in the response

Since:

  • 0.1.0

"application/octet-stream"
RACK_ERRORS =

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

Since:

  • 0.2.0

::Rack::RACK_ERRORS
CACHE_CONTROL =

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

The HTTP header for Cache-Control

Since:

  • 2.0.0

::Rack::CACHE_CONTROL
IF_NONE_MATCH =

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

Since:

  • 2.0.0

"HTTP_IF_NONE_MATCH"
ETAG =

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

The HTTP header for ETag

Since:

  • 2.0.0

::Rack::ETAG
IF_MODIFIED_SINCE =

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

Since:

  • 2.0.0

"HTTP_IF_MODIFIED_SINCE"
EXPIRES =

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

The HTTP header for Expires

Since:

  • 2.0.0

::Rack::EXPIRES
LAST_MODIFIED =

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

The HTTP header for Last-Modified

Since:

  • 0.3.0

"Last-Modified"
RACK_EXCEPTION =

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

This isn’t part of Rack SPEC

Exception notifiers use rack.exception instead of rack.errors, so we need to support it.

"rack.exception"
LOCATION =

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

The HTTP header for redirects

Since:

  • 0.2.0

"Location"
RACK_SESSION =

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

The key that returns Rack session params from the Rack env Please note that this is used only when an action is unit tested.

Examples:

# action unit test
action.call("rack.session" => { "foo" => "bar" })
action.session[:foo] # => "bar"

Since:

  • 2.0.0

::Rack::RACK_SESSION
REQUEST_ID =

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

Since:

  • 2.0.0

"hanami.request_id"
DEFAULT_ID_LENGTH =

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

Since:

  • 2.0.0

16
::Rack::HTTP_COOKIE
::Rack::RACK_REQUEST_COOKIE_HASH
::Rack::RACK_REQUEST_COOKIE_STRING
RACK_INPUT =

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

The key that returns raw input from the Rack env

Since:

  • 2.0.0

::Rack::RACK_INPUT
ROUTER_PARAMS =

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

The key that returns router params from the Rack env This is a builtin integration for Hanami::Router

Since:

  • 2.0.0

"router.params"
DEFAULT_REQUEST_METHOD =

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

Default HTTP request method for Rack env

Since:

  • 2.0.0

GET
DEFAULT_CHARSET =

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

Since:

  • 2.0.0

"utf-8"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: self.class.config) ⇒ Action

Returns a new action

Since:

  • 2.0.0



294
295
296
297
# File 'lib/hanami/action.rb', line 294

def initialize(config: self.class.config)
  @config = config
  freeze
end

Class Method Details

.self.append_after(*callbacks, &block) ⇒ void Also known as: after

This method returns an undefined value.

Define a callback for an Action. The callback will be executed after the action is called, in the order they are added.

Parameters:

  • callbacks (Symbol, Array<Symbol>)

    a single or multiple symbol(s) each of them is representing a name of a method available in the context of the Action.

  • blk (Proc)

    an anonymous function to be executed

See Also:

  • Hanami::Action::Callbacks::ClassMethods#append_before

Since:

  • 0.3.2

Since:

  • 0.1.0



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

def self.append_after(...)
  config.after_callbacks.append(...)
end

.self.append_before(*callbacks, &block) ⇒ void Also known as: before

This method returns an undefined value.

Define a callback for an Action. The callback will be executed before the action is called, in the order they are added.

Examples:

Method names (symbols)

require "hanami/controller"

class Show < Hanami::Action
  before :authenticate, :set_article

  def handle(req, res)
  end

  private
  def authenticate
    # ...
  end

  # `params` in the method signature is optional
  def set_article(params)
    @article = Article.find params[:id]
  end
end

# The order of execution will be:
#
# 1. #authenticate
# 2. #set_article
# 3. #call

Anonymous functions (Procs)

require "hanami/controller"

class Show < Hanami::Action
  before { ... } # 1 do some authentication stuff
  before {|req, res| @article = Article.find params[:id] } # 2

  def handle(req, res)
  end
end

# The order of execution will be:
#
# 1. authentication
# 2. set the article
# 3. `#handle`

Parameters:

  • callbacks (Symbol, Array<Symbol>)

    a single or multiple symbol(s) each of them is representing a name of a method available in the context of the Action.

  • blk (Proc)

    an anonymous function to be executed

See Also:

  • Hanami::Action::Callbacks::ClassMethods#append_after

Since:

  • 0.3.2

Since:

  • 0.1.0



200
201
202
# File 'lib/hanami/action.rb', line 200

def self.append_before(...)
  config.before_callbacks.append(...)
end

.configConfig

Returns the action’s config. Use this to configure your action.

Examples:

Access inside class body

class Show < Hanami::Action
  config.format :json
end

Returns:

Since:

  • 2.0.0



# File 'lib/hanami/action.rb', line 78

.formatObject

See Also:

Since:

  • 2.0.0



278
279
280
# File 'lib/hanami/action.rb', line 278

def self.format(...)
  config.format(...)
end

.gem_loaderObject

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:

  • 2.0.0



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hanami/action.rb', line 33

def self.gem_loader
  @gem_loader ||= Zeitwerk::Loader.new.tap do |loader|
    root = File.expand_path("..", __dir__)
    loader.tag = "hanami-controller"
    loader.inflector = Zeitwerk::GemInflector.new("#{root}/hanami-controller.rb")
    loader.push_dir(root)
    loader.ignore(
      "#{root}/hanami-controller.rb",
      "#{root}/hanami/controller/version.rb",
      "#{root}/hanami/action/{constants,errors,params,validatable}.rb"
    )
    loader.inflector.inflect("csrf_protection" => "CSRFProtection")
  end
end

.handle_exceptionObject



286
287
288
# File 'lib/hanami/action.rb', line 286

def self.handle_exception(...)
  config.handle_exception(...)
end

.inherited(subclass) ⇒ 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.

Override Ruby’s hook for modules. It includes basic Hanami::Action modules to the given class.

Parameters:

  • subclass (Class)

    the target action

Since:

  • 0.1.0



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hanami/action.rb', line 100

def self.inherited(subclass)
  super

  if subclass.superclass == Action
    subclass.class_eval do
      include Validatable if defined?(Validatable)
    end
  end

  if instance_variable_defined?(:@params_class)
    subclass.instance_variable_set(:@params_class, @params_class)
  end
end

.params(_klass = nil) ⇒ Object

Placeholder implementation for params class method

Raises a developer friendly error to include ‘hanami/validations`.

Raises:

  • (NoMethodError)

Since:

  • 2.0.0



136
137
138
139
# File 'lib/hanami/action.rb', line 136

def self.params(_klass = nil)
  raise NoMethodError,
        "To use `params`, please add 'hanami/validations' gem to your Gemfile"
end

.params_classClass

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.

Returns the class which defines the params

Returns the class which has been provided to define the params. By default this will be Hanami::Action::Params.

Returns:

  • (Class)

    A params class (when whitelisted) or Hanami::Action::Params

Since:

  • 0.7.0



124
125
126
# File 'lib/hanami/action.rb', line 124

def self.params_class
  @params_class || BaseParams
end

.self.prepend_after(*callbacks, &block) ⇒ void

This method returns an undefined value.

Define a callback for an Action. The callback will be executed after the action is called. It will add the callback at the beginning of the callbacks’ chain.

Parameters:

  • callbacks (Symbol, Array<Symbol>)

    a single or multiple symbol(s) each of them is representing a name of a method available in the context of the Action.

  • blk (Proc)

    an anonymous function to be executed

See Also:

  • Hanami::Action::Callbacks::ClassMethods#prepend_before

Since:

  • 0.3.2

Since:

  • 0.1.0



270
271
272
# File 'lib/hanami/action.rb', line 270

def self.prepend_after(...)
  config.after_callbacks.prepend(...)
end

.self.prepend_before(*callbacks, &block) ⇒ void

This method returns an undefined value.

Define a callback for an Action. The callback will be executed before the action is called. It will add the callback at the beginning of the callbacks’ chain.

Parameters:

  • callbacks (Symbol, Array<Symbol>)

    a single or multiple symbol(s) each of them is representing a name of a method available in the context of the Action.

  • blk (Proc)

    an anonymous function to be executed

See Also:

  • Hanami::Action::Callbacks::ClassMethods#prepend_after

Since:

  • 0.3.2

Since:

  • 0.1.0



250
251
252
# File 'lib/hanami/action.rb', line 250

def self.prepend_before(...)
  config.before_callbacks.prepend(...)
end

Instance Method Details

#call(env) ⇒ 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.

Implements the Rack/Hanami::Action protocol

Since:

  • 0.1.0



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/hanami/action.rb', line 303

def call(env)
  request  = nil
  response = nil

  halted = catch :halt do
    params   = self.class.params_class.new(env)
    request  = build_request(
      env: env,
      params: params,
      session_enabled: session_enabled?
    )
    response = build_response(
      request: request,
      config: config,
      content_type: Mime.response_content_type_with_charset(request, config),
      env: env,
      headers: config.default_headers,
      session_enabled: session_enabled?
    )

    enforce_accepted_mime_types(request)

    _run_before_callbacks(request, response)
    handle(request, response)
    _run_after_callbacks(request, response)
  rescue StandardError => exception
    _handle_exception(request, response, exception)
  end

  finish(request, response, halted)
end