Class: Jetski::Router::Host::Controller

Inherits:
Base
  • Object
show all
Defined in:
lib/jetski/router/host/controller.rb

Instance Attribute Summary

Attributes inherited from Base

#action_name, #all_server_options, #controller_class, #controller_classname, #controller_file_name, #controller_name, #controller_path, #errors, #req, #request_method, #res, #served_url, #server

Instance Method Summary collapse

Constructor Details

#initialize(server, **server_options) ⇒ Controller

Responsibility is to host the route with methods to route url to controller action URL -> Controller#Action Uses controller class name and action name to serve request



9
10
11
# File 'lib/jetski/router/host/controller.rb', line 9

def initialize(server, **server_options)
  super(server, **server_options)
end

Instance Method Details

#callObject



13
14
15
16
17
18
# File 'lib/jetski/router/host/controller.rb', line 13

def call
  super do
    fetch_controller_class if errors.empty?
    call_controller if errors.empty?
  end
end

#call_controllerObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jetski/router/host/controller.rb', line 20

def call_controller
  controller = controller_class.new(res)
  controller.action_name = action_name
  controller.controller_name = controller_name
  controller.controller_path = controller_path
  params_hash = {}
  id_crud_actions = ["show", "edit", "destroy"]
  if id_crud_actions.include?(action_name)
    # Need to set id value from url. if available.
    auto_id_param = req.path.split("#{controller_path}/")[-1].split("/")[0]
    params_hash[:id] ||= auto_id_param
  end
  if req.body
    parsed_body = parse_body(req.body, req.content_type)
    params_hash = parsed_body.merge(params_hash)
  end

  # TODO: Need to support deep object structuring.
  controller.params = OpenStruct.new(params_hash)
  controller.cookies = req.cookies
  controller.send(action_name)
  if !controller.performed_render && (request_method.upcase == "GET")
    controller.render
  end
end