Class: RackStep::App

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

Overview

Abstract class with the base of a RackStep app. This class MUST be extended by the user.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ App

Initialize the request instance variable and add a default “not found” route.



33
34
35
36
37
38
# File 'lib/rackstep.rb', line 33

def initialize(env)
  @request = Rack::Request.new(env)

  # Adding default routes to handle page not found (404).
  router.add_route_for_all_verbs('notfound', 'RackStep::NotFoundController')
end

Instance Attribute Details

#requestObject (readonly)

Stores the received request which will then be injected into the user controllers.



20
21
22
# File 'lib/rackstep.rb', line 20

def request
  @request
end

Class Method Details

.add_route(verb, path, controller) ⇒ Object

This method was created to make it easier for the user to add routes, but it will delegate to the router singleton class.



67
68
69
70
# File 'lib/rackstep.rb', line 67

def self.add_route(verb, path, controller)
  router = Router.instance
  router.add_route(verb, path, controller)
end

.call(env) ⇒ Object

Static method called from config.ru (“run App”).



28
29
30
# File 'lib/rackstep.rb', line 28

def self.call(env)
  new(env).process_request
end

Instance Method Details

#process_requestObject

TODO: Code Climate says this method is too big.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rackstep.rb', line 41

def process_request
  verb = request.request_method
  path = request.path

  # In RackStep, each request is processed by a controller. The router 
  # is responsable to find, based on the given path and http verb,
  # the apropriate controller to handle the request.
  route = router.find_route_for(path, verb)
  # Initialize the correspondent controller.
  controller = Object.const_get(route.controller).new
  # Inject the request into the controller.
  controller.request = request
  # Execute the before method of this controller.
  controller.send(:before)
  # Execute the apropriate method/action.
  controller.send(:process_request)
  # Execute the after method of this controller.
  controller.send(:after)
  # Get from the controller what is the response for this request.
  response = controller.response

  return response
end

#routerObject

Access the Router, a singleton class that stores all the registred routes.



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

def router
  Router.instance
end