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 all instance variables and add a default “not found” route.



39
40
41
42
43
44
45
# File 'lib/rackstep.rb', line 39

def initialize(env)
  @request = Rack::Request.new(env)
  @settings = RackStep::GlobalConfiguration.instance.settings

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

Instance Attribute Details

#requestObject (readonly)

Will store the received request which will be injected into the user controllers.



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

def request
  @request
end

#settingsObject

A hash that will be injected into the controller. This hash may contain “global” settings, like a connection to database and other things that should be initiaized only once while the app is starting.



26
27
28
# File 'lib/rackstep.rb', line 26

def settings
  @settings
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.



76
77
78
79
# File 'lib/rackstep.rb', line 76

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”).



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

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

Instance Method Details

#process_requestObject

TODO: Code Climate says this method is too big.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rackstep.rb', line 48

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
  # Inject the settings into the controller.
  controller.settings = settings
  # 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

Router is a singleton that will store all the registred routes.



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

def router
  Router.instance
end