class Bezel::ControllerBase

Attributes

flash[R]
params[R]
req[R]
res[R]

Public Class Methods

new(req, res, route_params = {}) click to toggle source
# 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
protect_from_forgery() click to toggle source
# File lib/controller_base.rb, line 10
def self.protect_from_forgery
  @@csrf_auth = true
end

Public Instance Methods

already_built_response?() click to toggle source
# File lib/controller_base.rb, line 32
def already_built_response?
  !!@already_built_response
end
form_authenticity_token() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
session() click to toggle source
# File lib/controller_base.rb, line 70
def session
  @session ||= Session.new(@req)
end
valid_authenticity_token?(token = "") click to toggle source
# File lib/controller_base.rb, line 27
def valid_authenticity_token?(token = "")
  @params['authenticity_token'] == token
end