Class: Hanami::Action::Request

Inherits:
Rack::Request
  • Object
show all
Defined in:
lib/hanami/action/request.rb

Overview

The HTTP request for an action, given to #handle.

Inherits from ‘Rack::Request`, providing compatibility with Rack functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:, params:, session_enabled: false) ⇒ Request

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 a new instance of Request.

Since:

  • 2.0.0



32
33
34
35
36
37
# File 'lib/hanami/action/request.rb', line 32

def initialize(env:, params:, session_enabled: false)
  super(env)

  @params = params
  @session_enabled = session_enabled
end

Instance Attribute Details

#paramsBaseParams, Params (readonly)

Returns the request’s params.

For an action with Validatable included, this will be a Params instance, otherwise a BaseParams.

Returns:

Since:

  • 2.0.0



28
29
30
# File 'lib/hanami/action/request.rb', line 28

def params
  @params
end

Instance Method Details

#acceptObject

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



113
114
115
# File 'lib/hanami/action/request.rb', line 113

def accept
  @accept ||= @env[Action::HTTP_ACCEPT] || Action::DEFAULT_ACCEPT
end

#accept?(mime_type) ⇒ Boolean

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:

  • (Boolean)

Since:

  • 2.0.0



99
100
101
102
103
# File 'lib/hanami/action/request.rb', line 99

def accept?(mime_type)
  !!::Rack::Utils.q_values(accept).find do |mime, _|
    ::Rack::Mime.match?(mime_type, mime)
  end
end

#accept_header?Boolean

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:

  • (Boolean)

Since:

  • 2.0.0



107
108
109
# File 'lib/hanami/action/request.rb', line 107

def accept_header?
  accept != Action::DEFAULT_ACCEPT
end

#flashFlash

Returns the flash for the request.

Returns:

Raises:

See Also:

Since:

  • 2.0.0



89
90
91
92
93
94
95
# File 'lib/hanami/action/request.rb', line 89

def flash
  unless session_enabled?
    raise Hanami::Action::MissingSessionError.new("Hanami::Action::Request#flash")
  end

  @flash ||= Flash.new(session[Flash::KEY])
end

#idString

Returns the request’s ID

Returns:

  • (String)

Since:

  • 2.0.0



45
46
47
48
# File 'lib/hanami/action/request.rb', line 45

def id
  # FIXME: make this number configurable and document the probabilities of clashes
  @id ||= @env[Action::REQUEST_ID] = SecureRandom.hex(Action::DEFAULT_ID_LENGTH)
end

#sessionHash

Returns the session for the request.

Returns:

  • (Hash)

    the session object

Raises:

See Also:

Since:

  • 2.0.0



71
72
73
74
75
76
77
# File 'lib/hanami/action/request.rb', line 71

def session
  unless session_enabled?
    raise Hanami::Action::MissingSessionError.new("Hanami::Action::Request#session")
  end

  super
end

#session_enabled?Boolean

Returns true if the session is enabled for the request.

Returns:

  • (Boolean)

Since:

  • 2.1.0



56
57
58
# File 'lib/hanami/action/request.rb', line 56

def session_enabled?
  @session_enabled
end