Class: Helmet::Handler

Inherits:
Object
  • Object
show all
Includes:
Templates
Defined in:
lib/helmet/handler.rb

Overview

Handle each request and provide usefull methods for request handling

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Templates

#erb, #render

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

#envObject (readonly)

Request environment



13
14
15
# File 'lib/helmet/handler.rb', line 13

def env
  @env
end

#responseObject (readonly)

Returns the value of attribute response.



15
16
17
# File 'lib/helmet/handler.rb', line 15

def response
  @response
end

Instance Method Details

#bodyString



64
65
66
# File 'lib/helmet/handler.rb', line 64

def body
  @response.body
end

#configHash



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

#headerHash



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

#paramsHash



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

#sessionRack::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