Class: Hanami::Action::Request
- Inherits:
-
Rack::Request
- Object
- Rack::Request
- Hanami::Action::Request
- Defined in:
- lib/hanami/action/request.rb,
lib/hanami/action/request/session.rb
Overview
The HTTP request for an action, given to #handle.
Inherits from ‘Rack::Request`, providing compatibility with Rack functionality.
Defined Under Namespace
Classes: Session
Instance Attribute Summary collapse
-
#params ⇒ Params
readonly
Returns the request’s params.
Instance Method Summary collapse
- #accept ⇒ Object private
- #accept?(mime_type) ⇒ Boolean private
- #accept_header? ⇒ Boolean private
-
#flash ⇒ Flash
Returns the flash for the request.
-
#id ⇒ String
Returns the request’s ID.
-
#initialize(env:, params:, default_tld_length: 1, session_enabled: false) ⇒ Request
constructor
private
A new instance of Request.
-
#session ⇒ Hanami::Request::Session
Returns the session for the request.
-
#session_enabled? ⇒ Boolean
Returns true if the session is enabled for the request.
-
#subdomain(tld_length = @default_tld_length) ⇒ String
Returns the subdomain for the current host.
-
#subdomains(tld_length = @default_tld_length) ⇒ Array<String>
Returns the subdomains for the current host.
Constructor Details
#initialize(env:, params:, default_tld_length: 1, 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.
29 30 31 32 33 34 35 |
# File 'lib/hanami/action/request.rb', line 29 def initialize(env:, params:, default_tld_length: 1, session_enabled: false) super(env) @params = params @session_enabled = session_enabled @default_tld_length = default_tld_length end |
Instance Attribute Details
#params ⇒ Params (readonly)
Returns the request’s params.
25 26 27 |
# File 'lib/hanami/action/request.rb', line 25 def params @params end |
Instance Method Details
#accept ⇒ 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.
136 137 138 |
# File 'lib/hanami/action/request.rb', line 136 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.
122 123 124 125 126 |
# File 'lib/hanami/action/request.rb', line 122 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.
130 131 132 |
# File 'lib/hanami/action/request.rb', line 130 def accept_header? accept != Action::DEFAULT_ACCEPT end |
#flash ⇒ Flash
Returns the flash for the request.
87 88 89 90 91 92 93 |
# File 'lib/hanami/action/request.rb', line 87 def flash unless session_enabled? raise Hanami::Action::MissingSessionError.new("Hanami::Action::Request#flash") end @flash ||= Flash.new(session[Flash::KEY]) end |
#id ⇒ String
Returns the request’s ID
43 44 45 46 |
# File 'lib/hanami/action/request.rb', line 43 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 |
#session ⇒ Hanami::Request::Session
Returns the session for the request.
69 70 71 72 73 74 75 |
# File 'lib/hanami/action/request.rb', line 69 def session unless session_enabled? raise Hanami::Action::MissingSessionError.new("Hanami::Action::Request#session") end @session ||= Session.new(super) end |
#session_enabled? ⇒ Boolean
Returns true if the session is enabled for the request.
54 55 56 |
# File 'lib/hanami/action/request.rb', line 54 def session_enabled? @session_enabled end |
#subdomain(tld_length = @default_tld_length) ⇒ String
Returns the subdomain for the current host.
116 117 118 |
# File 'lib/hanami/action/request.rb', line 116 def subdomain(tld_length = @default_tld_length) subdomains(tld_length).join(".") end |
#subdomains(tld_length = @default_tld_length) ⇒ Array<String>
Returns the subdomains for the current host.
101 102 103 104 105 |
# File 'lib/hanami/action/request.rb', line 101 def subdomains(tld_length = @default_tld_length) return [] if IP_ADDRESS_HOST_REGEXP.match?(host) host.split(".")[0..-(tld_length + 2)] end |