Class: Helmet::Handler
- Inherits:
-
Object
- Object
- Helmet::Handler
- Includes:
- Templates
- Defined in:
- lib/helmet/handler.rb
Overview
Handle each request and provide usefull methods for request handling
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Request environment.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#body ⇒ String
Response body.
-
#config ⇒ Hash
Config object.
-
#content_type(ext) ⇒ String
Set content-type header based on extension.
-
#halt(body = nil) ⇒ Object
halt the execution.
-
#handle!(&block) ⇒ Response
Request response.
-
#header ⇒ Hash
Response header.
-
#initialize(env, klass) ⇒ Handler
constructor
A new instance of Handler.
-
#mime_type(ext) ⇒ String
Return mime type based on extension Uses Rack::Mime library.
-
#params ⇒ Hash
Request params.
-
#redirect(uri) ⇒ Object
Creates a redirect HTTP response.
-
#session ⇒ Rack::Session
The current session data.
-
#status(code) ⇒ Object
set response status code.
Methods included from Templates
Constructor Details
#initialize(env, klass) ⇒ Handler
17 18 19 20 21 |
# File 'lib/helmet/handler.rb', line 17 def initialize(env, klass) @env = env @klass = klass @response = Response.new(env) end |
Instance Attribute Details
#env ⇒ Object (readonly)
Request environment
13 14 15 |
# File 'lib/helmet/handler.rb', line 13 def env @env end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
15 16 17 |
# File 'lib/helmet/handler.rb', line 15 def response @response end |
Instance Method Details
#body ⇒ String
64 65 66 |
# File 'lib/helmet/handler.rb', line 64 def body @response.body end |
#config ⇒ Hash
74 75 76 |
# File 'lib/helmet/handler.rb', line 74 def config env.config end |
#content_type(ext) ⇒ String
Set content-type header based on extension
82 83 84 |
# File 'lib/helmet/handler.rb', line 82 def content_type ext header['Content-Type'] = mime_type(ext) end |
#halt(body = nil) ⇒ Object
halt the execution
43 44 45 46 |
# File 'lib/helmet/handler.rb', line 43 def halt(body=nil) @response.body = body if body throw :halt end |
#handle!(&block) ⇒ Response
24 25 26 27 |
# File 'lib/helmet/handler.rb', line 24 def handle!(&block) @response.body = instance_exec(&block) @response.format_response end |
#header ⇒ Hash
59 60 61 |
# File 'lib/helmet/handler.rb', line 59 def header @response.header end |
#mime_type(ext) ⇒ String
Return mime type based on extension Uses Rack::Mime library.
91 92 93 |
# File 'lib/helmet/handler.rb', line 91 def mime_type ext Rack::Mime.mime_type ".#{ext}" end |
#params ⇒ Hash
69 70 71 |
# File 'lib/helmet/handler.rb', line 69 def params env.params || {} end |
#redirect(uri) ⇒ Object
Creates a redirect HTTP response
35 36 37 38 39 40 |
# File 'lib/helmet/handler.rb', line 35 def redirect(uri) @response.status = 302 @response.header['Location'] = uri @response.body = '' halt end |
#session ⇒ Rack::Session
30 31 32 |
# File 'lib/helmet/handler.rb', line 30 def session @env['rack.session'] end |
#status(code) ⇒ Object
set response status code
49 50 51 |
# File 'lib/helmet/handler.rb', line 49 def status @response.status end |