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
-
#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.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/hanami/api/block/context.rb', line 138 def call case caught in String => body [status, headers, [body]] in Enumerator => body [status, headers, body] in Integer => status # # 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)]] in [Integer => status, String => body] [status, headers, [body]] in [Integer => status, Enumerator => body] [status, headers, body] in [Integer => status, Hash => caught_headers, String => body] headers.merge!(caught_headers) [status, headers, [body]] in [Integer => status, Hash => caught_headers, Enumerator => body] headers.merge!(caught_headers) [status, headers, body] 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 129 130 131 132 133 |
# File 'lib/hanami/api/block/context.rb', line 125 def json(object, mime = "application/json") headers["Content-Type"] = mime case object in Enumerator => collection json_enum(collection) else JSON.generate(object) end 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 |