Class: Hanami::API::Block::Context
- Inherits:
-
Router::Block::Context
- Object
- Router::Block::Context
- Hanami::API::Block::Context
- Defined in:
- lib/hanami/api/block/context.rb
Overview
Execution context for Block syntax
Instance Method Summary collapse
-
#back ⇒ Object
Utility for redirect back using HTTP request header ‘HTTP_REFERER`.
- #body(value = nil) ⇒ Object
-
#call ⇒ Object
private
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength.
-
#halt(status, body = nil) ⇒ Object
Halts the flow of the block and immediately returns with the current HTTP status.
-
#json(object, mime = "application/json") ⇒ Object
Sets a JSON response for the given object.
-
#redirect(url, status = 301) ⇒ Object
Redirects request and immediately halts it.
Instance Method Details
#back ⇒ Object
Utility for redirect back using HTTP request header ‘HTTP_REFERER`
103 104 105 |
# File 'lib/hanami/api/block/context.rb', line 103 def back env["HTTP_REFERER"] || "/" end |
#body ⇒ String #body(value) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/hanami/api/block/context.rb', line 22 def body(value = nil) if value @body = value else @body end end |
#call ⇒ 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.
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/hanami/api/block/context.rb', line 135 def call case caught in String => body [status, headers, [body]] in Integer => status # rubocop:disable Style/RedundantSelf # # NOTE: It must use `self.body` so it will pick the method defined above. # # If `self` isn't enforced, Ruby will try to bind `body` to # the current pattern matching context. # When that happens, the body that was manually set is ignored, # which results in a bug. [status, headers, [self.body || http_status(status)]] # rubocop:enable Style/RedundantSelf in [Integer, String] => response [response[0], headers, [response[1]]] in [Integer, Hash, String] => response headers.merge!(response[1]) [response[0], headers, [response[2]]] end end |
#halt(status, body = nil) ⇒ Object
Halts the flow of the block and immediately returns with the current HTTP status
55 56 57 58 |
# File 'lib/hanami/api/block/context.rb', line 55 def halt(status, body = nil) body ||= http_status(status) throw :halt, [status, body] end |
#json(object, mime = "application/json") ⇒ Object
Sets a JSON response for the given object
125 126 127 128 |
# File 'lib/hanami/api/block/context.rb', line 125 def json(object, mime = "application/json") headers["Content-Type"] = mime JSON.generate(object) end |
#redirect(url, status = 301) ⇒ Object
Redirects request and immediately halts it
86 87 88 89 |
# File 'lib/hanami/api/block/context.rb', line 86 def redirect(url, status = 301) headers["Location"] = url halt(status) end |