Class: Bezel::ControllerBase

Inherits:
Object
  • Object
show all
Defined in:
lib/controller_base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(req, res, route_params = {}) ⇒ ControllerBase

Returns a new instance of ControllerBase.



14
15
16
17
18
19
20
# File 'lib/controller_base.rb', line 14

def initialize(req, res, route_params = {})
  @req = req
  @res = res
  @params = req.params.merge(route_params)
  @flash = Flash.new(req)
  @params['authenticity_token'] ||= SecureRandom.base64
end

Instance Attribute Details

#flashObject (readonly)

Returns the value of attribute flash.



8
9
10
# File 'lib/controller_base.rb', line 8

def flash
  @flash
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/controller_base.rb', line 8

def params
  @params
end

#reqObject (readonly)

Returns the value of attribute req.



8
9
10
# File 'lib/controller_base.rb', line 8

def req
  @req
end

#resObject (readonly)

Returns the value of attribute res.



8
9
10
# File 'lib/controller_base.rb', line 8

def res
  @res
end

Class Method Details

.protect_from_forgeryObject



10
11
12
# File 'lib/controller_base.rb', line 10

def self.protect_from_forgery
  @@csrf_auth = true
end

Instance Method Details

#already_built_response?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/controller_base.rb', line 32

def already_built_response?
  !!@already_built_response
end

#form_authenticity_tokenObject



22
23
24
25
# File 'lib/controller_base.rb', line 22

def form_authenticity_token
  @res.set_cookie('authenticity_token',@params['authenticity_token'])
  @params['authenticity_token']
end

#invoke_action(name) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/controller_base.rb', line 74

def invoke_action(name)
  if @@csrf_auth && @req.request_method != "GET"
    unless valid_authenticity_token?(@req.cookies['authenticity_token'])
      raise "Invalid authenticity token"
    end
  end

  send(name)
  render(name) unless already_built_response?
end

#redirect_to(url) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/controller_base.rb', line 37

def redirect_to(url)
  raise 'You cannot call render more than once' if already_built_response?
  @res.status = 302
  @res['Location'] = url
  @already_built_response = true

  session.store_session(@res)
end

#render(template_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/controller_base.rb', line 55

def render(template_name)
  body = ''
  file_name = "app/views/"
  file_name += "#{self.class.to_s.underscore}/"
  file_name += "#{template_name}.html.erb"
  File.open(file_name, 'r') do |file|
    file.each_line do |line|
      body += line
    end
  end
  content = ERB.new(body).result(binding)

  render_content(content, "text/html")
end

#render_content(content, content_type) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/controller_base.rb', line 46

def render_content(content, content_type)
  raise 'You cannot call render more than once' if already_built_response?
  @res['Content-Type'] = content_type
  @res.write(content)
  @already_built_response = true

  session.store_session(@res)
end

#sessionObject



70
71
72
# File 'lib/controller_base.rb', line 70

def session
  @session ||= Session.new(@req)
end

#valid_authenticity_token?(token = "") ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/controller_base.rb', line 27

def valid_authenticity_token?(token = "")
  @params['authenticity_token'] == token
end